field.hpp 30 KB

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