field.hpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. //
  2. // Copyright (c) 2019-2024 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_HPP
  8. #define BOOST_MYSQL_FIELD_HPP
  9. #include <boost/mysql/blob.hpp>
  10. #include <boost/mysql/field_kind.hpp>
  11. #include <boost/mysql/field_view.hpp>
  12. #include <boost/mysql/string_view.hpp>
  13. #include <boost/mysql/detail/config.hpp>
  14. #include <boost/mysql/detail/field_impl.hpp>
  15. #include <boost/variant2/variant.hpp>
  16. #include <cstddef>
  17. #include <iosfwd>
  18. #include <string>
  19. #ifdef __cpp_lib_string_view
  20. #include <string_view>
  21. #endif
  22. namespace boost {
  23. namespace mysql {
  24. /**
  25. * \brief Variant-like class that can represent of any of the allowed database types.
  26. * \details
  27. * This is a regular variant-like class that can represent any of the types that MySQL allows. It
  28. * has value semantics (as opposed to \ref field_view). Instances of this class are not created
  29. * by the library. They should be created by the user, when the reference semantics of
  30. * \ref field_view are not appropriate.
  31. * \n
  32. * Like a variant, at any point, a `field` always contains a value of
  33. * certain type. You can query the type using \ref kind and the `is_xxx` functions
  34. * like \ref is_int64. Use `as_xxx` and `get_xxx` for checked and unchecked value
  35. * access, respectively. You can mutate a `field` by calling the assignment operator,
  36. * or using the lvalue references returned by `as_xxx` and `get_xxx`.
  37. */
  38. class field
  39. {
  40. public:
  41. /**
  42. * \brief Constructs a `field` holding NULL.
  43. * \par Exception safety
  44. * No-throw guarantee.
  45. */
  46. field() = default;
  47. /**
  48. * \brief Copy constructor.
  49. * \par Exception safety
  50. * Strong guarantee. Internal allocations may throw.
  51. */
  52. field(const field&) = default;
  53. /**
  54. * \brief Move constructor.
  55. * \par Exception safety
  56. * No-throw guarantee.
  57. *
  58. * \par Object lifetimes
  59. * All references into `other` are invalidated, including the ones obtained by calling
  60. * get_xxx, as_xxx and \ref field::operator field_view().
  61. */
  62. field(field&& other) = default;
  63. /**
  64. * \brief Copy assignment.
  65. * \par Exception safety
  66. * Basic guarantee. Internal allocations may throw.
  67. *
  68. * \par Object lifetimes
  69. * Invalidates references obtained by as_xxx and get_xxx functions,
  70. * but not the ones obtained by \ref field::operator field_view().
  71. */
  72. field& operator=(const field&) = default;
  73. /**
  74. * \brief Move assignment.
  75. * \par Exception safety
  76. * No-throw guarantee.
  77. *
  78. * \par Object lifetimes
  79. * Invalidates references to `*this` obtained by as_xxx and get_xxx functions,
  80. * but not the ones obtained by \ref field::operator field_view(). All references into `other`
  81. * are invalidated, including the ones obtained by calling get_xxx, as_xxx and
  82. * \ref field::operator field_view().
  83. */
  84. field& operator=(field&& other) = default;
  85. /// Destructor.
  86. ~field() = default;
  87. /**
  88. * \brief Constructs a `field` holding NULL.
  89. * \details
  90. * Caution: `field(NULL)` will __NOT__ match this overload. It will try to construct
  91. * a `string_view` from a NULL C string, causing undefined behavior.
  92. *
  93. * \par Exception safety
  94. * No-throw guarantee.
  95. */
  96. explicit field(std::nullptr_t) noexcept {}
  97. /**
  98. * \brief Constructs a `field` holding an `int64`.
  99. * \par Exception safety
  100. * No-throw guarantee.
  101. */
  102. explicit field(signed char v) noexcept : repr_(std::int64_t(v)) {}
  103. /// \copydoc field(signed char)
  104. explicit field(short v) noexcept : repr_(std::int64_t(v)) {}
  105. /// \copydoc field(signed char)
  106. explicit field(int v) noexcept : repr_(std::int64_t(v)) {}
  107. /// \copydoc field(signed char)
  108. explicit field(long v) noexcept : repr_(std::int64_t(v)) {}
  109. /// \copydoc field(signed char)
  110. explicit field(long long v) noexcept : repr_(std::int64_t(v)) {}
  111. /**
  112. * \brief Constructs a `field` holding an `uint64`.
  113. * \par Exception safety
  114. * No-throw guarantee.
  115. */
  116. explicit field(unsigned char v) noexcept : repr_(std::uint64_t(v)) {}
  117. /// \copydoc field(unsigned char)
  118. explicit field(unsigned short v) noexcept : repr_(std::uint64_t(v)) {}
  119. /// \copydoc field(unsigned char)
  120. explicit field(unsigned int v) noexcept : repr_(std::uint64_t(v)) {}
  121. /// \copydoc field(unsigned char)
  122. explicit field(unsigned long v) noexcept : repr_(std::uint64_t(v)) {}
  123. /// \copydoc field(unsigned char)
  124. explicit field(unsigned long long v) noexcept : repr_(std::uint64_t(v)) {}
  125. /**
  126. * \brief Constructors from character types would incorrectly construct a `field` holding an integer,
  127. * so they are not allowed.
  128. */
  129. explicit field(char) = delete;
  130. /// \copydoc field(char)
  131. explicit field(wchar_t) = delete;
  132. /// \copydoc field(char)
  133. explicit field(char16_t) = delete;
  134. /// \copydoc field(char)
  135. explicit field(char32_t) = delete;
  136. #ifdef __cpp_char8_t
  137. /// \copydoc field(char)
  138. explicit field(char8_t) = delete;
  139. #endif
  140. /**
  141. * \brief Constructs a `field` holding a string.
  142. * \par Exception safety
  143. * Strong guarantee. Internal allocations may throw.
  144. */
  145. explicit field(const std::string& v) : repr_(v) {}
  146. /**
  147. * \brief Constructs a `field` holding a string.
  148. * \details v is moved into an internal `std::string` object.
  149. * \par Exception safety
  150. * No-throw guarantee.
  151. */
  152. explicit field(std::string&& v) noexcept : repr_(std::move(v)) {}
  153. /// \copydoc field(const std::string&)
  154. explicit field(const char* v) : repr_(boost::variant2::in_place_type_t<std::string>(), v) {}
  155. /// \copydoc field(const std::string&)
  156. explicit field(string_view v) : repr_(boost::variant2::in_place_type_t<std::string>(), v) {}
  157. #if defined(__cpp_lib_string_view)
  158. /// \copydoc field(const std::string&)
  159. explicit field(std::string_view v) noexcept : repr_(boost::variant2::in_place_type_t<std::string>(), v) {}
  160. #endif
  161. /**
  162. * \brief Constructs a `field` holding a `blob`.
  163. * \details v is moved into an internal `blob` object.
  164. * \par Exception safety
  165. * No-throw guarantee.
  166. */
  167. explicit field(blob v) noexcept : repr_(std::move(v)) {}
  168. /**
  169. * \brief Constructs a `field` holding a `float`.
  170. * \par Exception safety
  171. * No-throw guarantee.
  172. */
  173. explicit field(float v) noexcept : repr_(v) {}
  174. /**
  175. * \brief Constructs a `field` holding a `double`.
  176. * \par Exception safety
  177. * No-throw guarantee.
  178. */
  179. explicit field(double v) noexcept : repr_(v) {}
  180. /**
  181. * \brief Constructs a `field` holding a `date`.
  182. * \par Exception safety
  183. * No-throw guarantee.
  184. */
  185. explicit field(const date& v) noexcept : repr_(v) {}
  186. /**
  187. * \brief Constructs a `field` holding a `datetime`.
  188. * \par Exception safety
  189. * No-throw guarantee.
  190. */
  191. explicit field(const datetime& v) noexcept : repr_(v) {}
  192. /**
  193. * \brief Constructs a `field` holding a `time`.
  194. * \par Exception safety
  195. * No-throw guarantee.
  196. */
  197. explicit field(const time& v) noexcept : repr_(v) {}
  198. /**
  199. * \brief Constructs a `field` from a \ref field_view.
  200. * \details The resulting `field` has the same kind and value as the original `field_view`.
  201. *
  202. * \par Exception safety
  203. * Strong guarantee. Internal allocations may throw.
  204. *
  205. * \par Object lifetimes
  206. * The resulting `field` is guaranteed to be valid even after `v` becomes invalid.
  207. */
  208. field(const field_view& v) { from_view(v); }
  209. /**
  210. * \brief Replaces `*this` with a `NULL`, changing the kind to `null` and destroying any
  211. * previous contents.
  212. *
  213. * \par Exception safety
  214. * No-throw guarantee.
  215. *
  216. * \par Object lifetimes
  217. * Invalidates references obtained by as_xxx and get_xxx functions,
  218. * but not the ones obtained by \ref field::operator field_view().
  219. */
  220. field& operator=(std::nullptr_t) noexcept
  221. {
  222. repr_.data.emplace<detail::field_impl::null_t>();
  223. return *this;
  224. }
  225. /**
  226. * \brief Replaces `*this` with `v`, changing the kind to `int64` and destroying any
  227. * previous contents.
  228. *
  229. * \par Exception safety
  230. * No-throw guarantee.
  231. *
  232. * \par Object lifetimes
  233. * Invalidates references obtained by as_xxx and get_xxx functions,
  234. * but not the ones obtained by \ref field::operator field_view().
  235. */
  236. field& operator=(signed char v) noexcept
  237. {
  238. repr_.data.emplace<std::int64_t>(v);
  239. return *this;
  240. }
  241. /// \copydoc operator=(signed char)
  242. field& operator=(short v) noexcept
  243. {
  244. repr_.data.emplace<std::int64_t>(v);
  245. return *this;
  246. }
  247. /// \copydoc operator=(signed char)
  248. field& operator=(int v) noexcept
  249. {
  250. repr_.data.emplace<std::int64_t>(v);
  251. return *this;
  252. }
  253. /// \copydoc operator=(signed char)
  254. field& operator=(long v) noexcept
  255. {
  256. repr_.data.emplace<std::int64_t>(v);
  257. return *this;
  258. }
  259. /// \copydoc operator=(signed char)
  260. field& operator=(long long v) noexcept
  261. {
  262. repr_.data.emplace<std::int64_t>(v);
  263. return *this;
  264. }
  265. /**
  266. * \brief Replaces `*this` with `v`, changing the kind to `uint64` and destroying any
  267. * previous contents.
  268. *
  269. * \par Exception safety
  270. * No-throw guarantee.
  271. *
  272. * \par Object lifetimes
  273. * Invalidates references obtained by as_xxx and get_xxx functions,
  274. * but not the ones obtained by \ref field::operator field_view().
  275. */
  276. field& operator=(unsigned char v) noexcept
  277. {
  278. repr_.data.emplace<std::uint64_t>(v);
  279. return *this;
  280. }
  281. /// \copydoc operator=(unsigned char)
  282. field& operator=(unsigned short v) noexcept
  283. {
  284. repr_.data.emplace<std::uint64_t>(v);
  285. return *this;
  286. }
  287. /// \copydoc operator=(unsigned char)
  288. field& operator=(unsigned int v) noexcept
  289. {
  290. repr_.data.emplace<std::uint64_t>(v);
  291. return *this;
  292. }
  293. /// \copydoc operator=(unsigned char)
  294. field& operator=(unsigned long v) noexcept
  295. {
  296. repr_.data.emplace<std::uint64_t>(v);
  297. return *this;
  298. }
  299. /// \copydoc operator=(unsigned char)
  300. field& operator=(unsigned long long v) noexcept
  301. {
  302. repr_.data.emplace<std::uint64_t>(v);
  303. return *this;
  304. }
  305. /**
  306. * \brief Assignments from character types would incorrectly assign an integer,
  307. * so they are not allowed.
  308. */
  309. field& operator=(char) = delete;
  310. /// \copydoc operator=(char)
  311. field& operator=(wchar_t) = delete;
  312. /// \copydoc operator=(char)
  313. field& operator=(char16_t) = delete;
  314. /// \copydoc operator=(char)
  315. field& operator=(char32_t) = delete;
  316. #ifdef __cpp_char8_t
  317. /// \copydoc operator=(char)
  318. field& operator=(char8_t) = delete;
  319. #endif
  320. /**
  321. * \brief Replaces `*this` with `v`, changing the kind to `string` and destroying any previous
  322. * contents.
  323. *
  324. * \par Exception safety
  325. * Basic guarantee. Internal allocations may throw.
  326. *
  327. * \par Object lifetimes
  328. * Invalidates references obtained by as_xxx and get_xxx functions,
  329. * but not the ones obtained by \ref field::operator field_view().
  330. */
  331. field& operator=(const std::string& v)
  332. {
  333. repr_.data.emplace<std::string>(v);
  334. return *this;
  335. }
  336. /// \copydoc operator=(const std::string&)
  337. field& operator=(std::string&& v)
  338. {
  339. repr_.data.emplace<std::string>(std::move(v));
  340. return *this;
  341. }
  342. /// \copydoc operator=(const std::string&)
  343. field& operator=(const char* v)
  344. {
  345. repr_.data.emplace<std::string>(v);
  346. return *this;
  347. }
  348. /// \copydoc operator=(const std::string&)
  349. field& operator=(string_view v)
  350. {
  351. repr_.data.emplace<std::string>(v);
  352. return *this;
  353. }
  354. #if defined(__cpp_lib_string_view)
  355. /// \copydoc operator=(const std::string&)
  356. field& operator=(std::string_view v)
  357. {
  358. repr_.data.emplace<std::string>(v);
  359. return *this;
  360. }
  361. #endif
  362. /**
  363. * \brief Replaces `*this` with `v`, changing the kind to `blob` and destroying any
  364. * previous contents.
  365. *
  366. * \par Exception safety
  367. * Basic guarantee. Internal allocations may throw.
  368. *
  369. * \par Object lifetimes
  370. * Invalidates references obtained by as_xxx and get_xxx functions,
  371. * but not the ones obtained by \ref field::operator field_view().
  372. */
  373. field& operator=(blob v)
  374. {
  375. repr_.data.emplace<blob>(std::move(v));
  376. return *this;
  377. }
  378. /**
  379. * \brief Replaces `*this` with `v`, changing the kind to `float_` and destroying any
  380. * previous contents.
  381. *
  382. * \par Exception safety
  383. * No-throw guarantee.
  384. *
  385. * \par Object lifetimes
  386. * Invalidates references obtained by as_xxx and get_xxx functions,
  387. * but not the ones obtained by \ref field::operator field_view().
  388. */
  389. field& operator=(float v) noexcept
  390. {
  391. repr_.data.emplace<float>(v);
  392. return *this;
  393. }
  394. /**
  395. * \brief Replaces `*this` with `v`, changing the kind to `double` and destroying any
  396. * previous contents.
  397. *
  398. * \par Exception safety
  399. * No-throw guarantee.
  400. *
  401. * \par Object lifetimes
  402. * Invalidates references obtained by as_xxx and get_xxx functions,
  403. * but not the ones obtained by \ref field::operator field_view().
  404. */
  405. field& operator=(double v) noexcept
  406. {
  407. repr_.data.emplace<double>(v);
  408. return *this;
  409. }
  410. /**
  411. * \brief Replaces `*this` with `v`, changing the kind to `date` and destroying any
  412. * previous contents.
  413. *
  414. * \par Exception safety
  415. * No-throw guarantee.
  416. *
  417. * \par Object lifetimes
  418. * Invalidates references obtained by as_xxx and get_xxx functions,
  419. * but not the ones obtained by \ref field::operator field_view().
  420. */
  421. field& operator=(const date& v) noexcept
  422. {
  423. repr_.data.emplace<date>(v);
  424. return *this;
  425. }
  426. /**
  427. * \brief Replaces `*this` with `v`, changing the kind to `datetime` and destroying any
  428. * previous contents.
  429. *
  430. * \par Exception safety
  431. * No-throw guarantee.
  432. *
  433. * \par Object lifetimes
  434. * Invalidates references obtained by as_xxx and get_xxx functions,
  435. * but not the ones obtained by \ref field::operator field_view().
  436. */
  437. field& operator=(const datetime& v) noexcept
  438. {
  439. repr_.data.emplace<datetime>(v);
  440. return *this;
  441. }
  442. /**
  443. * \brief Replaces `*this` with `v`, changing the kind to `time` and destroying any
  444. * previous contents.
  445. *
  446. * \par Exception safety
  447. * No-throw guarantee.
  448. *
  449. * \par Object lifetimes
  450. * Invalidates references obtained by as_xxx and get_xxx functions, but not
  451. */
  452. field& operator=(const time& v) noexcept
  453. {
  454. repr_.data.emplace<time>(v);
  455. return *this;
  456. }
  457. /**
  458. * \brief Replaces `*this` with `v`, changing the kind to `v.kind()` and destroying any previous
  459. * contents.
  460. *
  461. * \par Exception safety
  462. * Basic guarantee. Internal allocations may throw.
  463. *
  464. * \par Object lifetimes
  465. * Invalidates references to `*this` obtained by as_xxx and get_xxx functions, but not
  466. * the ones obtained by \ref field::operator field_view().
  467. *\n
  468. * `*this` is guaranteed to be valid even after `v` becomes invalid.
  469. */
  470. field& operator=(const field_view& v)
  471. {
  472. from_view(v);
  473. return *this;
  474. }
  475. /**
  476. * \brief Returns the type of the value this `field` is holding.
  477. * \par Exception safety
  478. * No-throw guarantee.
  479. */
  480. field_kind kind() const noexcept { return repr_.kind(); }
  481. /**
  482. * \brief Returns whether this `field` is holding a `NULL` value.
  483. * \par Exception safety
  484. * No-throw guarantee.
  485. */
  486. bool is_null() const noexcept { return kind() == field_kind::null; }
  487. /**
  488. * \brief Returns whether this `field` is holding a `int64` value.
  489. * \par Exception safety
  490. * No-throw guarantee.
  491. */
  492. bool is_int64() const noexcept { return kind() == field_kind::int64; }
  493. /**
  494. * \brief Returns whether this `field` is holding a `uint64` value.
  495. * \par Exception safety
  496. * No-throw guarantee.
  497. */
  498. bool is_uint64() const noexcept { return kind() == field_kind::uint64; }
  499. /**
  500. * \brief Returns whether this `field` is holding a string value.
  501. * \par Exception safety
  502. * No-throw guarantee.
  503. */
  504. bool is_string() const noexcept { return kind() == field_kind::string; }
  505. /**
  506. * \brief Returns whether this `field` is holding a blob value.
  507. * \par Exception safety
  508. * No-throw guarantee.
  509. */
  510. bool is_blob() const noexcept { return kind() == field_kind::blob; }
  511. /**
  512. * \brief Returns whether this `field` is holding a `float` value.
  513. * \par Exception safety
  514. * No-throw guarantee.
  515. */
  516. bool is_float() const noexcept { return kind() == field_kind::float_; }
  517. /**
  518. * \brief Returns whether this `field` is holding a `double` value.
  519. * \par Exception safety
  520. * No-throw guarantee.
  521. */
  522. bool is_double() const noexcept { return kind() == field_kind::double_; }
  523. /**
  524. * \brief Returns whether this `field` is holding a `date` value.
  525. * \par Exception safety
  526. * No-throw guarantee.
  527. */
  528. bool is_date() const noexcept { return kind() == field_kind::date; }
  529. /**
  530. * \brief Returns whether this `field` is holding a `datetime` value.
  531. * \par Exception safety
  532. * No-throw guarantee.
  533. */
  534. bool is_datetime() const noexcept { return kind() == field_kind::datetime; }
  535. /**
  536. * \brief Returns whether this `field` is holding a `time` value.
  537. * \par Exception safety
  538. * No-throw guarantee.
  539. */
  540. bool is_time() const noexcept { return kind() == field_kind::time; }
  541. /**
  542. * \brief Retrieves a reference to the underlying `std::int64_t` value or throws an exception.
  543. * \par Exception safety
  544. * Strong guarantee. Throws on type mismatch.
  545. * \throws bad_field_access If `!this->is_int64()`
  546. *
  547. * \par Object lifetimes
  548. * The returned reference is valid as long as `*this` is alive and no function that invalidates
  549. * references is called on `*this`.
  550. */
  551. const std::int64_t& as_int64() const { return repr_.as<std::int64_t>(); }
  552. /**
  553. * \brief Retrieves a reference to the underlying `std::uint64_t` value or throws an exception.
  554. * \par Exception safety
  555. * Strong guarantee. Throws on type mismatch.
  556. * \throws bad_field_access If `!this->is_uint64()`
  557. *
  558. * \par Object lifetimes
  559. * The returned reference is valid as long as `*this` is alive and no function that invalidates
  560. * references is called on `*this`.
  561. */
  562. const std::uint64_t& as_uint64() const { return repr_.as<std::uint64_t>(); }
  563. /**
  564. * \brief Retrieves a reference to the underlying `std::string` value or throws an exception.
  565. * \par Exception safety
  566. * Strong guarantee. Throws on type mismatch.
  567. * \throws bad_field_access If `!this->is_string()`
  568. *
  569. * \par Object lifetimes
  570. * The returned reference is valid as long as `*this` is alive and no function that invalidates
  571. * references is called on `*this`.
  572. */
  573. const std::string& as_string() const { return repr_.as<std::string>(); }
  574. /**
  575. * \brief Retrieves a reference to the underlying `blob` value or throws an exception.
  576. * \par Exception safety
  577. * Strong guarantee. Throws on type mismatch.
  578. * \throws bad_field_access If `!this->is_blob()`
  579. *
  580. * \par Object lifetimes
  581. * The returned reference is valid as long as `*this` is alive and no function that invalidates
  582. * references is called on `*this`.
  583. */
  584. const blob& as_blob() const { return repr_.as<blob>(); }
  585. /**
  586. * \brief Retrieves a reference to the underlying `float` value or throws an exception.
  587. * \par Exception safety
  588. * Strong guarantee. Throws on type mismatch.
  589. * \throws bad_field_access If `!this->is_float()`
  590. *
  591. * \par Object lifetimes
  592. * The returned reference is valid as long as `*this` is alive and no function that invalidates
  593. * references is called on `*this`.
  594. */
  595. const float& as_float() const { return repr_.as<float>(); }
  596. /**
  597. * \brief Retrieves a reference to the underlying `double` value or throws an exception.
  598. * \par Exception safety
  599. * Strong guarantee. Throws on type mismatch.
  600. * \throws bad_field_access If `!this->is_double()`
  601. *
  602. * \par Object lifetimes
  603. * The returned reference is valid as long as `*this` is alive and no function that invalidates
  604. * references is called on `*this`.
  605. */
  606. const double& as_double() const { return repr_.as<double>(); }
  607. /**
  608. * \brief Retrieves a reference to the underlying `date` value or throws an exception.
  609. * \par Exception safety
  610. * Strong guarantee. Throws on type mismatch.
  611. * \throws bad_field_access If `!this->is_date()`
  612. *
  613. * \par Object lifetimes
  614. * The returned reference is valid as long as `*this` is alive and no function that invalidates
  615. * references is called on `*this`.
  616. */
  617. const date& as_date() const { return repr_.as<date>(); }
  618. /**
  619. * \brief Retrieves a reference to the underlying `datetime` value or throws an exception.
  620. * \par Exception safety
  621. * Strong guarantee. Throws on type mismatch.
  622. * \throws bad_field_access If `!this->is_datetime()`
  623. *
  624. * \par Object lifetimes
  625. * The returned reference is valid as long as `*this` is alive and no function that invalidates
  626. * references is called on `*this`.
  627. */
  628. const datetime& as_datetime() const { return repr_.as<datetime>(); }
  629. /**
  630. * \brief Retrieves a reference to the underlying `time` value or throws an exception.
  631. * \par Exception safety
  632. * Strong guarantee. Throws on type mismatch.
  633. * \throws bad_field_access If `!this->is_time()`
  634. *
  635. * \par Object lifetimes
  636. * The returned reference is valid as long as `*this` is alive and no function that invalidates
  637. * references is called on `*this`.
  638. */
  639. const time& as_time() const { return repr_.as<time>(); }
  640. /// \copydoc as_int64
  641. std::int64_t& as_int64() { return repr_.as<std::int64_t>(); }
  642. /// \copydoc as_uint64
  643. std::uint64_t& as_uint64() { return repr_.as<std::uint64_t>(); }
  644. /// \copydoc as_string
  645. std::string& as_string() { return repr_.as<std::string>(); }
  646. /// \copydoc as_blob
  647. blob& as_blob() { return repr_.as<blob>(); }
  648. /// \copydoc as_float
  649. float& as_float() { return repr_.as<float>(); }
  650. /// \copydoc as_double
  651. double& as_double() { return repr_.as<double>(); }
  652. /// \copydoc as_date
  653. date& as_date() { return repr_.as<date>(); }
  654. /// \copydoc as_datetime
  655. datetime& as_datetime() { return repr_.as<datetime>(); }
  656. /// \copydoc as_time
  657. time& as_time() { return repr_.as<time>(); }
  658. /**
  659. * \brief Retrieves a reference to the underlying `std::int64_t` value (unchecked access).
  660. * \par Preconditions
  661. * `this->is_int64() == true` (if violated, results in undefined behavior).
  662. *
  663. * \par Exception safety
  664. * No-throw guarantee.
  665. *
  666. * \par Object lifetimes
  667. * The returned reference is valid as long as `*this` is alive and no function that invalidates
  668. * references is called on `*this`.
  669. */
  670. const std::int64_t& get_int64() const noexcept { return repr_.get<std::int64_t>(); }
  671. /**
  672. * \brief Retrieves a reference to the underlying `std::uint64_t` value (unchecked access).
  673. * \par Preconditions
  674. * `this->is_uint64() == true` (if violated, results in undefined behavior).
  675. *
  676. * \par Exception safety
  677. * No-throw guarantee.
  678. *
  679. * \par Object lifetimes
  680. * The returned reference is valid as long as `*this` is alive and no function that invalidates
  681. * references is called on `*this`.
  682. */
  683. const std::uint64_t& get_uint64() const noexcept { return repr_.get<std::uint64_t>(); }
  684. /**
  685. * \brief Retrieves a reference to the underlying `std::string` value (unchecked access).
  686. * \par Preconditions
  687. * `this->is_string() == true` (if violated, results in undefined behavior).
  688. *
  689. * \par Exception safety
  690. * No-throw guarantee.
  691. *
  692. * \par Object lifetimes
  693. * The returned reference is valid as long as `*this` is alive and no function that invalidates
  694. * references is called on `*this`.
  695. */
  696. const std::string& get_string() const noexcept { return repr_.get<std::string>(); }
  697. /**
  698. * \brief Retrieves a reference to the underlying `blob` value (unchecked access).
  699. * \par Preconditions
  700. * `this->is_blob() == true` (if violated, results in undefined behavior).
  701. *
  702. * \par Exception safety
  703. * No-throw guarantee.
  704. *
  705. * \par Object lifetimes
  706. * The returned reference is valid as long as `*this` is alive and no function that invalidates
  707. * references is called on `*this`.
  708. */
  709. const blob& get_blob() const noexcept { return repr_.get<blob>(); }
  710. /**
  711. * \brief Retrieves a reference to the underlying `float` value (unchecked access).
  712. * \par Preconditions
  713. * `this->is_float() == true` (if violated, results in undefined behavior).
  714. *
  715. * \par Exception safety
  716. * No-throw guarantee.
  717. *
  718. * \par Object lifetimes
  719. * The returned reference is valid as long as `*this` is alive and no function that invalidates
  720. * references is called on `*this`.
  721. */
  722. const float& get_float() const noexcept { return repr_.get<float>(); }
  723. /**
  724. * \brief Retrieves a reference to the underlying `double` value (unchecked access).
  725. * \par Preconditions
  726. * `this->is_double() == true` (if violated, results in undefined behavior).
  727. *
  728. * \par Exception safety
  729. * No-throw guarantee.
  730. *
  731. * \par Object lifetimes
  732. * The returned reference is valid as long as `*this` is alive and no function that invalidates
  733. * references is called on `*this`.
  734. */
  735. const double& get_double() const noexcept { return repr_.get<double>(); }
  736. /**
  737. * \brief Retrieves a reference to the underlying `date` value (unchecked access).
  738. * \par Preconditions
  739. * `this->is_date() == true` (if violated, results in undefined behavior).
  740. *
  741. * \par Exception safety
  742. * No-throw guarantee.
  743. *
  744. * \par Object lifetimes
  745. * The returned reference is valid as long as `*this` is alive and no function that invalidates
  746. * references is called on `*this`.
  747. */
  748. const date& get_date() const noexcept { return repr_.get<date>(); }
  749. /**
  750. * \brief Retrieves a reference to the underlying `datetime` value (unchecked access).
  751. * \par Preconditions
  752. * `this->is_datetime() == true` (if violated, results in undefined behavior).
  753. *
  754. * \par Exception safety
  755. * No-throw guarantee.
  756. *
  757. * \par Object lifetimes
  758. * The returned reference is valid as long as `*this` is alive and no function that invalidates
  759. * references is called on `*this`.
  760. */
  761. const datetime& get_datetime() const noexcept { return repr_.get<datetime>(); }
  762. /**
  763. * \brief Retrieves a reference to the underlying `time` value (unchecked access).
  764. * \par Preconditions
  765. * `this->is_time() == true` (if violated, results in undefined behavior).
  766. *
  767. * \par Exception safety
  768. * No-throw guarantee.
  769. *
  770. * \par Object lifetimes
  771. * The returned reference is valid as long as `*this` is alive and no function that invalidates
  772. * references is called on `*this`.
  773. */
  774. const time& get_time() const noexcept { return repr_.get<time>(); }
  775. /// \copydoc get_int64
  776. std::int64_t& get_int64() noexcept { return repr_.get<std::int64_t>(); }
  777. /// \copydoc get_uint64
  778. std::uint64_t& get_uint64() noexcept { return repr_.get<std::uint64_t>(); }
  779. /// \copydoc get_string
  780. std::string& get_string() noexcept { return repr_.get<std::string>(); }
  781. /// \copydoc get_blob
  782. blob& get_blob() noexcept { return repr_.get<blob>(); }
  783. /// \copydoc get_float
  784. float& get_float() noexcept { return repr_.get<float>(); }
  785. /// \copydoc get_double
  786. double& get_double() noexcept { return repr_.get<double>(); }
  787. /// \copydoc get_date
  788. date& get_date() noexcept { return repr_.get<date>(); }
  789. /// \copydoc get_datetime
  790. datetime& get_datetime() noexcept { return repr_.get<datetime>(); }
  791. /// \copydoc get_time
  792. time& get_time() noexcept { return repr_.get<time>(); }
  793. /**
  794. * \brief Constructs a \ref field_view pointing to `*this`.
  795. * \details The resulting `field_view` has the same kind and value as `*this`.
  796. *
  797. * \par Exception safety
  798. * No-throw guarantee.
  799. *
  800. * \par Object lifetimes
  801. * The returned object acts as a
  802. * reference to `*this`, and will be valid as long as `*this` is alive.
  803. */
  804. inline operator field_view() const noexcept { return field_view(&repr_); }
  805. private:
  806. detail::field_impl repr_;
  807. BOOST_MYSQL_DECL
  808. void from_view(const field_view& v);
  809. };
  810. /**
  811. * \relates field
  812. * \brief Tests for equality.
  813. * \details The same considerations as \ref field_view::operator== apply.
  814. *
  815. * \par Exception safety
  816. * No-throw guarantee.
  817. */
  818. inline bool operator==(const field& lhs, const field& rhs) noexcept
  819. {
  820. return field_view(lhs) == field_view(rhs);
  821. }
  822. /**
  823. * \relates field
  824. * \brief Tests for inequality.
  825. * \par Exception safety
  826. * No-throw guarantee.
  827. */
  828. inline bool operator!=(const field& lhs, const field& rhs) noexcept { return !(lhs == rhs); }
  829. /**
  830. * \relates field
  831. * \brief Tests for equality.
  832. * \details The same considerations as \ref field_view::operator== apply.
  833. *
  834. * \par Exception safety
  835. * No-throw guarantee.
  836. */
  837. inline bool operator==(const field_view& lhs, const field& rhs) noexcept { return lhs == field_view(rhs); }
  838. /**
  839. * \relates field
  840. * \brief Tests for inequality.
  841. * \par Exception safety
  842. * No-throw guarantee.
  843. */
  844. inline bool operator!=(const field_view& lhs, const field& rhs) noexcept { return !(lhs == rhs); }
  845. /**
  846. * \relates field
  847. * \brief Tests for equality.
  848. * \details The same considerations as \ref field_view::operator== apply.
  849. * \par Exception safety
  850. * No-throw guarantee.
  851. */
  852. inline bool operator==(const field& lhs, const field_view& rhs) noexcept { return field_view(lhs) == rhs; }
  853. /**
  854. * \relates field
  855. * \brief Tests for inequality.
  856. * \par Exception safety
  857. * No-throw guarantee.
  858. */
  859. inline bool operator!=(const field& lhs, const field_view& rhs) noexcept { return !(lhs == rhs); }
  860. /**
  861. * \relates field
  862. * \brief Streams a `field`.
  863. */
  864. BOOST_MYSQL_DECL
  865. std::ostream& operator<<(std::ostream& os, const field& v);
  866. } // namespace mysql
  867. } // namespace boost
  868. #ifdef BOOST_MYSQL_HEADER_ONLY
  869. #include <boost/mysql/impl/field.ipp>
  870. #endif
  871. #endif