libbmq  a5f8a06ba1d16cb5a65643e1fa7f1a1d6aadef40
bmqt_correlationid.h
Go to the documentation of this file.
1 // Copyright 2014-2023 Bloomberg Finance L.P.
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 // bmqt_correlationid.h -*-C++-*-
17 #ifndef INCLUDED_BMQT_CORRELATIONID
18 #define INCLUDED_BMQT_CORRELATIONID
19 
173 
174 // BDE
175 #include <bdlb_variant.h>
176 
177 #include <bsl_cstdint.h>
178 #include <bsl_iosfwd.h>
179 #include <bsl_memory.h>
180 #include <bslh_hash.h>
181 #include <bsls_assert.h>
182 #include <bsls_types.h>
183 
184 namespace BloombergLP {
185 namespace bmqt {
186 
187 // ===================
188 // class CorrelationId
189 // ===================
190 
194  // FRIENDS
195  // Needed to access getAsAutoValue
196  friend bool operator==(const CorrelationId& lhs, const CorrelationId& rhs);
197  friend bool operator!=(const CorrelationId& lhs, const CorrelationId& rhs);
198  friend struct CorrelationIdLess;
199  friend bsl::ostream& operator<<(bsl::ostream& stream,
200  const CorrelationId& rhs);
201 
202  template <class HASH_ALGORITHM>
203  friend void hashAppend(HASH_ALGORITHM& hashAlgo,
204  const CorrelationId& value);
205 
206  private:
207  // PRIVATE TYPES
208 
211  typedef bsls::Types::Uint64 AutoValue;
212 
213  // DATA
214 
218  bdlb::Variant4<bsls::Types::Int64, void*, bsl::shared_ptr<void>, AutoValue>
219  d_variant;
220 
221  // PRIVATE ACCESSORS
222 
227  AutoValue theAutoValue() const;
228 
229  public:
230  // TYPES
231  enum Type {
232  e_NUMERIC // the 'CorrelationId' holds a 64-bit integer
233  ,
234  e_POINTER // the 'CorrelationId' holds a raw pointer
235  ,
236  e_SHARED_PTR // the 'CorrelationId' holds a shared pointer
237  ,
238  e_AUTO_VALUE // the 'CorrelationId' holds an auto value
239  ,
240  e_UNSET // the 'CorrelationId' is not set
241  };
242 
243  // CLASS METHOD
244 
248 
249  // CREATORS
250 
252  explicit CorrelationId();
253 
256  explicit CorrelationId(bsls::Types::Int64 numeric);
257 
260  explicit CorrelationId(void* pointer);
261 
264  explicit CorrelationId(const bsl::shared_ptr<void>& sharedPtr);
265 
266  // MANIPULATORS
267 
270 
273  CorrelationId& setNumeric(bsls::Types::Int64 numeric);
274 
277  CorrelationId& setPointer(void* pointer);
278 
281  CorrelationId& setSharedPointer(const bsl::shared_ptr<void>& sharedPtr);
282 
283  // void setAutoValue
284  // There is explicitly no setAuto, autoCorrelation should only be used
285  // at creation
286 
287  // ACCESSORS
288 
290  bool isUnset() const;
291 
294  bool isNumeric() const;
295 
298  bool isPointer() const;
299 
302  bool isSharedPtr() const;
303 
306  bool isAutoValue() const;
307 
310  bsls::Types::Int64 theNumeric() const;
311 
314  void* thePointer() const;
315 
318  const bsl::shared_ptr<void>& theSharedPtr() const;
319 
321  Type type() const;
322 
332  bsl::ostream&
333  print(bsl::ostream& stream, int level = 0, int spacesPerLevel = 4) const;
334 };
335 
336 // FREE OPERATORS
337 
338 // -------------------
339 // class CorrelationId
340 // -------------------
341 
344 bsl::ostream& operator<<(bsl::ostream& stream, const CorrelationId& rhs);
345 
349 bool operator==(const CorrelationId& lhs, const CorrelationId& rhs);
350 
354 bool operator!=(const CorrelationId& lhs, const CorrelationId& rhs);
355 
358 bool operator<(const CorrelationId& lhs, const CorrelationId& rhs);
359 
360 // =======================
361 // class CorrelationIdLess
362 // =======================
363 
365  // ACCESSORS
366 
369  bool operator()(const CorrelationId& lhs, const CorrelationId& rhs) const;
370 };
371 
372 // ============================================================================
373 // INLINE DEFINITIONS
374 // ============================================================================
375 
376 // -------------------
377 // class CorrelationId
378 // -------------------
379 
380 inline CorrelationId::AutoValue CorrelationId::theAutoValue() const
381 {
382  // PRECONDITIONS
383  BSLS_ASSERT(isAutoValue());
384 
385  return d_variant.the<AutoValue>();
386 }
387 
388 // CREATORS
390 : d_variant()
391 {
392  // NOTHING
393 }
394 
395 inline CorrelationId::CorrelationId(bsls::Types::Int64 numeric)
396 : d_variant(numeric)
397 {
398  // NOTHING
399 }
400 
401 inline CorrelationId::CorrelationId(void* pointer)
402 : d_variant(pointer)
403 {
404  // NOTHING
405 }
406 
407 inline CorrelationId::CorrelationId(const bsl::shared_ptr<void>& sharedPtr)
408 : d_variant(sharedPtr)
409 {
410  // NOTHING
411 }
412 
413 // MANIPULATORS
415 {
416  d_variant.reset();
417  return *this;
418 }
419 
420 inline CorrelationId& CorrelationId::setNumeric(bsls::Types::Int64 numeric)
421 {
422  d_variant.assign(numeric);
423  return *this;
424 }
425 
427 {
428  d_variant.assign(pointer);
429  return *this;
430 }
431 
432 inline CorrelationId&
433 CorrelationId::setSharedPointer(const bsl::shared_ptr<void>& sharedPtr)
434 {
435  d_variant.assign(sharedPtr);
436  return *this;
437 }
438 
439 // ACCESSORS
440 inline bool CorrelationId::isUnset() const
441 {
442  return d_variant.isUnset();
443 }
444 
445 inline bool CorrelationId::isNumeric() const
446 {
447  return d_variant.is<bsls::Types::Int64>();
448 }
449 
450 inline bool CorrelationId::isPointer() const
451 {
452  return d_variant.is<void*>();
453 }
454 
455 inline bool CorrelationId::isSharedPtr() const
456 {
457  return d_variant.is<bsl::shared_ptr<void> >();
458 }
459 
460 inline bool CorrelationId::isAutoValue() const
461 {
462  return d_variant.is<AutoValue>();
463 }
464 
465 inline bsls::Types::Int64 CorrelationId::theNumeric() const
466 {
467  // PRECONDITIONS
468  BSLS_ASSERT(isNumeric());
469 
470  return d_variant.the<bsls::Types::Int64>();
471 }
472 
473 inline void* CorrelationId::thePointer() const
474 {
475  // PRECONDITIONS
476  BSLS_ASSERT(isPointer());
477 
478  return d_variant.the<void*>();
479 }
480 
481 inline const bsl::shared_ptr<void>& CorrelationId::theSharedPtr() const
482 {
483  // PRECONDITIONS
484  BSLS_ASSERT(isSharedPtr());
485 
486  return d_variant.the<bsl::shared_ptr<void> >();
487 }
488 
490 {
491  int result = isUnset() ? e_UNSET
492  : isNumeric() ? e_NUMERIC
493  : isPointer() ? e_POINTER
496  : -1;
497  BSLS_ASSERT_OPT(-1 != result && "Unknown correlation type");
498 
499  return static_cast<Type>(result);
500 }
501 
502 // FREE FUNCTIONS
503 template <class HASH_ALGORITHM>
504 void hashAppend(HASH_ALGORITHM& hashAlgo, const CorrelationId& value)
505 {
506  using bslh::hashAppend; // for ADL
507 
508  CorrelationId::Type type = value.type();
509  hashAppend(hashAlgo, static_cast<int>(type));
510 
511  switch (type) {
513  hashAppend(hashAlgo, value.theNumeric());
514  } break;
516  hashAppend(hashAlgo, value.thePointer());
517  } break;
519  hashAppend(hashAlgo, value.theSharedPtr());
520  } break;
522  hashAppend(hashAlgo, value.theAutoValue());
523  } break;
524  case CorrelationId::e_UNSET: {
525  hashAppend(hashAlgo, 0);
526  } break;
527  default: {
528  BSLS_ASSERT_OPT(false && "Unknown correlationId type");
529  hashAppend(hashAlgo, 0);
530  }
531  }
532 }
533 
534 // ------------------------
535 // struct CorrelationIdLess
536 // ------------------------
537 
539  const CorrelationId& rhs) const
540 {
541  // If 'lhs' and 'rhs' have different types, they are compared by their
542  // corresponding type indices. Otherwise, they are compared by value.
543 
544  bool result;
545  if (lhs.d_variant.typeIndex() != rhs.d_variant.typeIndex()) {
546  result = lhs.d_variant.typeIndex() < rhs.d_variant.typeIndex();
547  }
548  else if (lhs.isNumeric()) {
549  result = lhs.theNumeric() < rhs.theNumeric();
550  }
551  else if (lhs.isPointer()) {
552  result = reinterpret_cast<bsl::uintptr_t>(lhs.thePointer()) <
553  reinterpret_cast<bsl::uintptr_t>(rhs.thePointer());
554  }
555  else if (lhs.isSharedPtr()) {
556  result = lhs.theSharedPtr() < rhs.theSharedPtr();
557  }
558  else if (lhs.isAutoValue()) {
559  result = lhs.theAutoValue() < rhs.theAutoValue();
560  }
561  else {
562  BSLS_ASSERT_OPT(false && "Unknown correlator type");
563  result = false;
564  }
565 
566  return result;
567 }
568 
569 } // close package namespace
570 
571 // -------------------
572 // class CorrelationId
573 // -------------------
574 
575 // FREE OPERATORS
576 inline bool bmqt::operator==(const bmqt::CorrelationId& lhs,
577  const bmqt::CorrelationId& rhs)
578 {
579  return lhs.d_variant == rhs.d_variant;
580 }
581 
582 inline bool bmqt::operator!=(const bmqt::CorrelationId& lhs,
583  const bmqt::CorrelationId& rhs)
584 {
585  return lhs.d_variant != rhs.d_variant;
586 }
587 
588 inline bool bmqt::operator<(const bmqt::CorrelationId& lhs,
589  const bmqt::CorrelationId& rhs)
590 {
591  CorrelationIdLess less;
592  return less(lhs, rhs);
593 }
594 
595 inline bsl::ostream& bmqt::operator<<(bsl::ostream& stream,
596  const bmqt::CorrelationId& rhs)
597 {
598  return rhs.print(stream, 0, -1);
599 }
600 
601 } // close enterprise namespace
602 
603 #endif
Definition: bmqt_correlationid.h:193
friend bool operator==(const CorrelationId &lhs, const CorrelationId &rhs)
CorrelationId & setSharedPointer(const bsl::shared_ptr< void > &sharedPtr)
Definition: bmqt_correlationid.h:433
const bsl::shared_ptr< void > & theSharedPtr() const
Definition: bmqt_correlationid.h:481
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
bool isNumeric() const
Definition: bmqt_correlationid.h:445
bool isSharedPtr() const
Definition: bmqt_correlationid.h:455
bool isUnset() const
Return true if the value of this object is unset;.
Definition: bmqt_correlationid.h:440
Type type() const
Return the type of the value of this object.
Definition: bmqt_correlationid.h:489
void * thePointer() const
Definition: bmqt_correlationid.h:473
bool isPointer() const
Definition: bmqt_correlationid.h:450
CorrelationId & setPointer(void *pointer)
Definition: bmqt_correlationid.h:426
Type
Definition: bmqt_correlationid.h:231
@ e_SHARED_PTR
Definition: bmqt_correlationid.h:236
@ e_AUTO_VALUE
Definition: bmqt_correlationid.h:238
@ e_NUMERIC
Definition: bmqt_correlationid.h:232
@ e_UNSET
Definition: bmqt_correlationid.h:240
@ e_POINTER
Definition: bmqt_correlationid.h:234
static CorrelationId autoValue()
bsls::Types::Int64 theNumeric() const
Definition: bmqt_correlationid.h:465
friend void hashAppend(HASH_ALGORITHM &hashAlgo, const CorrelationId &value)
Definition: bmqt_correlationid.h:504
bool isAutoValue() const
Definition: bmqt_correlationid.h:460
CorrelationId & setNumeric(bsls::Types::Int64 numeric)
Definition: bmqt_correlationid.h:420
friend bool operator!=(const CorrelationId &lhs, const CorrelationId &rhs)
CorrelationId & makeUnset()
Unset the value of this object.
Definition: bmqt_correlationid.h:414
friend bsl::ostream & operator<<(bsl::ostream &stream, const CorrelationId &rhs)
CorrelationId()
Create a CorrelationId having the default, unset, value.
Definition: bmqt_correlationid.h:389
void hashAppend(HASH_ALGORITHM &hashAlgo, const CorrelationId &value)
Definition: bmqt_correlationid.h:504
bsl::ostream & operator<<(bsl::ostream &stream, CompressionAlgorithmType::Enum value)
Definition: bmqt_compressionalgorithmtype.h:141
bool operator!=(const CorrelationId &lhs, const CorrelationId &rhs)
Definition: bmqt_correlationid.h:582
bool operator==(const CorrelationId &lhs, const CorrelationId &rhs)
Definition: bmqt_correlationid.h:576
bool operator<(const CorrelationId &lhs, const CorrelationId &rhs)
Definition: bmqt_correlationid.h:588
Definition: bmqa_abstractsession.h:42
Definition: bmqt_correlationid.h:364
bool operator()(const CorrelationId &lhs, const CorrelationId &rhs) const
Definition: bmqt_correlationid.h:538