TypesBase.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. // Copyright 2018 Proyectos y Sistemas de Mantenimiento SL (eProsima).
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef TYPES_BASE_H
  15. #define TYPES_BASE_H
  16. #include <fastdds/rtps/common/Types.h>
  17. #include <bitset>
  18. #include <string>
  19. #include <map>
  20. #include <vector>
  21. #include <cctype>
  22. #include <algorithm>
  23. #include <memory>
  24. namespace eprosima {
  25. namespace fastdds {
  26. namespace dds {
  27. using DomainId_t = uint32_t;
  28. } // dds
  29. } // namespace fastdds
  30. namespace fastcdr {
  31. class Cdr;
  32. } // namespace fastcdr
  33. namespace fastrtps {
  34. namespace types {
  35. using eprosima::fastrtps::rtps::octet;
  36. using OctetSeq = std::vector<octet>;
  37. OctetSeq& operator ++(
  38. OctetSeq&);
  39. OctetSeq operator ++(
  40. OctetSeq&,
  41. int);
  42. size_t to_size_t(
  43. const OctetSeq&);
  44. const std::string CONST_TRUE = "true";
  45. const std::string CONST_FALSE = "false";
  46. const std::string ANNOTATION_KEY_ID = "key";
  47. const std::string ANNOTATION_EPKEY_ID = "Key";
  48. const std::string ANNOTATION_TOPIC_ID = "Topic";
  49. const std::string ANNOTATION_EXTENSIBILITY_ID = "extensibility";
  50. const std::string ANNOTATION_FINAL_ID = "final";
  51. const std::string ANNOTATION_APPENDABLE_ID = "appendable";
  52. const std::string ANNOTATION_MUTABLE_ID = "mutable";
  53. const std::string ANNOTATION_NESTED_ID = "nested";
  54. const std::string ANNOTATION_OPTIONAL_ID = "optional";
  55. const std::string ANNOTATION_MUST_UNDERSTAND_ID = "must_understand";
  56. const std::string ANNOTATION_NON_SERIALIZED_ID = "non_serialized";
  57. const std::string ANNOTATION_BIT_BOUND_ID = "bit_bound";
  58. const std::string ANNOTATION_DEFAULT_ID = "default";
  59. const std::string ANNOTATION_DEFAULT_LITERAL_ID = "default_literal";
  60. const std::string ANNOTATION_VALUE_ID = "value";
  61. const std::string ANNOTATION_POSITION_ID = "position";
  62. const std::string EXTENSIBILITY_FINAL = "FINAL";
  63. const std::string EXTENSIBILITY_APPENDABLE = "APPENDABLE";
  64. const std::string EXTENSIBILITY_MUTABLE = "MUTABLE";
  65. const std::string TKNAME_BOOLEAN = "bool";
  66. const std::string TKNAME_INT16 = "int16_t";
  67. const std::string TKNAME_UINT16 = "uint16_t";
  68. const std::string TKNAME_INT32 = "int32_t";
  69. const std::string TKNAME_UINT32 = "uint32_t";
  70. const std::string TKNAME_INT64 = "int64_t";
  71. const std::string TKNAME_UINT64 = "uint64_t";
  72. const std::string TKNAME_CHAR8 = "char";
  73. const std::string TKNAME_BYTE = "octet";
  74. const std::string TKNAME_INT8 = "int8_t";
  75. const std::string TKNAME_UINT8 = "uint8_t";
  76. const std::string TKNAME_CHAR16 = "wchar";
  77. const std::string TKNAME_CHAR16T = "wchar_t";
  78. const std::string TKNAME_FLOAT32 = "float";
  79. const std::string TKNAME_FLOAT64 = "double";
  80. const std::string TKNAME_FLOAT128 = "longdouble";
  81. const std::string TKNAME_STRING8 = "string";
  82. const std::string TKNAME_STRING16 = "wstring";
  83. const std::string TKNAME_ALIAS = "alias";
  84. const std::string TKNAME_ENUM = "enum";
  85. const std::string TKNAME_BITMASK = "bitmask";
  86. const std::string TKNAME_ANNOTATION = "annotation";
  87. const std::string TKNAME_STRUCTURE = "structure";
  88. const std::string TKNAME_UNION = "union";
  89. const std::string TKNAME_BITSET = "bitset";
  90. const std::string TKNAME_SEQUENCE = "sequence";
  91. const std::string TKNAME_ARRAY = "array";
  92. const std::string TKNAME_MAP = "map";
  93. // ---------- Equivalence Kinds ------------------
  94. typedef octet EquivalenceKind;
  95. const octet EK_MINIMAL = 0xF1; // 0x1111 0001
  96. const octet EK_COMPLETE = 0xF2; // 0x1111 0010
  97. const octet EK_BOTH = 0xF3; // 0x1111 0011
  98. // ---------- TypeKinds (begin) ------------------
  99. typedef octet TypeKind; // Primitive TKs
  100. const octet TK_NONE = 0x00;
  101. const octet TK_BOOLEAN = 0x01;
  102. const octet TK_BYTE = 0x02;
  103. const octet TK_INT16 = 0x03;
  104. const octet TK_INT32 = 0x04;
  105. const octet TK_INT64 = 0x05;
  106. const octet TK_UINT16 = 0x06;
  107. const octet TK_UINT32 = 0x07;
  108. const octet TK_UINT64 = 0x08;
  109. const octet TK_FLOAT32 = 0x09;
  110. const octet TK_FLOAT64 = 0x0A;
  111. const octet TK_FLOAT128 = 0x0B;
  112. const octet TK_CHAR8 = 0x10;
  113. const octet TK_CHAR16 = 0x11;
  114. // String TKs
  115. const octet TK_STRING8 = 0x20;
  116. const octet TK_STRING16 = 0x21;
  117. // Constructed/Named types
  118. const octet TK_ALIAS = 0x30;
  119. // Enumerated TKs
  120. const octet TK_ENUM = 0x40;
  121. const octet TK_BITMASK = 0x41;
  122. // Structured TKs
  123. const octet TK_ANNOTATION = 0x50;
  124. const octet TK_STRUCTURE = 0x51;
  125. const octet TK_UNION = 0x52;
  126. const octet TK_BITSET = 0x53;
  127. // Collection TKs
  128. const octet TK_SEQUENCE = 0x60;
  129. const octet TK_ARRAY = 0x61;
  130. const octet TK_MAP = 0x62;
  131. // ---------- TypeKinds (end) ------------------
  132. // The name of some element (e.g. type, type member, module)
  133. // Valid characters are alphanumeric plus the "_" cannot start with digit
  134. const int32_t MEMBER_NAME_MAX_LENGTH = 256;
  135. typedef std::string MemberName;
  136. // Qualified type name includes the name of containing modules
  137. // using "::" as separator. No leading "::". E.g. "MyModule::MyType"
  138. const int32_t TYPE_NAME_MAX_LENGTH = 256;
  139. typedef std::string QualifiedTypeName;
  140. // Every type has an ID. Those of the primitive types are pre-defined.
  141. typedef octet PrimitiveTypeId;
  142. // First 4 bytes of MD5 of of a member name converted to bytes
  143. // using UTF-8 encoding and without a 'nul' terminator.
  144. // Example: the member name "color" has NameHash {0x70, 0xDD, 0xA5, 0xDF}
  145. typedef std::array<uint8_t, 4> NameHash;
  146. // Mask used to remove the flags that do no affect assignability
  147. // Selects T1, T2, O, M, K, D
  148. const uint16_t MemberFlagMinimalMask = 0x003f;
  149. /*!
  150. * @brief This class represents the enumeration ReturnCode_t.
  151. */
  152. /*
  153. enum ReturnCode_t : uint32_t
  154. {
  155. RETCODE_OK = 0,
  156. RETCODE_ERROR = 1,
  157. RETCODE_UNSUPPORTED = 2,
  158. RETCODE_BAD_PARAMETER = 3,
  159. RETCODE_PRECONDITION_NOT_MET = 4,
  160. RETCODE_OUT_OF_RESOURCES = 5,
  161. RETCODE_NOT_ENABLED = 6,
  162. RETCODE_IMMUTABLE_POLICY = 7,
  163. RETCODE_INCONSISTENT_POLICY = 8,
  164. RETCODE_ALREADY_DELETED = 9,
  165. RETCODE_TIMEOUT = 10,
  166. RETCODE_NO_DATA = 11,
  167. RETCODE_ILLEGAL_OPERATION = 12
  168. };
  169. */
  170. class ReturnCode_t;
  171. class RTPS_DllAPI ReturnCode_t
  172. {
  173. public:
  174. ReturnCode_t(
  175. uint32_t value)
  176. {
  177. value_ = value;
  178. }
  179. ReturnCode_t(
  180. const ReturnCode_t& code)
  181. {
  182. value_ = code.value_;
  183. }
  184. explicit operator bool() = delete;
  185. bool operator !() const
  186. {
  187. return value_ != ReturnCode_t::RETCODE_OK.value_;
  188. }
  189. ReturnCode_t& operator =(
  190. const ReturnCode_t& c)
  191. {
  192. value_ = c.value_;
  193. return *this;
  194. }
  195. bool operator ==(
  196. const ReturnCode_t& c) const
  197. {
  198. return value_ == c.value_;
  199. }
  200. bool operator !=(
  201. const ReturnCode_t& c) const
  202. {
  203. return value_ != c.value_;
  204. }
  205. uint32_t operator ()() const
  206. {
  207. return value_;
  208. }
  209. static const ReturnCode_t RETCODE_OK;
  210. static const ReturnCode_t RETCODE_ERROR;
  211. static const ReturnCode_t RETCODE_UNSUPPORTED;
  212. static const ReturnCode_t RETCODE_BAD_PARAMETER;
  213. static const ReturnCode_t RETCODE_PRECONDITION_NOT_MET;
  214. static const ReturnCode_t RETCODE_OUT_OF_RESOURCES;
  215. static const ReturnCode_t RETCODE_NOT_ENABLED;
  216. static const ReturnCode_t RETCODE_IMMUTABLE_POLICY;
  217. static const ReturnCode_t RETCODE_INCONSISTENT_POLICY;
  218. static const ReturnCode_t RETCODE_ALREADY_DELETED;
  219. static const ReturnCode_t RETCODE_TIMEOUT;
  220. static const ReturnCode_t RETCODE_NO_DATA;
  221. static const ReturnCode_t RETCODE_ILLEGAL_OPERATION;
  222. private:
  223. uint32_t value_ = ReturnCode_t::RETCODE_OK.value_;
  224. };
  225. // TODO Remove this alias when Fast-RTPS reaches version 2
  226. using ResponseCode = ReturnCode_t;
  227. typedef uint32_t MemberId;
  228. #define MEMBER_ID_INVALID 0X0FFFFFFF
  229. #define INDEX_INVALID UINT32_MAX
  230. const int32_t MAX_BITMASK_LENGTH = 64;
  231. const int32_t MAX_ELEMENTS_COUNT = 100;
  232. const int32_t MAX_STRING_LENGTH = 255;
  233. // Long Bound of a collection type
  234. typedef uint32_t LBound;
  235. typedef std::vector<LBound> LBoundSeq;
  236. const LBound INVALID_LBOUND = 0;
  237. // Short Bound of a collection type
  238. typedef octet SBound;
  239. typedef std::vector<SBound> SBoundSeq;
  240. const SBound INVALID_SBOUND = 0;
  241. // Auxiliar function to compare sequences (std::vector)
  242. template<class T>
  243. bool compareSequence(
  244. const std::vector<T>& a,
  245. const std::vector<T>& b)
  246. {
  247. if (a.size() == b.size())
  248. {
  249. auto aIt = a.begin();
  250. auto bIt = b.begin();
  251. while (aIt != a.end() && bIt != b.end())
  252. {
  253. if (*aIt == *bIt)
  254. {
  255. ++aIt;
  256. ++bIt;
  257. }
  258. else
  259. {
  260. return false;
  261. }
  262. }
  263. return true;
  264. }
  265. return false;
  266. }
  267. // Flags that apply to struct/union/collection/enum/bitmask/bitset
  268. // members/elements and DO affect type assignability
  269. // Depending on the flag it may not apply to members of all types
  270. // When not all, the applicable member types are listed
  271. class MemberFlag
  272. {
  273. private:
  274. std::bitset<16> m_MemberFlag;
  275. public:
  276. MemberFlag()
  277. {
  278. }
  279. MemberFlag(
  280. const MemberFlag& x)
  281. : m_MemberFlag(x.m_MemberFlag)
  282. {
  283. }
  284. MemberFlag(
  285. MemberFlag&& x)
  286. : m_MemberFlag(std::move(x.m_MemberFlag))
  287. {
  288. }
  289. MemberFlag& operator =(
  290. const MemberFlag& x)
  291. {
  292. m_MemberFlag = x.m_MemberFlag;
  293. return *this;
  294. }
  295. MemberFlag& operator =(
  296. MemberFlag&& x)
  297. {
  298. m_MemberFlag = std::move(x.m_MemberFlag);
  299. return *this;
  300. }
  301. // T1 | 00 = INVALID, 01 = DISCARD
  302. bool TRY_CONSTRUCT1() const
  303. {
  304. return m_MemberFlag.test(0);
  305. }
  306. void TRY_CONSTRUCT1(
  307. bool b)
  308. {
  309. b ? m_MemberFlag.set(0) : m_MemberFlag.reset(0);
  310. }
  311. // T2 | 10 = USE_DEFAULT, 11 = TRIM
  312. bool TRY_CONSTRUCT2() const
  313. {
  314. return m_MemberFlag.test(1);
  315. }
  316. void TRY_CONSTRUCT2(
  317. bool b)
  318. {
  319. b ? m_MemberFlag.set(1) : m_MemberFlag.reset(1);
  320. }
  321. // X StructMember, UnionMember,
  322. // CollectionElement
  323. bool IS_EXTERNAL() const
  324. {
  325. return m_MemberFlag.test(2);
  326. }
  327. void IS_EXTERNAL(
  328. bool b)
  329. {
  330. b ? m_MemberFlag.set(2) : m_MemberFlag.reset(2);
  331. }
  332. // O StructMember
  333. bool IS_OPTIONAL() const
  334. {
  335. return m_MemberFlag.test(3);
  336. }
  337. void IS_OPTIONAL(
  338. bool b)
  339. {
  340. b ? m_MemberFlag.set(3) : m_MemberFlag.reset(3);
  341. }
  342. // M StructMember
  343. bool IS_MUST_UNDERSTAND() const
  344. {
  345. return m_MemberFlag.test(4);
  346. }
  347. void IS_MUST_UNDERSTAND(
  348. bool b)
  349. {
  350. b ? m_MemberFlag.set(4) : m_MemberFlag.reset(4);
  351. }
  352. // K StructMember, UnionDiscriminator
  353. bool IS_KEY() const
  354. {
  355. return m_MemberFlag.test(5);
  356. }
  357. void IS_KEY(
  358. bool b)
  359. {
  360. b ? m_MemberFlag.set(5) : m_MemberFlag.reset(5);
  361. }
  362. // D UnionMember, EnumerationLiteral
  363. bool IS_DEFAULT() const
  364. {
  365. return m_MemberFlag.test(6);
  366. }
  367. void IS_DEFAULT(
  368. bool b)
  369. {
  370. b ? m_MemberFlag.set(6) : m_MemberFlag.reset(6);
  371. }
  372. void serialize(
  373. eprosima::fastcdr::Cdr& cdr) const;
  374. void deserialize(
  375. eprosima::fastcdr::Cdr& cdr);
  376. static size_t getCdrSerializedSize(
  377. const MemberFlag&,
  378. size_t current_alignment = 0);
  379. bool operator ==(
  380. const MemberFlag& other) const
  381. {
  382. return m_MemberFlag == other.m_MemberFlag;
  383. }
  384. };
  385. typedef MemberFlag CollectionElementFlag; // T1, T2, X
  386. typedef MemberFlag StructMemberFlag; // T1, T2, O, M, K, X
  387. typedef MemberFlag UnionMemberFlag; // T1, T2, D, X
  388. typedef MemberFlag UnionDiscriminatorFlag; // T1, T2, K
  389. typedef MemberFlag EnumeratedLiteralFlag; // D
  390. typedef MemberFlag AnnotationParameterFlag; // Unused. No flags apply
  391. typedef MemberFlag AliasMemberFlag; // Unused. No flags apply
  392. typedef MemberFlag BitflagFlag; // Unused. No flags apply
  393. typedef MemberFlag BitsetMemberFlag; // Unused. No flags apply
  394. // Flags that apply to type declarationa and DO affect assignability
  395. // Depending on the flag it may not apply to all types
  396. // When not all, the applicable types are listed
  397. class TypeFlag
  398. {
  399. private:
  400. std::bitset<16> m_TypeFlag;
  401. public:
  402. TypeFlag()
  403. {
  404. }
  405. TypeFlag(
  406. const TypeFlag& x)
  407. : m_TypeFlag(x.m_TypeFlag)
  408. {
  409. }
  410. TypeFlag(
  411. TypeFlag&& x)
  412. : m_TypeFlag(std::move(x.m_TypeFlag))
  413. {
  414. }
  415. TypeFlag& operator =(
  416. const TypeFlag& x)
  417. {
  418. m_TypeFlag = x.m_TypeFlag;
  419. return *this;
  420. }
  421. TypeFlag& operator =(
  422. TypeFlag&& x)
  423. {
  424. m_TypeFlag = std::move(x.m_TypeFlag);
  425. return *this;
  426. }
  427. // F |
  428. bool IS_FINAL() const
  429. {
  430. return m_TypeFlag.test(0);
  431. }
  432. void IS_FINAL(
  433. bool b)
  434. {
  435. b ? m_TypeFlag.set(0) : m_TypeFlag.reset(0);
  436. }
  437. // A |- Struct, Union
  438. bool IS_APPENDABLE() const
  439. {
  440. return m_TypeFlag.test(1);
  441. }
  442. void IS_APPENDABLE(
  443. bool b)
  444. {
  445. b ? m_TypeFlag.set(1) : m_TypeFlag.reset(1);
  446. }
  447. // M | (exactly one flag)
  448. bool IS_MUTABLE() const
  449. {
  450. return m_TypeFlag.test(2);
  451. }
  452. void IS_MUTABLE(
  453. bool b)
  454. {
  455. b ? m_TypeFlag.set(2) : m_TypeFlag.reset(2);
  456. }
  457. // N Struct, Union
  458. bool IS_NESTED() const
  459. {
  460. return m_TypeFlag.test(3);
  461. }
  462. void IS_NESTED(
  463. bool b)
  464. {
  465. b ? m_TypeFlag.set(3) : m_TypeFlag.reset(3);
  466. }
  467. // H Struct
  468. bool IS_AUTOID_HASH() const
  469. {
  470. return m_TypeFlag.test(4);
  471. }
  472. void IS_AUTOID_HASH(
  473. bool b)
  474. {
  475. b ? m_TypeFlag.set(4) : m_TypeFlag.reset(4);
  476. }
  477. void serialize(
  478. eprosima::fastcdr::Cdr& cdr) const;
  479. void deserialize(
  480. eprosima::fastcdr::Cdr& cdr);
  481. static size_t getCdrSerializedSize(
  482. const TypeFlag&,
  483. size_t current_alignment = 0);
  484. bool operator ==(
  485. const TypeFlag& other) const
  486. {
  487. return m_TypeFlag == other.m_TypeFlag;
  488. }
  489. };
  490. typedef TypeFlag StructTypeFlag; // All flags apply
  491. typedef TypeFlag UnionTypeFlag; // All flags apply
  492. typedef TypeFlag CollectionTypeFlag; // Unused. No flags apply
  493. typedef TypeFlag AnnotationTypeFlag; // Unused. No flags apply
  494. typedef TypeFlag AliasTypeFlag; // Unused. No flags apply
  495. typedef TypeFlag EnumTypeFlag; // Unused. No flags apply
  496. typedef TypeFlag BitmaskTypeFlag; // Unused. No flags apply
  497. typedef TypeFlag BitsetTypeFlag; // Unused. No flags apply
  498. // Mask used to remove the flags that do no affect assignability
  499. const uint16_t TypeFlagMinimalMask = 0x0007; // Selects M, A, F
  500. // --- Annotation usage: ----------------------------------------------
  501. // ID of a type member
  502. const uint32_t ANNOTATION_STR_VALUE_MAX_LEN = 128;
  503. const uint32_t ANNOTATION_OCTETSEC_VALUE_MAX_LEN = 128;
  504. } // namespace types
  505. } // namespace fastrtps
  506. } // namespace eprosima
  507. #endif // TYPES_BASE_H