UUID.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2016-2021, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of NVIDIA CORPORATION nor the names of its
  13. * contributors may be used to endorse or promote products derived
  14. * from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  20. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  24. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /**
  29. * @file
  30. * <b>Libargus API: UUID API</b>
  31. *
  32. * @b Description: Defines the UUID types used by libargus.
  33. */
  34. #ifndef _ARGUS_UUID_H
  35. #define _ARGUS_UUID_H
  36. #include <stdint.h>
  37. #include <cstring>
  38. namespace Argus
  39. {
  40. const uint32_t MAX_UUID_NAME_SIZE = 32;
  41. /**
  42. * A universally unique identifier.
  43. */
  44. struct UUID
  45. {
  46. uint32_t time_low;
  47. uint16_t time_mid;
  48. uint16_t time_hi_and_version;
  49. uint16_t clock_seq;
  50. uint8_t node[6];
  51. bool operator==(const UUID &r) const
  52. {
  53. return memcmp(this, &r, sizeof(UUID)) == 0;
  54. }
  55. bool operator<(const UUID &r) const
  56. {
  57. return memcmp(this, &r, sizeof(UUID)) < 0;
  58. }
  59. };
  60. /**
  61. * A universally unique identifier with a name (used for debugging purposes).
  62. */
  63. class NamedUUID : public UUID
  64. {
  65. public:
  66. NamedUUID(uint32_t time_low_
  67. , uint16_t time_mid_
  68. , uint16_t time_hi_and_version_
  69. , uint16_t clock_seq_
  70. , uint8_t c0, uint8_t c1, uint8_t c2, uint8_t c3, uint8_t c4, uint8_t c5
  71. , const char* name)
  72. {
  73. time_low = time_low_;
  74. time_mid = time_mid_;
  75. time_hi_and_version = time_hi_and_version_;
  76. clock_seq = clock_seq_;
  77. node[0] = c0; node[1] = c1; node[2] = c2; node[3] = c3; node[4] = c4; node[5] = c5;
  78. strncpy(m_name, name, sizeof(m_name)-1);
  79. m_name[sizeof(m_name)-1] = '\0';
  80. }
  81. NamedUUID(const NamedUUID& copied)
  82. : UUID(copied)
  83. {
  84. memcpy(m_name, copied.m_name, (strnlen(copied.m_name, (sizeof(m_name) - 1)) + 1));
  85. m_name[sizeof(m_name)-1] = '\0';
  86. }
  87. NamedUUID& operator=(const NamedUUID& copied)
  88. {
  89. static_cast<UUID&>(*this) = copied;
  90. return *this;
  91. }
  92. bool operator==(const NamedUUID& compared) const
  93. {
  94. return static_cast<const UUID&>(*this) == compared;
  95. }
  96. bool operator!=(const NamedUUID& compared) const
  97. {
  98. return !(static_cast<const UUID&>(*this) == compared);
  99. }
  100. const char* getName() const { return m_name; }
  101. private:
  102. char m_name[MAX_UUID_NAME_SIZE];
  103. NamedUUID();
  104. };
  105. /// Helper macro used to define NamedUUID-derived values.
  106. #define DEFINE_UUID(TYPE, NAME, l, s0, s1, s2, c0,c1,c2,c3,c4,c5) \
  107. static const TYPE NAME(0x##l, 0x##s0, 0x##s1, 0x##s2, \
  108. 0x##c0, 0x##c1, 0x##c2, 0x##c3, 0x##c4, 0x##c5, #NAME);
  109. #define DEFINE_NAMED_UUID_CLASS(NAME) \
  110. class NAME : public NamedUUID \
  111. { \
  112. public: \
  113. NAME(uint32_t time_low_ \
  114. , uint16_t time_mid_ \
  115. , uint16_t time_hi_and_version_ \
  116. , uint16_t clock_seq_ \
  117. , uint8_t c0, uint8_t c1, uint8_t c2, uint8_t c3, uint8_t c4, uint8_t c5 \
  118. , const char* name) \
  119. : NamedUUID(time_low_, time_mid_, time_hi_and_version_, clock_seq_, \
  120. c0, c1, c2, c3, c4, c5, name) \
  121. {} \
  122. private: \
  123. NAME();\
  124. };
  125. } // namespace Argus
  126. #endif // _ARGUS_UUID_H