BLPAPI C++  3.22.1
blpapi_correlationid.h
Go to the documentation of this file.
1 /* Copyright 2012. Bloomberg Finance L.P.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions: The above
9  * copyright notice and this permission notice shall be included in all copies
10  * or substantial portions of the Software.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18  * IN THE SOFTWARE.
19  */
20 
28 #ifndef INCLUDED_BLPAPI_CORRELATIONID
29 #define INCLUDED_BLPAPI_CORRELATIONID
30 
61 #ifndef INCLUDED_BLPAPI_TYPES
62 #include <blpapi_types.h>
63 #endif
64 
65 #ifndef INCLUDED_BLPAPI_DEFS
66 #include <blpapi_defs.h>
67 #endif
68 
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72 
74 struct blpapi_ManagedPtr_t_;
75 typedef struct blpapi_ManagedPtr_t_ blpapi_ManagedPtr_t;
76 
77 typedef int (*blpapi_ManagedPtr_ManagerFunction_t)(
78  blpapi_ManagedPtr_t *managedPtr,
79  const blpapi_ManagedPtr_t *srcPtr,
80  int operation);
81 
82 typedef union {
83  int intValue;
84  void *ptr;
85 } blpapi_ManagedPtr_t_data_;
86 
87 struct blpapi_ManagedPtr_t_ {
88  void *pointer;
89  blpapi_ManagedPtr_t_data_ userData[4];
90  blpapi_ManagedPtr_ManagerFunction_t manager;
91 };
92 
93 typedef struct blpapi_CorrelationId_t_ {
94  unsigned int size : 8; // fill in the size of this struct
95  unsigned int valueType : 4; // type of value held by this correlation id
96  unsigned int classId : 16; // user defined classification id
97  unsigned int reserved : 4; // for internal use must be 0
98 
99  union {
100  blpapi_UInt64_t intValue;
101  blpapi_ManagedPtr_t ptrValue;
102  } value;
103 } blpapi_CorrelationId_t;
104 
105 #ifdef __cplusplus
106 }
107 
109 
110 #include <cstring>
111 #include <ostream>
112 #include <utility>
113 
121 namespace BloombergLP {
122 namespace blpapi {
123 
124 // ===================
125 // class CorrelationId
126 // ===================
127 
202 
203  blpapi_CorrelationId_t d_impl;
204 
205  void copy(const blpapi_CorrelationId_t& src);
206 
207  template <typename TYPE>
208  static int managerFunc(blpapi_ManagedPtr_t *managedPtr,
209  const blpapi_ManagedPtr_t *srcPtr,
210  int operation);
211 
212  template <typename TYPE> static void assertSmartPtrFits();
213 
214  public:
215  // Possible return values for valueType() method.
216 
217  enum ValueType {
229  };
230 
231  // The maximum value allowed for classId
232 
233  enum {
235  };
237 
238  CorrelationId();
246  CorrelationId(const blpapi_CorrelationId_t& correlation);
251 
253  CorrelationId(const CorrelationId& original);
259  explicit CorrelationId(long long value, int classId = 0);
266  explicit CorrelationId(void *value, int classId = 0);
273  template <typename TYPE>
274  CorrelationId(const TYPE& smartPtr, void *pointerValue, int classId = 0);
283  ~CorrelationId();
289  void swap(CorrelationId& other);
302  ValueType valueType() const;
307  unsigned short classId() const;
313  void *asPointer() const;
320  template <typename TYPE> TYPE asSmartPointer() const;
328  long long asInteger() const;
335  blpapi_CorrelationId_t& impl();
337  const blpapi_CorrelationId_t& impl() const;
339 };
340 
344 // FREE OPERATORS
345 inline bool operator==(const CorrelationId& lhs, const CorrelationId& rhs);
354 inline bool operator!=(const CorrelationId& lhs, const CorrelationId& rhs);
359 inline bool operator<(const CorrelationId& lhs, const CorrelationId& rhs);
370 //=============================================================================
371 // INLINE FUNCTION DEFINITIONS
372 //=============================================================================
373 
374 // -------------------
375 // class CorrelationId
376 // -------------------
377 
379 {
380  std::memset(&d_impl, 0, sizeof(d_impl));
381 }
382 
384  const blpapi_CorrelationId_t& correlationId)
385 {
386  copy(correlationId);
387 }
388 
390 {
391  copy(original.d_impl);
392 }
393 
394 inline CorrelationId::CorrelationId(long long intValue, int newClassId)
395 {
396  std::memset(&d_impl, 0, sizeof(d_impl));
397 
398  d_impl.size = static_cast<unsigned>(sizeof(d_impl));
399  d_impl.valueType = INT_VALUE;
400  d_impl.value.intValue = static_cast<blpapi_UInt64_t>(intValue);
401  d_impl.classId = static_cast<unsigned>(newClassId);
402 }
403 
404 inline CorrelationId::CorrelationId(void *ptrValue, int newClassId)
405 {
406  std::memset(&d_impl, 0, sizeof(d_impl));
407 
408  d_impl.size = static_cast<unsigned>(sizeof(d_impl));
409  d_impl.valueType = POINTER_VALUE;
410  d_impl.value.ptrValue.pointer = ptrValue;
411  d_impl.classId = static_cast<unsigned>(newClassId);
412 }
413 
414 template <typename TYPE>
416  const TYPE& smartPtr, void *ptrValue, int newClassId)
417 {
418  // If you get a compiler error here, the specified smart pointer does not
419  // fit in the CorrelationId and cannot be used at this time.
420 
421  assertSmartPtrFits<TYPE>();
422 
423  std::memset(&d_impl, 0, sizeof(d_impl));
424 
425  d_impl.size = sizeof(d_impl);
426  d_impl.valueType = POINTER_VALUE;
427  d_impl.classId = newClassId;
428 
429  d_impl.value.ptrValue.pointer = ptrValue;
430  d_impl.value.ptrValue.manager = &CorrelationId::managerFunc<TYPE>;
431 
432  void *arena = (void *)d_impl.value.ptrValue.userData;
433  new (arena) TYPE(smartPtr);
434 }
435 
437 {
438  if (POINTER_VALUE == valueType()) {
439  blpapi_ManagedPtr_ManagerFunction_t& manager
440  = d_impl.value.ptrValue.manager;
441  if (manager) {
442  manager(&d_impl.value.ptrValue, 0, BLPAPI_MANAGEDPTR_DESTROY);
443  }
444  }
445 }
446 
448 {
449  using std::swap;
450 
451  swap(other.d_impl, d_impl);
452 }
453 
455 {
456  CorrelationId tmp(rhs);
457  tmp.swap(*this);
458  return *this;
459 }
460 
461 inline blpapi_CorrelationId_t& CorrelationId::impl() { return d_impl; }
462 
464 {
465  return (ValueType)d_impl.valueType;
466 }
467 
468 inline unsigned short CorrelationId::classId() const { return d_impl.classId; }
469 
470 inline void *CorrelationId::asPointer() const
471 {
472  return d_impl.value.ptrValue.pointer;
473 }
474 
475 template <typename TYPE> inline TYPE CorrelationId::asSmartPointer() const
476 {
477  typedef int (*ManagerFuncPtr)(
478  blpapi_ManagedPtr_t *, const blpapi_ManagedPtr_t *, int);
479 
480  ManagerFuncPtr managerFnPtr
481  = static_cast<ManagerFuncPtr>(&(CorrelationId::managerFunc<TYPE>));
482  if (d_impl.valueType != POINTER_VALUE
483  || (d_impl.value.ptrValue.manager != managerFnPtr)) {
484  return TYPE();
485  }
486  return *(TYPE *)d_impl.value.ptrValue.userData;
487 }
488 
489 inline long long CorrelationId::asInteger() const
490 {
491  return static_cast<long long>(d_impl.value.intValue);
492 }
493 
494 inline const blpapi_CorrelationId_t& CorrelationId::impl() const
495 {
496  return d_impl;
497 }
498 
499 inline void CorrelationId::copy(const blpapi_CorrelationId_t& src)
500 {
501  d_impl = src;
502 
503  if (POINTER_VALUE == valueType()) {
504  blpapi_ManagedPtr_ManagerFunction_t& manager
505  = d_impl.value.ptrValue.manager;
506  if (manager) {
507  manager(&d_impl.value.ptrValue,
508  &src.value.ptrValue,
510  }
511  }
512 }
513 
514 template <typename TYPE>
515 inline int CorrelationId::managerFunc(blpapi_ManagedPtr_t *managedPtr,
516  const blpapi_ManagedPtr_t *srcPtr,
517  int operation)
518 {
519  if (operation == BLPAPI_MANAGEDPTR_COPY) {
520  managedPtr->pointer = srcPtr->pointer;
521  managedPtr->manager = srcPtr->manager;
522 
523  void *arena = managedPtr->userData;
524  new (arena) TYPE(*((TYPE *)&srcPtr->userData[0]));
525  } else if (operation == BLPAPI_MANAGEDPTR_DESTROY) {
526  TYPE *managedPtr_p = (TYPE *)&managedPtr->userData[0];
527  managedPtr_p->~TYPE();
528  } else if (operation == BLPAPI_MANAGEDPTR_IMPOSSIBLE_OPERATION) {
529  static int uniquePerTemplateInstantiation;
530 
531  const int *address = &uniquePerTemplateInstantiation;
532  int rc = 0;
533  std::memcpy(&rc, &address, std::min(sizeof address, sizeof rc));
534  return rc;
535  // Instantiations of this function template, 'managerFunc<TYPE>',
536  // will never be called with 'operation' equal to
537  // 'BLPAPI_MANAGEDPTR_IMPOSSIBLE_OPERATION': this branch will never
538  // be executed.
539  //
540  // The observable use of the address of a local static variable
541  // forces '&managerFunc<T1> != &managerFunc<T2>' for types
542  // 'T1 != T2' even in the presence of Visual C++'s "identical
543  // COMDAT folding" optimization.
544  }
545 
546  return 0;
547 }
548 
549 template <typename TYPE> inline void CorrelationId::assertSmartPtrFits()
550 {
551  if (false) {
552  // If you get a compiler error here, the specified smart pointer does
553  // not fit in the CorrelationId and cannot be used at this time.
554 
555  char errorIfSmartPtrDoesNotFit[sizeof(TYPE) <= (sizeof(void *) * 4)
556  ? 1
557  : -1];
558  (void)errorIfSmartPtrDoesNotFit;
559  }
560 }
561 
562 inline bool operator==(const CorrelationId& lhs, const CorrelationId& rhs)
563 {
564  if (lhs.valueType() != rhs.valueType()) {
565  return false;
566  }
567  if (lhs.classId() != rhs.classId()) {
568  return false;
569  }
570 
572  if (lhs.asPointer() != rhs.asPointer()) {
573  return false;
574  }
575  } else if (lhs.asInteger() != rhs.asInteger()) {
576  return false;
577  }
578 
579  return true;
580 }
581 
582 inline bool operator!=(const CorrelationId& lhs, const CorrelationId& rhs)
583 {
584  return !(lhs == rhs);
585 }
586 
587 inline bool operator<(const CorrelationId& lhs, const CorrelationId& rhs)
588 {
589  return std::memcmp(&lhs.impl(), &rhs.impl(), sizeof(rhs.impl())) < 0;
590 }
591 
592 inline std::ostream& operator<<(
593  std::ostream& os, const CorrelationId& correlator)
594 {
595  const char *valueType = 0;
596  switch (correlator.valueType()) {
598  valueType = "UNSET";
599  break;
601  valueType = "INT";
602  break;
604  valueType = "POINTER";
605  break;
607  valueType = "AUTOGEN";
608  break;
609  default:
610  valueType = "UNKNOWN";
611  }
612 
613  os << "[ valueType=" << valueType << " classId=" << correlator.classId()
614  << " value=";
615 
616  if (correlator.valueType() == CorrelationId::POINTER_VALUE) {
617  os << correlator.asPointer();
618  } else
619  os << correlator.asInteger();
620  os << " ]";
621 
622  return os;
623 }
624 
625 } // close namespace blpapi
626 } // close namespace BloombergLP
627 
628 #endif // #ifdef __cplusplus
629 #endif // #ifndef INCLUDED_BLPAPI_CORRELATIONID
ValueType valueType() const
Definition: blpapi_correlationid.h:463
The CorrelationId was created internally by API.
Definition: blpapi_correlationid.h:227
~CorrelationId()
Definition: blpapi_correlationid.h:436
bool operator!=(const CorrelationId &lhs, const CorrelationId &rhs)
Definition: blpapi_correlationid.h:582
#define BLPAPI_CORRELATION_TYPE_POINTER
Definition: blpapi_defs.h:53
#define BLPAPI_CORRELATION_TYPE_AUTOGEN
Definition: blpapi_defs.h:54
Common definitions used by the library.
bool operator==(const CorrelationId &lhs, const CorrelationId &rhs)
Definition: blpapi_correlationid.h:562
void swap(Event::iterator &lhs, Event::iterator &rhs)
Swap the contents of the lhs and rhs iterators.
Definition: blpapi_event.h:720
bool operator<(const CorrelationId &lhs, const CorrelationId &rhs)
Definition: blpapi_correlationid.h:587
Definition: blpapi_abstractsession.h:220
#define BLPAPI_MANAGEDPTR_DESTROY
Definition: blpapi_defs.h:58
Definition: blpapi_correlationid.h:221
#define BLPAPI_CORRELATION_MAX_CLASS_ID
Definition: blpapi_defs.h:55
Definition: blpapi_correlationid.h:234
long long asInteger() const
Definition: blpapi_correlationid.h:489
Definition: blpapi_correlationid.h:218
Definition: blpapi_correlationid.h:224
void swap(CorrelationId &other)
Definition: blpapi_correlationid.h:447
CorrelationId()
Definition: blpapi_correlationid.h:378
unsigned long long blpapi_UInt64_t
Definition: blpapi_types.h:70
std::ostream & operator<<(std::ostream &os, const CorrelationId &correlator)
Definition: blpapi_correlationid.h:592
Definition: blpapi_correlationid.h:201
#define BLPAPI_CORRELATION_TYPE_UNSET
Definition: blpapi_defs.h:51
CorrelationId & operator=(const CorrelationId &rhs)
Definition: blpapi_correlationid.h:454
#define BLPAPI_MANAGEDPTR_IMPOSSIBLE_OPERATION
Definition: blpapi_defs.h:59
ValueType
Definition: blpapi_correlationid.h:217
TYPE asSmartPointer() const
Definition: blpapi_correlationid.h:475
#define BLPAPI_CORRELATION_TYPE_INT
Definition: blpapi_defs.h:52
Provide BLPAPI types.
void * asPointer() const
Definition: blpapi_correlationid.h:470
unsigned short classId() const
Definition: blpapi_correlationid.h:468
#define BLPAPI_MANAGEDPTR_COPY
Definition: blpapi_defs.h:57