element.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 <cstddef>
  16. #include <cstdint>
  17. #include <bsoncxx/stdx/string_view.hpp>
  18. #include <bsoncxx/config/prelude.hpp>
  19. namespace bsoncxx {
  20. BSONCXX_INLINE_NAMESPACE_BEGIN
  21. enum class type : std::uint8_t;
  22. enum class binary_sub_type : std::uint8_t;
  23. namespace types {
  24. struct b_eod;
  25. struct b_double;
  26. struct b_utf8;
  27. struct b_document;
  28. struct b_array;
  29. struct b_binary;
  30. struct b_undefined;
  31. struct b_oid;
  32. struct b_bool;
  33. struct b_date;
  34. struct b_null;
  35. struct b_regex;
  36. struct b_dbpointer;
  37. struct b_code;
  38. struct b_symbol;
  39. struct b_codewscope;
  40. struct b_int32;
  41. struct b_timestamp;
  42. struct b_int64;
  43. struct b_decimal128;
  44. struct b_minkey;
  45. struct b_maxkey;
  46. namespace bson_value {
  47. class value;
  48. class view;
  49. } // namespace bson_value
  50. } // namespace types
  51. namespace array {
  52. class element;
  53. } // namespace array
  54. namespace document {
  55. ///
  56. /// A variant view type that accesses values in serialized BSON documents.
  57. ///
  58. /// Element functions as a variant type, where the kind of the element can be
  59. /// interrogated by calling type(), the key can be extracted by calling key() and
  60. /// a specific value can be extracted through get_X() accessors.
  61. ///
  62. /// @relatesalso array::element
  63. ///
  64. class BSONCXX_API element {
  65. public:
  66. ///
  67. /// Construct an invalid element.
  68. ///
  69. /// This is useful when mapping the end iterator of a document or array
  70. /// view.
  71. ///
  72. element();
  73. ///
  74. /// Conversion operator to bool which is true for valid elements
  75. /// and false for invalid elements.
  76. ///
  77. explicit operator bool() const;
  78. ///
  79. /// Getter for the raw bson bytes the element points to.
  80. ///
  81. /// @return a pointer to the raw bson bytes.
  82. ///
  83. const std::uint8_t* raw() const;
  84. ///
  85. /// Getter for length of the raw bson bytes the element points to.
  86. ///
  87. /// @return a pointer to the length of the raw bson bytes.
  88. ///
  89. std::uint32_t length() const;
  90. ///
  91. /// Getter for the offset into the raw bson bytes the element points to.
  92. ///
  93. /// @return the offset into the raw bson bytes.
  94. ///
  95. std::uint32_t offset() const;
  96. ///
  97. /// Getter for the type of the element.
  98. ///
  99. /// @return the element's type.
  100. ///
  101. /// @throws bsoncxx::exception if this element is invalid.
  102. ///
  103. bsoncxx::type type() const;
  104. ///
  105. /// Getter for the element's key.
  106. ///
  107. /// @return the element's key.
  108. ///
  109. /// @throws bsoncxx::exception if this element is invalid.
  110. ///
  111. stdx::string_view key() const;
  112. ///
  113. /// Getter for the element's key length.
  114. ///
  115. /// @return the element's key length.
  116. ///
  117. std::uint32_t keylen() const;
  118. ///
  119. /// Getter for elements of the b_double type.
  120. ///
  121. /// @throws bsoncxx::exception if this element is not a b_double.
  122. ///
  123. /// @return the element's value.
  124. ///
  125. types::b_double get_double() const;
  126. ///
  127. /// Getter for elements of the b_utf8 type.
  128. ///
  129. /// @throws bsoncxx::exception if this element is not a b_utf8.
  130. ///
  131. /// @return the element's value.
  132. ///
  133. types::b_utf8 get_utf8() const;
  134. ///
  135. /// Getter for elements of the b_document type.
  136. ///
  137. /// @throws bsoncxx::exception if this element is not a b_document.
  138. ///
  139. /// @return the element's value.
  140. ///
  141. types::b_document get_document() const;
  142. ///
  143. /// Getter for elements of the b_array type.
  144. ///
  145. /// @throws bsoncxx::exception if this element is not a b_array.
  146. ///
  147. /// @return the element's value.
  148. ///
  149. types::b_array get_array() const;
  150. ///
  151. /// Getter for elements of the b_binary type.
  152. ///
  153. /// @throws bsoncxx::exception if this element is not a b_binary.
  154. ///
  155. /// @return the element's value.
  156. ///
  157. types::b_binary get_binary() const;
  158. ///
  159. /// Getter for elements of the b_undefined type.
  160. ///
  161. /// @throws bsoncxx::exception if this element is not a b_undefined.
  162. ///
  163. /// @return the element's value.
  164. ///
  165. types::b_undefined get_undefined() const;
  166. ///
  167. /// Getter for elements of the b_oid type.
  168. ///
  169. /// @throws bsoncxx::exception if this element is not a b_oid.
  170. ///
  171. /// @return the element's value.
  172. ///
  173. types::b_oid get_oid() const;
  174. ///
  175. /// Getter for elements of the b_bool type.
  176. ///
  177. /// @throws bsoncxx::exception if this element is not a b_bool.
  178. ///
  179. /// @return the element's value.
  180. ///
  181. types::b_bool get_bool() const;
  182. ///
  183. /// Getter for elements of the b_date type.
  184. ///
  185. /// @throws bsoncxx::exception if this element is not a b_date.
  186. ///
  187. /// @return the element's value.
  188. ///
  189. types::b_date get_date() const;
  190. ///
  191. /// Getter for elements of the b_null type.
  192. ///
  193. /// @throws bsoncxx::exception if this element is not a b_null.
  194. ///
  195. /// @return the element's value.
  196. ///
  197. types::b_null get_null() const;
  198. ///
  199. /// Getter for elements of the b_regex type.
  200. ///
  201. /// @throws bsoncxx::exception if this element is not a b_regex.
  202. ///
  203. /// @return the element's value.
  204. ///
  205. types::b_regex get_regex() const;
  206. ///
  207. /// Getter for elements of the b_dbpointer type.
  208. ///
  209. /// @throws bsoncxx::exception if this element is not a b_dbpointer.
  210. ///
  211. /// @return the element's value.
  212. ///
  213. types::b_dbpointer get_dbpointer() const;
  214. ///
  215. /// Getter for elements of the b_code type.
  216. ///
  217. /// @throws bsoncxx::exception if this element is not a b_code.
  218. ///
  219. /// @return the element's value.
  220. ///
  221. types::b_code get_code() const;
  222. ///
  223. /// Getter for elements of the b_symbol type.
  224. ///
  225. /// @throws bsoncxx::exception if this element is not a b_symbol.
  226. ///
  227. /// @return the element's value.
  228. ///
  229. types::b_symbol get_symbol() const;
  230. ///
  231. /// Getter for elements of the b_codewscope type.
  232. ///
  233. /// @throws bsoncxx::exception if this element is not a b_codewscope.
  234. ///
  235. /// @return the element's value.
  236. ///
  237. types::b_codewscope get_codewscope() const;
  238. ///
  239. /// Getter for elements of the b_int32 type.
  240. ///
  241. /// @throws bsoncxx::exception if this element is not a b_int32.
  242. ///
  243. /// @return the element's value.
  244. ///
  245. types::b_int32 get_int32() const;
  246. ///
  247. /// Getter for elements of the b_timestamp type.
  248. ///
  249. /// @throws bsoncxx::exception if this element is not a b_timestamp.
  250. ///
  251. /// @return the element's value.
  252. ///
  253. types::b_timestamp get_timestamp() const;
  254. ///
  255. /// Getter for elements of the b_int64 type.
  256. ///
  257. /// @throws bsoncxx::exception if this element is not a b_int64.
  258. ///
  259. /// @return the element's value.
  260. ///
  261. types::b_int64 get_int64() const;
  262. ///
  263. /// Getter for elements of the b_decimal128 type.
  264. ///
  265. /// @throws bsoncxx::exception if this element is not a b_decimal128.
  266. ///
  267. /// @return the element's value.
  268. ///
  269. types::b_decimal128 get_decimal128() const;
  270. ///
  271. /// Getter for elements of the b_minkey type.
  272. ///
  273. /// @throws bsoncxx::exception if this element is not a b_minkey.
  274. ///
  275. /// @return the element's value.
  276. ///
  277. types::b_minkey get_minkey() const;
  278. ///
  279. /// Getter for elements of the b_maxkey type.
  280. ///
  281. /// @throws bsoncxx::exception if this element is not a b_maxkey.
  282. ///
  283. /// @return the element's value.
  284. ///
  285. types::b_maxkey get_maxkey() const;
  286. ///
  287. /// Getter for a types::bson_value::view variant wrapper of the value portion of the
  288. /// element.
  289. ///
  290. /// @return the element's value.
  291. ///
  292. types::bson_value::view get_value() const;
  293. ///
  294. /// Getter for a types::bson_value::value variant wrapper of the value portion of
  295. /// the element. The returned object will make a copy of the buffer from this object.
  296. ///
  297. /// @return an owning version of the element's value.
  298. ///
  299. types::bson_value::value get_owning_value() const;
  300. ///
  301. /// If this element is a document, finds the first element of the document
  302. /// with the provided key. If there is no such element, an invalid
  303. /// document::element will be returned. The runtime of operator[] is
  304. /// linear in the length of the document.
  305. ///
  306. /// If this element is not a document, an invalid document::element will
  307. /// be returned.
  308. ///
  309. /// @param key
  310. /// The key to search for.
  311. ///
  312. /// @return The matching element, if found, or an invalid element.
  313. ///
  314. element operator[](stdx::string_view key) const;
  315. ///
  316. /// If this element is an array, indexes into this BSON array. If the
  317. /// index is out-of-bounds, an invalid array::element will be returned. As
  318. /// BSON represents arrays as documents, the runtime of operator[] is
  319. /// linear in the length of the array.
  320. ///
  321. /// If this element is not an array, an invalid array::element will
  322. /// be returned.
  323. ///
  324. /// @param i
  325. /// The index of the element.
  326. ///
  327. /// @return The element if it exists, or an invalid element.
  328. ///
  329. array::element operator[](std::uint32_t i) const;
  330. private:
  331. ///
  332. /// Construct an element as an offset into a buffer of bson bytes.
  333. ///
  334. /// @param raw
  335. /// A pointer to the raw bson bytes.
  336. ///
  337. /// @param length
  338. /// The size of the bson buffer.
  339. ///
  340. /// @param offset
  341. /// The element's offset into the buffer.
  342. ///
  343. BSONCXX_PRIVATE explicit element(const std::uint8_t* raw,
  344. std::uint32_t length,
  345. std::uint32_t offset,
  346. std::uint32_t keylen);
  347. friend class view;
  348. friend class array::element;
  349. const std::uint8_t* _raw;
  350. std::uint32_t _length;
  351. std::uint32_t _offset;
  352. std::uint32_t _keylen;
  353. };
  354. } // namespace document
  355. BSONCXX_INLINE_NAMESPACE_END
  356. } // namespace bsoncxx
  357. #include <bsoncxx/config/postlude.hpp>