field_view.hpp 22 KB

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