field.hpp 31 KB

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