types.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. // Copyright 2014 MongoDB Inc.
  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. #pragma once
  15. #include <chrono>
  16. #include <cstring>
  17. #include <bsoncxx/array/view.hpp>
  18. #include <bsoncxx/decimal128.hpp>
  19. #include <bsoncxx/document/view.hpp>
  20. #include <bsoncxx/oid.hpp>
  21. #include <bsoncxx/stdx/string_view.hpp>
  22. #include <bsoncxx/config/prelude.hpp>
  23. namespace bsoncxx {
  24. BSONCXX_INLINE_NAMESPACE_BEGIN
  25. ///
  26. /// An enumeration of each BSON type.
  27. /// These x-macros will expand to be of the form:
  28. /// k_double = 0x01,
  29. /// k_utf8 = 0x02,
  30. /// k_document = 0x03,
  31. /// k_array = 0x04 ...
  32. ///
  33. enum class type : std::uint8_t {
  34. #define BSONCXX_ENUM(name, val) k_##name = val,
  35. #include <bsoncxx/enums/type.hpp>
  36. #undef BSONCXX_ENUM
  37. };
  38. ///
  39. /// An enumeration of each BSON binary sub type.
  40. /// These x-macros will expand to be of the form:
  41. /// k_binary = 0x00,
  42. /// k_function = 0x01,
  43. /// k_binary_deprecated = 0x02,
  44. /// k_uuid_deprecated = 0x03,
  45. /// k_uuid = 0x04,
  46. /// k_md5 = 0x05,
  47. /// k_encrypted = 0x06,
  48. /// k_user = 0x80
  49. ///
  50. enum class binary_sub_type : std::uint8_t {
  51. #define BSONCXX_ENUM(name, val) k_##name = val,
  52. #include <bsoncxx/enums/binary_sub_type.hpp>
  53. #undef BSONCXX_ENUM
  54. };
  55. ///
  56. /// Returns a stringification of the given type.
  57. ///
  58. /// @param rhs
  59. /// The type to stringify.
  60. ///
  61. /// @return a std::string representation of the type.
  62. ///
  63. BSONCXX_API std::string BSONCXX_CALL to_string(type rhs);
  64. ///
  65. /// Returns a stringification of the given binary sub type.
  66. ///
  67. /// @param rhs
  68. /// The type to stringify.
  69. ///
  70. /// @return a std::string representation of the type.
  71. ///
  72. BSONCXX_API std::string BSONCXX_CALL to_string(binary_sub_type rhs);
  73. namespace types {
  74. ///
  75. /// A BSON double value.
  76. ///
  77. struct BSONCXX_API b_double {
  78. static constexpr auto type_id = type::k_double;
  79. double value;
  80. ///
  81. /// Conversion operator unwrapping a double
  82. ///
  83. BSONCXX_INLINE operator double() const {
  84. return value;
  85. }
  86. };
  87. ///
  88. /// free function comparator for b_double
  89. ///
  90. /// @relatesalso b_double
  91. ///
  92. BSONCXX_INLINE bool operator==(const b_double& lhs, const b_double& rhs) {
  93. return lhs.value == rhs.value;
  94. }
  95. ///
  96. /// A BSON UTF-8 encoded string value.
  97. ///
  98. struct BSONCXX_API b_utf8 {
  99. static constexpr auto type_id = type::k_utf8;
  100. ///
  101. /// Constructor for b_utf8.
  102. ///
  103. /// @param value
  104. /// The value to wrap.
  105. ///
  106. template <typename T,
  107. typename std::enable_if<!std::is_same<b_utf8, typename std::decay<T>::type>::value,
  108. int>::type = 0>
  109. BSONCXX_INLINE explicit b_utf8(T&& t) : value(std::forward<T>(t)) {}
  110. stdx::string_view value;
  111. ///
  112. /// Conversion operator unwrapping a string_view
  113. ///
  114. BSONCXX_INLINE operator stdx::string_view() const {
  115. return value;
  116. }
  117. };
  118. ///
  119. /// free function comparator for b_utf8
  120. ///
  121. /// @relatesalso b_utf8
  122. ///
  123. BSONCXX_INLINE bool operator==(const b_utf8& lhs, const b_utf8& rhs) {
  124. return lhs.value == rhs.value;
  125. }
  126. ///
  127. /// A BSON document value.
  128. ///
  129. struct BSONCXX_API b_document {
  130. static constexpr auto type_id = type::k_document;
  131. document::view value;
  132. ///
  133. /// Conversion operator unwrapping a document::view
  134. ///
  135. BSONCXX_INLINE operator document::view() const {
  136. return value;
  137. }
  138. ///
  139. /// Returns an unwrapped document::view
  140. ///
  141. BSONCXX_INLINE document::view view() {
  142. return value;
  143. }
  144. };
  145. ///
  146. /// free function comparator for b_document
  147. ///
  148. /// @relatesalso b_document
  149. ///
  150. BSONCXX_INLINE bool operator==(const b_document& lhs, const b_document& rhs) {
  151. return lhs.value == rhs.value;
  152. }
  153. ///
  154. /// A BSON array value.
  155. ///
  156. struct BSONCXX_API b_array {
  157. static constexpr auto type_id = type::k_array;
  158. array::view value;
  159. ///
  160. /// Conversion operator unwrapping an array::view
  161. ///
  162. BSONCXX_INLINE operator array::view() const {
  163. return value;
  164. }
  165. };
  166. ///
  167. /// free function comparator for b_array
  168. ///
  169. /// @relatesalso b_array
  170. ///
  171. BSONCXX_INLINE bool operator==(const b_array& lhs, const b_array& rhs) {
  172. return lhs.value == rhs.value;
  173. }
  174. ///
  175. /// A BSON binary data value.
  176. ///
  177. struct BSONCXX_API b_binary {
  178. static constexpr auto type_id = type::k_binary;
  179. binary_sub_type sub_type;
  180. uint32_t size;
  181. const uint8_t* bytes;
  182. };
  183. ///
  184. /// free function comparator for b_binary
  185. ///
  186. /// @relatesalso b_binary
  187. ///
  188. BSONCXX_INLINE bool operator==(const b_binary& lhs, const b_binary& rhs) {
  189. return lhs.sub_type == rhs.sub_type && lhs.size == rhs.size &&
  190. (std::memcmp(lhs.bytes, rhs.bytes, lhs.size) == 0);
  191. }
  192. ///
  193. /// A BSON undefined value.
  194. ///
  195. /// @deprecated
  196. /// This BSON type is deprecated and use by clients is discouraged.
  197. ///
  198. struct BSONCXX_API b_undefined {
  199. static constexpr auto type_id = type::k_undefined;
  200. };
  201. ///
  202. /// free function comparator for b_undefined
  203. ///
  204. /// @relatesalso b_undefined
  205. ///
  206. BSONCXX_INLINE bool operator==(const b_undefined&, const b_undefined&) {
  207. return true;
  208. }
  209. ///
  210. /// A BSON ObjectId value.
  211. ///
  212. struct BSONCXX_API b_oid {
  213. static constexpr auto type_id = type::k_oid;
  214. oid value;
  215. };
  216. ///
  217. /// free function comparator for b_oid
  218. ///
  219. /// @relatesalso b_oid
  220. ///
  221. BSONCXX_INLINE bool operator==(const b_oid& lhs, const b_oid& rhs) {
  222. return lhs.value == rhs.value;
  223. }
  224. ///
  225. /// A BSON boolean value.
  226. ///
  227. struct BSONCXX_API b_bool {
  228. static constexpr auto type_id = type::k_bool;
  229. bool value;
  230. ///
  231. /// Conversion operator unwrapping a bool
  232. ///
  233. BSONCXX_INLINE operator bool() const {
  234. return value;
  235. }
  236. };
  237. ///
  238. /// free function comparator for b_bool
  239. ///
  240. /// @relatesalso b_bool
  241. ///
  242. BSONCXX_INLINE bool operator==(const b_bool& lhs, const b_bool& rhs) {
  243. return lhs.value == rhs.value;
  244. }
  245. ///
  246. /// A BSON date value.
  247. ///
  248. struct BSONCXX_API b_date {
  249. static constexpr auto type_id = type::k_date;
  250. ///
  251. /// Constructor for b_date
  252. ///
  253. /// @param value
  254. /// Milliseconds since the system_clock epoch.
  255. ///
  256. BSONCXX_INLINE
  257. explicit b_date(std::chrono::milliseconds value) : value(value) {}
  258. ///
  259. /// Constructor for b_date
  260. ///
  261. /// @param value
  262. /// A system_clock time_point.
  263. ///
  264. BSONCXX_INLINE
  265. explicit b_date(const std::chrono::system_clock::time_point& tp)
  266. : value(std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch())) {}
  267. std::chrono::milliseconds value;
  268. ///
  269. /// Conversion operator unwrapping a int64_t
  270. ///
  271. BSONCXX_INLINE operator int64_t() const {
  272. return value.count();
  273. }
  274. ///
  275. /// Manually convert this b_date to an int64_t
  276. ///
  277. BSONCXX_INLINE int64_t to_int64() const {
  278. return value.count();
  279. }
  280. ///
  281. /// Conversion operator unwrapping a time_point
  282. ///
  283. BSONCXX_INLINE operator std::chrono::system_clock::time_point() const {
  284. return std::chrono::system_clock::time_point(
  285. std::chrono::duration_cast<std::chrono::system_clock::duration>(value));
  286. }
  287. };
  288. ///
  289. /// free function comparator for b_date
  290. ///
  291. /// @relatesalso b_date
  292. ///
  293. BSONCXX_INLINE bool operator==(const b_date& lhs, const b_date& rhs) {
  294. return lhs.value == rhs.value;
  295. }
  296. ///
  297. /// A BSON null value.
  298. ///
  299. struct BSONCXX_API b_null {
  300. static constexpr auto type_id = type::k_null;
  301. };
  302. ///
  303. /// free function comparator for b_null
  304. ///
  305. /// @relatesalso b_null
  306. ///
  307. BSONCXX_INLINE bool operator==(const b_null&, const b_null&) {
  308. return true;
  309. }
  310. ///
  311. /// A BSON regex value.
  312. ///
  313. struct BSONCXX_API b_regex {
  314. static constexpr auto type_id = type::k_regex;
  315. ///
  316. /// Constructor for b_regex
  317. ///
  318. /// @param regex
  319. /// The regex pattern
  320. ///
  321. /// @param options
  322. /// The regex options
  323. ///
  324. template <typename T,
  325. typename U = stdx::string_view,
  326. typename std::enable_if<!std::is_same<b_regex, typename std::decay<T>::type>::value,
  327. int>::type = 0>
  328. BSONCXX_INLINE explicit b_regex(T&& regex, U&& options = U{})
  329. : regex(std::forward<T>(regex)), options(std::forward<U>(options)) {}
  330. stdx::string_view regex;
  331. stdx::string_view options;
  332. };
  333. ///
  334. /// free function comparator for b_regex
  335. ///
  336. /// @relatesalso b_regex
  337. ///
  338. BSONCXX_INLINE bool operator==(const b_regex& lhs, const b_regex& rhs) {
  339. return lhs.regex == rhs.regex && lhs.options == rhs.options;
  340. }
  341. ///
  342. /// A BSON DBPointer value.
  343. ///
  344. /// @deprecated
  345. /// A BSON DBPointer (aka DBRef) is still supported but deprecated.
  346. ///
  347. struct BSONCXX_API b_dbpointer {
  348. static constexpr auto type_id = type::k_dbpointer;
  349. stdx::string_view collection;
  350. oid value;
  351. };
  352. ///
  353. /// free function comparator for b_dbpointer
  354. ///
  355. /// @relatesalso b_dbpointer
  356. ///
  357. BSONCXX_INLINE bool operator==(const b_dbpointer& lhs, const b_dbpointer& rhs) {
  358. return lhs.collection == rhs.collection && lhs.value == rhs.value;
  359. }
  360. ///
  361. /// A BSON JavaScript code value.
  362. ///
  363. struct BSONCXX_API b_code {
  364. static constexpr auto type_id = type::k_code;
  365. ///
  366. /// Constructor for b_code.
  367. ///
  368. /// @param code
  369. /// The js code
  370. ///
  371. template <typename T,
  372. typename std::enable_if<!std::is_same<b_code, typename std::decay<T>::type>::value,
  373. int>::type = 0>
  374. BSONCXX_INLINE explicit b_code(T&& t) : code(std::forward<T>(t)) {}
  375. stdx::string_view code;
  376. ///
  377. /// Conversion operator unwrapping a string_view
  378. ///
  379. BSONCXX_INLINE operator stdx::string_view() const {
  380. return code;
  381. }
  382. };
  383. ///
  384. /// free function comparator for b_code
  385. ///
  386. /// @relatesalso b_code
  387. ///
  388. BSONCXX_INLINE bool operator==(const b_code& lhs, const b_code& rhs) {
  389. return lhs.code == rhs.code;
  390. }
  391. ///
  392. /// A BSON Symbol value.
  393. ///
  394. /// @deprecated
  395. /// This BSON type is deprecated and use by clients is discouraged.
  396. ///
  397. struct BSONCXX_API b_symbol {
  398. static constexpr auto type_id = type::k_symbol;
  399. ///
  400. /// Constructor for b_symbol.
  401. ///
  402. /// @param symbol
  403. /// The symbol.
  404. ///
  405. template <typename T,
  406. typename std::enable_if<!std::is_same<b_symbol, typename std::decay<T>::type>::value,
  407. int>::type = 0>
  408. BSONCXX_INLINE explicit b_symbol(T&& t) : symbol(std::forward<T>(t)) {}
  409. stdx::string_view symbol;
  410. ///
  411. /// Conversion operator unwrapping a string_view
  412. ///
  413. BSONCXX_INLINE operator stdx::string_view() const {
  414. return symbol;
  415. }
  416. };
  417. ///
  418. /// free function comparator for b_symbol
  419. ///
  420. /// @relatesalso b_symbol
  421. ///
  422. BSONCXX_INLINE bool operator==(const b_symbol& lhs, const b_symbol& rhs) {
  423. return lhs.symbol == rhs.symbol;
  424. }
  425. ///
  426. /// A BSON JavaScript code with scope value.
  427. ///
  428. struct BSONCXX_API b_codewscope {
  429. static constexpr auto type_id = type::k_codewscope;
  430. ///
  431. /// Constructor for b_codewscope.
  432. ///
  433. /// @param code
  434. /// The js code
  435. ///
  436. /// @param scope
  437. /// A bson document view holding the scope environment.
  438. ///
  439. template <
  440. typename T,
  441. typename U,
  442. typename std::enable_if<!std::is_same<b_codewscope, typename std::decay<T>::type>::value,
  443. int>::type = 0>
  444. BSONCXX_INLINE explicit b_codewscope(T&& code, U&& scope)
  445. : code(std::forward<T>(code)), scope(std::forward<U>(scope)) {}
  446. stdx::string_view code;
  447. document::view scope;
  448. };
  449. ///
  450. /// free function comparator for b_codewscope
  451. ///
  452. /// @relatesalso b_codewscope
  453. ///
  454. BSONCXX_INLINE bool operator==(const b_codewscope& lhs, const b_codewscope& rhs) {
  455. return lhs.code == rhs.code && lhs.scope == rhs.scope;
  456. }
  457. ///
  458. /// A BSON signed 32-bit integer value.
  459. ///
  460. struct BSONCXX_API b_int32 {
  461. static constexpr auto type_id = type::k_int32;
  462. int32_t value;
  463. ///
  464. /// Conversion operator unwrapping a int32_t
  465. ///
  466. BSONCXX_INLINE operator int32_t() const {
  467. return value;
  468. }
  469. };
  470. ///
  471. /// free function comparator for b_int32
  472. ///
  473. /// @relatesalso b_int32
  474. ///
  475. BSONCXX_INLINE bool operator==(const b_int32& lhs, const b_int32& rhs) {
  476. return lhs.value == rhs.value;
  477. }
  478. ///
  479. /// A BSON replication timestamp value.
  480. ///
  481. /// @warning
  482. /// This BSON type is used internally by the MongoDB server - use by clients
  483. /// is discouraged.
  484. ///
  485. struct BSONCXX_API b_timestamp {
  486. static constexpr auto type_id = type::k_timestamp;
  487. uint32_t increment;
  488. uint32_t timestamp;
  489. };
  490. ///
  491. /// free function comparator for b_timestamp
  492. ///
  493. /// @relatesalso b_timestamp
  494. ///
  495. BSONCXX_INLINE bool operator==(const b_timestamp& lhs, const b_timestamp& rhs) {
  496. return lhs.increment == rhs.increment && lhs.timestamp == rhs.timestamp;
  497. }
  498. ///
  499. /// A BSON 64-bit signed integer value.
  500. ///
  501. struct BSONCXX_API b_int64 {
  502. static constexpr auto type_id = type::k_int64;
  503. int64_t value;
  504. ///
  505. /// Conversion operator unwrapping a int64_t
  506. ///
  507. BSONCXX_INLINE operator int64_t() const {
  508. return value;
  509. }
  510. };
  511. ///
  512. /// free function comparator for b_int64
  513. ///
  514. /// @relatesalso b_int64
  515. ///
  516. BSONCXX_INLINE bool operator==(const b_int64& lhs, const b_int64& rhs) {
  517. return lhs.value == rhs.value;
  518. }
  519. ///
  520. /// A BSON Decimal128 value.
  521. ///
  522. struct BSONCXX_API b_decimal128 {
  523. static constexpr auto type_id = type::k_decimal128;
  524. decimal128 value;
  525. ///
  526. /// Constructor for b_decimal128.
  527. ///
  528. /// @param value
  529. /// The value to wrap.
  530. ///
  531. template <
  532. typename T,
  533. typename std::enable_if<!std::is_same<b_decimal128, typename std::decay<T>::type>::value,
  534. int>::type = 0>
  535. BSONCXX_INLINE explicit b_decimal128(T&& t) : value(std::forward<T>(t)) {}
  536. };
  537. ///
  538. /// free function comparator for b_decimal128
  539. ///
  540. /// @relatesalso b_decimal128
  541. ///
  542. BSONCXX_INLINE bool operator==(const b_decimal128& lhs, const b_decimal128& rhs) {
  543. return lhs.value == rhs.value;
  544. }
  545. ///
  546. /// A BSON min-key value.
  547. ///
  548. struct BSONCXX_API b_minkey {
  549. static constexpr auto type_id = type::k_minkey;
  550. };
  551. ///
  552. /// free function comparator for b_minkey
  553. ///
  554. /// @relatesalso b_minkey
  555. ///
  556. BSONCXX_INLINE bool operator==(const b_minkey&, const b_minkey&) {
  557. return true;
  558. }
  559. ///
  560. /// A BSON max-key value.
  561. ///
  562. struct BSONCXX_API b_maxkey {
  563. static constexpr auto type_id = type::k_maxkey;
  564. };
  565. ///
  566. /// free function comparator for b_maxkey
  567. ///
  568. /// @relatesalso b_maxkey
  569. ///
  570. BSONCXX_INLINE bool operator==(const b_maxkey&, const b_maxkey&) {
  571. return true;
  572. }
  573. #define BSONCXX_ENUM(name, val) \
  574. BSONCXX_INLINE bool operator!=(const b_##name& lhs, const b_##name& rhs) { \
  575. return !(lhs == rhs); \
  576. }
  577. #include <bsoncxx/enums/type.hpp>
  578. #undef BSONCXX_ENUM
  579. } // namespace types
  580. BSONCXX_INLINE_NAMESPACE_END
  581. } // namespace bsoncxx
  582. #include <bsoncxx/config/postlude.hpp>