field_view.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. //
  2. // Copyright (c) 2019-2023 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_MYSQL_FIELD_VIEW_HPP
  8. #define BOOST_MYSQL_FIELD_VIEW_HPP
  9. #include <boost/mysql/blob_view.hpp>
  10. #include <boost/mysql/date.hpp>
  11. #include <boost/mysql/datetime.hpp>
  12. #include <boost/mysql/field_kind.hpp>
  13. #include <boost/mysql/string_view.hpp>
  14. #include <boost/mysql/time.hpp>
  15. #include <boost/mysql/detail/auxiliar/access_fwd.hpp>
  16. #include <boost/mysql/detail/auxiliar/field_impl.hpp>
  17. #include <boost/mysql/detail/auxiliar/string_view_offset.hpp>
  18. #include <boost/config.hpp>
  19. #include <array>
  20. #include <cstddef>
  21. #include <cstdint>
  22. #include <iosfwd>
  23. namespace boost {
  24. namespace mysql {
  25. /**
  26. * \brief Non-owning variant-like class that can represent of any of the allowed database types.
  27. * \details
  28. * This is a variant-like class, similar to \ref field, but semi-owning and read-only. Values
  29. * of this type are usually created by the library, not directly by the user. It's cheap to
  30. * construct and copy, and it's the main library interface when reading values from MySQL.
  31. * \n
  32. * Like a variant, at any point, a `field_view` always points to a value of
  33. * certain type. You can query the type using \ref field_view::kind and the `is_xxx` functions
  34. * like \ref field_view::is_int64. Use `as_xxx` and `get_xxx` for checked and unchecked value
  35. * access, respectively. As opposed to \ref field, these functions return values instead of
  36. * references.
  37. *
  38. * \par Object lifetimes
  39. * Depending on how it was constructed, `field_view` can have value or reference semantics:
  40. * \n
  41. * \li If it was created by the library, the `field_view` will have an associated \ref row,
  42. * \ref rows or \ref results object holding memory to which the `field_view` points. It will be valid as
  43. * long as the memory allocated by that object is valid.
  44. * \li If it was created from a \ref field (by calling `operator field_view`), the
  45. * `field_view` acts as a reference to that `field` object, and will be valid as long as the
  46. * `field` is.
  47. * \li If it was created from a scalar (null, integral, floating point, date, datetime or time), the
  48. * `field_view` has value semnatics and will always be valid.
  49. * \li If it was created from a string or blob type, the `field_view` acts as a `string_view` or `blob_view`,
  50. * and will be valid as long as the original string/blob is.
  51. * \n
  52. * Calling any member function on a `field_view` that has been invalidated results in undefined
  53. * behavior.
  54. */
  55. class field_view
  56. {
  57. public:
  58. /**
  59. * \brief Constructs a `field_view` holding NULL.
  60. * \par Exception safety
  61. * No-throw guarantee.
  62. *
  63. * \par Object lifetimes
  64. * Results in a `field_view` with value semantics (always valid).
  65. */
  66. BOOST_CXX14_CONSTEXPR field_view() = default;
  67. /**
  68. * \brief (EXPERIMENTAL) Constructs a `field_view` holding NULL.
  69. * \details
  70. * Caution: `field_view(NULL)` will <b>not</b> match this overload. It will try to construct
  71. * a `string_view` from a NULL C string, causing undefined behavior.
  72. *
  73. * \par Exception safety
  74. * No-throw guarantee.
  75. *
  76. * \par Object lifetimes
  77. * Results in a `field_view` with value semantics (always valid).
  78. */
  79. BOOST_CXX14_CONSTEXPR explicit field_view(std::nullptr_t) noexcept {}
  80. /**
  81. * \brief (EXPERIMENTAL) Constructs a `field_view` holding an `int64`.
  82. * \par Exception safety
  83. * No-throw guarantee.
  84. *
  85. * \par Object lifetimes
  86. * Results in a `field_view` with value semantics (always valid).
  87. */
  88. BOOST_CXX14_CONSTEXPR explicit inline field_view(signed char v) noexcept;
  89. /// \copydoc field_view(signed char)
  90. BOOST_CXX14_CONSTEXPR explicit inline field_view(short v) noexcept;
  91. /// \copydoc field_view(signed char)
  92. BOOST_CXX14_CONSTEXPR explicit inline field_view(int v) noexcept;
  93. /// \copydoc field_view(signed char)
  94. BOOST_CXX14_CONSTEXPR explicit inline field_view(long v) noexcept;
  95. /// \copydoc field_view(signed char)
  96. BOOST_CXX14_CONSTEXPR explicit inline field_view(long long v) noexcept;
  97. /**
  98. * \brief (EXPERIMENTAL) Constructs a `field_view` holding a `uint64`.
  99. * \par Exception safety
  100. * No-throw guarantee.
  101. *
  102. * \par Object lifetimes
  103. * Results in a `field_view` with value semantics (always valid).
  104. */
  105. BOOST_CXX14_CONSTEXPR explicit inline field_view(unsigned char v) noexcept;
  106. /// \copydoc field_view(unsigned char)
  107. BOOST_CXX14_CONSTEXPR explicit inline field_view(unsigned short v) noexcept;
  108. /// \copydoc field_view(unsigned char)
  109. BOOST_CXX14_CONSTEXPR explicit inline field_view(unsigned int v) noexcept;
  110. /// \copydoc field_view(unsigned char)
  111. BOOST_CXX14_CONSTEXPR explicit inline field_view(unsigned long v) noexcept;
  112. /// \copydoc field_view(unsigned char)
  113. BOOST_CXX14_CONSTEXPR explicit inline field_view(unsigned long long v) noexcept;
  114. /**
  115. * \brief (EXPERIMENTAL) Constructs a `field_view` holding a string.
  116. * \par Exception safety
  117. * No-throw guarantee.
  118. *
  119. * \par Object lifetimes
  120. * Results in a `field_view` with reference semantics. It will
  121. * be valid as long as the character buffer the `string_view` points to is valid.
  122. */
  123. BOOST_CXX14_CONSTEXPR explicit inline field_view(string_view v) noexcept;
  124. /**
  125. * \brief (EXPERIMENTAL) Constructs a `field_view` holding a blob.
  126. * \par Exception safety
  127. * No-throw guarantee.
  128. *
  129. * \par Object lifetimes
  130. * Results in a `field_view` with reference semantics. It will
  131. * be valid as long as the character buffer the `blob_view` points to is valid.
  132. */
  133. BOOST_CXX14_CONSTEXPR explicit inline field_view(blob_view v) noexcept;
  134. /**
  135. * \brief (EXPERIMENTAL) Constructs a `field_view` holding a `float`.
  136. * \par Exception safety
  137. * No-throw guarantee.
  138. *
  139. * \par Object lifetimes
  140. * Results in a `field_view` with value semantics (always valid).
  141. */
  142. BOOST_CXX14_CONSTEXPR explicit inline field_view(float v) noexcept;
  143. /**
  144. * \brief (EXPERIMENTAL) Constructs a `field_view` holding a `double`.
  145. * \par Exception safety
  146. * No-throw guarantee.
  147. *
  148. * \par Object lifetimes
  149. * Results in a `field_view` with value semantics (always valid).
  150. */
  151. BOOST_CXX14_CONSTEXPR explicit inline field_view(double v) noexcept;
  152. /**
  153. * \brief (EXPERIMENTAL) Constructs a `field_view` holding a `date`.
  154. * \par Exception safety
  155. * No-throw guarantee.
  156. *
  157. * \par Object lifetimes
  158. * Results in a `field_view` with value semantics (always valid).
  159. */
  160. BOOST_CXX14_CONSTEXPR explicit inline field_view(const date& v) noexcept;
  161. /**
  162. * \brief (EXPERIMENTAL) Constructs a `field_view` holding a `datetime`.
  163. * \par Exception safety
  164. * No-throw guarantee.
  165. *
  166. * \par Object lifetimes
  167. * Results in a `field_view` with value semantics (always valid).
  168. */
  169. BOOST_CXX14_CONSTEXPR explicit inline field_view(const datetime& v) noexcept;
  170. /**
  171. * \brief (EXPERIMENTAL) Constructs a `field_view` holding a `time`.
  172. * \par Exception safety
  173. * No-throw guarantee.
  174. *
  175. * \par Object lifetimes
  176. * Results in a `field_view` with value semantics (always valid).
  177. */
  178. BOOST_CXX14_CONSTEXPR explicit inline field_view(const time& v) noexcept;
  179. /**
  180. * \brief Returns the type of the value this `field_view` is pointing to.
  181. * \par Exception safety
  182. * No-throw guarantee.
  183. */
  184. BOOST_CXX14_CONSTEXPR inline field_kind kind() const noexcept;
  185. /**
  186. * \brief Returns whether this `field_view` points to a `NULL` value.
  187. * \par Exception safety
  188. * No-throw guarantee.
  189. */
  190. BOOST_CXX14_CONSTEXPR bool is_null() const noexcept { return kind() == field_kind::null; }
  191. /**
  192. * \brief Returns whether this `field_view` points to a `int64` value.
  193. * \par Exception safety
  194. * No-throw guarantee.
  195. */
  196. BOOST_CXX14_CONSTEXPR bool is_int64() const noexcept { return kind() == field_kind::int64; }
  197. /**
  198. * \brief Returns whether this `field_view` points to a `uint64` value.
  199. * \par Exception safety
  200. * No-throw guarantee.
  201. */
  202. BOOST_CXX14_CONSTEXPR bool is_uint64() const noexcept { return kind() == field_kind::uint64; }
  203. /**
  204. * \brief Returns whether this `field_view` points to a string value.
  205. * \par Exception safety
  206. * No-throw guarantee.
  207. */
  208. BOOST_CXX14_CONSTEXPR bool is_string() const noexcept { return kind() == field_kind::string; }
  209. /**
  210. * \brief Returns whether this `field_view` points to a binary blob.
  211. * \par Exception safety
  212. * No-throw guarantee.
  213. */
  214. BOOST_CXX14_CONSTEXPR bool is_blob() const noexcept { return kind() == field_kind::blob; }
  215. /**
  216. * \brief Returns whether this `field_view` points to a `float` value.
  217. * \par Exception safety
  218. * No-throw guarantee.
  219. */
  220. BOOST_CXX14_CONSTEXPR bool is_float() const noexcept { return kind() == field_kind::float_; }
  221. /**
  222. * \brief Returns whether this `field_view` points to a `double` value.
  223. * \par Exception safety
  224. * No-throw guarantee.
  225. */
  226. BOOST_CXX14_CONSTEXPR bool is_double() const noexcept { return kind() == field_kind::double_; }
  227. /**
  228. * \brief Returns whether this `field_view` points to a `date` value.
  229. * \par Exception safety
  230. * No-throw guarantee.
  231. */
  232. BOOST_CXX14_CONSTEXPR bool is_date() const noexcept { return kind() == field_kind::date; }
  233. /**
  234. * \brief Returns whether this `field_view` points to a `datetime` value.
  235. * \par Exception safety
  236. * No-throw guarantee.
  237. */
  238. BOOST_CXX14_CONSTEXPR bool is_datetime() const noexcept { return kind() == field_kind::datetime; }
  239. /**
  240. * \brief Returns whether this `field_view` points to a `time` value.
  241. * \par Exception safety
  242. * No-throw guarantee.
  243. */
  244. BOOST_CXX14_CONSTEXPR bool is_time() const noexcept { return kind() == field_kind::time; }
  245. /**
  246. * \brief Retrieves the underlying value as an `int64` or throws an exception.
  247. * \par Exception safety
  248. * Strong guarantee. Throws on type mismatch.
  249. * \throws bad_field_access If `!this->is_int64()`
  250. */
  251. BOOST_CXX14_CONSTEXPR inline std::int64_t as_int64() const;
  252. /**
  253. * \brief Retrieves the underlying value as an `uint64` or throws an exception.
  254. * \par Exception safety
  255. * Strong guarantee. Throws on type mismatch.
  256. * \throws bad_field_access If `!this->is_uint64()`
  257. */
  258. BOOST_CXX14_CONSTEXPR inline std::uint64_t as_uint64() const;
  259. /**
  260. * \brief Retrieves the underlying value as a string or throws an exception.
  261. * \par Exception safety
  262. * Strong guarantee. Throws on type mismatch.
  263. * \throws bad_field_access If `!this->is_string()`
  264. * \par Object lifetimes
  265. * The returned view has the same lifetime rules as `*this` (it's valid as long as `*this` is valid).
  266. */
  267. BOOST_CXX14_CONSTEXPR inline string_view as_string() const;
  268. /**
  269. * \brief Retrieves the underlying value as a blob or throws an exception.
  270. * \par Exception safety
  271. * Strong guarantee. Throws on type mismatch.
  272. * \throws bad_field_access If `!this->is_blob()`
  273. * \par Object lifetimes
  274. * The returned view has the same lifetime rules as `*this` (it's valid as long as `*this` is valid).
  275. */
  276. BOOST_CXX14_CONSTEXPR inline blob_view as_blob() const;
  277. /**
  278. * \brief Retrieves the underlying value as a `float` or throws an exception.
  279. * \par Exception safety
  280. * Strong guarantee. Throws on type mismatch.
  281. * \throws bad_field_access If `!this->is_float()`
  282. */
  283. BOOST_CXX14_CONSTEXPR inline float as_float() const;
  284. /**
  285. * \brief Retrieves the underlying value as a `double` or throws an exception.
  286. * \par Exception safety
  287. * Strong guarantee. Throws on type mismatch.
  288. * \throws bad_field_access If `!this->is_double()`
  289. */
  290. BOOST_CXX14_CONSTEXPR inline double as_double() const;
  291. /**
  292. * \brief Retrieves the underlying value as a `date` or throws an exception.
  293. * \par Exception safety
  294. * Strong guarantee. Throws on type mismatch.
  295. * \throws bad_field_access If `!this->is_date()`
  296. */
  297. BOOST_CXX14_CONSTEXPR inline date as_date() const;
  298. /**
  299. * \brief Retrieves the underlying value as a `datetime` or throws an exception.
  300. * \par Exception safety
  301. * Strong guarantee. Throws on type mismatch.
  302. * \throws bad_field_access If `!this->is_datetime()`
  303. */
  304. BOOST_CXX14_CONSTEXPR inline datetime as_datetime() const;
  305. /**
  306. * \brief Retrieves the underlying value as a `time` or throws an exception.
  307. * \par Exception safety
  308. * Strong guarantee. Throws on type mismatch.
  309. * \throws bad_field_access If `!this->is_time()`
  310. */
  311. BOOST_CXX14_CONSTEXPR inline time as_time() const;
  312. /**
  313. * \brief Retrieves the underlying value as an `int64` (unchecked access).
  314. * \par Preconditions
  315. * `this->is_int64() == true` (if violated, results in undefined behavior).
  316. *
  317. * \par Exception safety
  318. * No-throw guarantee.
  319. */
  320. BOOST_CXX14_CONSTEXPR inline std::int64_t get_int64() const noexcept;
  321. /**
  322. * \brief Retrieves the underlying value as an `uint64` (unchecked access).
  323. * \par Preconditions
  324. * `this->is_uint64() == true` (if violated, results in undefined behavior).
  325. *
  326. * \par Exception safety
  327. * No-throw guarantee.
  328. */
  329. BOOST_CXX14_CONSTEXPR inline std::uint64_t get_uint64() const noexcept;
  330. /**
  331. * \brief Retrieves the underlying value as a string (unchecked access).
  332. * \par Preconditions
  333. * `this->is_string() == true` (if violated, results in undefined behavior).
  334. *
  335. * \par Exception safety
  336. * No-throw guarantee.
  337. *
  338. * \par Object lifetimes
  339. * The returned view has the same lifetime rules as `*this` (it's valid as long as `*this` is valid).
  340. */
  341. BOOST_CXX14_CONSTEXPR inline string_view get_string() const noexcept;
  342. /**
  343. * \brief Retrieves the underlying value as a blob (unchecked access).
  344. * \par Preconditions
  345. * `this->is_blob() == true` (if violated, results in undefined behavior).
  346. *
  347. * \par Exception safety
  348. * No-throw guarantee.
  349. *
  350. * \par Object lifetimes
  351. * The returned view has the same lifetime rules as `*this` (it's valid as long as `*this` is valid).
  352. */
  353. BOOST_CXX14_CONSTEXPR inline blob_view get_blob() const noexcept;
  354. /**
  355. * \brief Retrieves the underlying value as a `float` (unchecked access).
  356. * \par Preconditions
  357. * `this->is_float() == true` (if violated, results in undefined behavior).
  358. *
  359. * \par Exception safety
  360. * No-throw guarantee.
  361. */
  362. BOOST_CXX14_CONSTEXPR inline float get_float() const noexcept;
  363. /**
  364. * \brief Retrieves the underlying value as a `double` (unchecked access).
  365. * \par Preconditions
  366. * `this->is_double() == true` (if violated, results in undefined behavior).
  367. *
  368. * \par Exception safety
  369. * No-throw guarantee.
  370. */
  371. BOOST_CXX14_CONSTEXPR inline double get_double() const noexcept;
  372. /**
  373. * \brief Retrieves the underlying value as a `date` (unchecked access).
  374. * \par Preconditions
  375. * `this->is_date() == true` (if violated, results in undefined behavior).
  376. *
  377. * \par Exception safety
  378. * No-throw guarantee.
  379. */
  380. BOOST_CXX14_CONSTEXPR inline date get_date() const noexcept;
  381. /**
  382. * \brief Retrieves the underlying value as a `datetime` (unchecked access).
  383. * \par Preconditions
  384. * `this->is_datetime() == true` (if violated, results in undefined behavior).
  385. *
  386. * \par Exception safety
  387. * No-throw guarantee.
  388. */
  389. BOOST_CXX14_CONSTEXPR inline datetime get_datetime() const noexcept;
  390. /**
  391. * \brief Retrieves the underlying value as a `time` (unchecked access).
  392. * \par Preconditions
  393. * `this->is_time() == true` (if violated, results in undefined behavior).
  394. *
  395. * \par Exception safety
  396. * No-throw guarantee.
  397. */
  398. BOOST_CXX14_CONSTEXPR inline time get_time() const noexcept;
  399. /**
  400. * \brief Tests for equality.
  401. * \details
  402. * If one of the operands is a `uint64` and the other a
  403. * `int64`, and the values are equal, returns `true`. Otherwise, if the types are
  404. * different, returns always `false` (`float` and `double` values are considered to be
  405. * different between them). `NULL` values are equal to other `NULL` values.
  406. *
  407. * \par Exception safety
  408. * No-throw guarantee.
  409. */
  410. BOOST_CXX14_CONSTEXPR inline bool operator==(const field_view& rhs) const noexcept;
  411. /**
  412. * \brief Tests for inequality.
  413. * \par Exception safety
  414. * No-throw guarantee.
  415. */
  416. BOOST_CXX14_CONSTEXPR bool operator!=(const field_view& rhs) const noexcept { return !(*this == rhs); }
  417. private:
  418. BOOST_CXX14_CONSTEXPR explicit field_view(detail::string_view_offset v, bool is_blob) noexcept
  419. : ikind_(is_blob ? internal_kind::sv_offset_blob : internal_kind::sv_offset_string), repr_(v)
  420. {
  421. }
  422. BOOST_CXX14_CONSTEXPR explicit field_view(const detail::field_impl* v) noexcept
  423. : ikind_(internal_kind::field_ptr), repr_(v)
  424. {
  425. }
  426. enum class internal_kind
  427. {
  428. null = 0,
  429. int64,
  430. uint64,
  431. string,
  432. blob,
  433. float_,
  434. double_,
  435. date,
  436. datetime,
  437. time,
  438. sv_offset_string,
  439. sv_offset_blob,
  440. field_ptr
  441. };
  442. union repr_t
  443. {
  444. std::int64_t int64;
  445. std::uint64_t uint64;
  446. string_view string;
  447. blob_view blob;
  448. float float_;
  449. double double_;
  450. date date_;
  451. datetime datetime_;
  452. time time_;
  453. detail::string_view_offset sv_offset_;
  454. const detail::field_impl* field_ptr;
  455. BOOST_CXX14_CONSTEXPR repr_t() noexcept : int64{} {}
  456. BOOST_CXX14_CONSTEXPR repr_t(std::int64_t v) noexcept : int64(v) {}
  457. BOOST_CXX14_CONSTEXPR repr_t(std::uint64_t v) noexcept : uint64(v) {}
  458. BOOST_CXX14_CONSTEXPR repr_t(string_view v) noexcept : string{v} {}
  459. BOOST_CXX14_CONSTEXPR repr_t(blob_view v) noexcept : blob{v} {}
  460. BOOST_CXX14_CONSTEXPR repr_t(float v) noexcept : float_(v) {}
  461. BOOST_CXX14_CONSTEXPR repr_t(double v) noexcept : double_(v) {}
  462. BOOST_CXX14_CONSTEXPR repr_t(date v) noexcept : date_(v) {}
  463. BOOST_CXX14_CONSTEXPR repr_t(datetime v) noexcept : datetime_(v) {}
  464. BOOST_CXX14_CONSTEXPR repr_t(time v) noexcept : time_(v.count()) {}
  465. BOOST_CXX14_CONSTEXPR repr_t(detail::string_view_offset v) noexcept : sv_offset_{v} {}
  466. BOOST_CXX14_CONSTEXPR repr_t(const detail::field_impl* v) noexcept : field_ptr(v) {}
  467. };
  468. internal_kind ikind_{internal_kind::null};
  469. repr_t repr_;
  470. BOOST_CXX14_CONSTEXPR bool is_field_ptr() const noexcept { return ikind_ == internal_kind::field_ptr; }
  471. BOOST_CXX14_CONSTEXPR inline void check_kind(internal_kind expected) const;
  472. #ifndef BOOST_MYSQL_DOXYGEN
  473. friend class field;
  474. friend struct detail::field_view_access;
  475. friend std::ostream& operator<<(std::ostream& os, const field_view& v);
  476. #endif
  477. };
  478. /**
  479. * \relates field_view
  480. * \brief Streams a `field_view`.
  481. */
  482. inline std::ostream& operator<<(std::ostream& os, const field_view& v);
  483. } // namespace mysql
  484. } // namespace boost
  485. #include <boost/mysql/impl/field_view.hpp>
  486. #endif