field.hpp 30 KB

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