| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001 |
- //
- // Copyright (c) 2019-2025 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
- //
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- //
- #ifndef BOOST_MYSQL_FIELD_HPP
- #define BOOST_MYSQL_FIELD_HPP
- #include <boost/mysql/blob.hpp>
- #include <boost/mysql/field_kind.hpp>
- #include <boost/mysql/field_view.hpp>
- #include <boost/mysql/string_view.hpp>
- #include <boost/mysql/detail/config.hpp>
- #include <boost/mysql/detail/field_impl.hpp>
- #include <boost/config.hpp>
- #include <boost/variant2/variant.hpp>
- #include <cstddef>
- #include <iosfwd>
- #include <string>
- #ifndef BOOST_NO_CXX17_HDR_STRING_VIEW
- #include <string_view>
- #endif
- namespace boost {
- namespace mysql {
- /**
- * \brief Variant-like class that can represent of any of the allowed database types.
- * \details
- * This is a regular variant-like class that can represent any of the types that MySQL allows. It
- * has value semantics (as opposed to \ref field_view). Instances of this class are not created
- * by the library. They should be created by the user, when the reference semantics of
- * \ref field_view are not appropriate.
- * \n
- * Like a variant, at any point, a `field` always contains a value of
- * certain type. You can query the type using \ref kind and the `is_xxx` functions
- * like \ref is_int64. Use `as_xxx` and `get_xxx` for checked and unchecked value
- * access, respectively. You can mutate a `field` by calling the assignment operator,
- * or using the lvalue references returned by `as_xxx` and `get_xxx`.
- */
- class field
- {
- public:
- /**
- * \brief Constructs a `field` holding NULL.
- * \par Exception safety
- * No-throw guarantee.
- */
- field() = default;
- /**
- * \brief Copy constructor.
- * \par Exception safety
- * Strong guarantee. Internal allocations may throw.
- */
- field(const field&) = default;
- /**
- * \brief Move constructor.
- * \par Exception safety
- * No-throw guarantee.
- *
- * \par Object lifetimes
- * All references into `other` are invalidated, including the ones obtained by calling
- * get_xxx, as_xxx and \ref field::operator field_view().
- */
- field(field&& other) = default;
- /**
- * \brief Copy assignment.
- * \par Exception safety
- * Basic guarantee. Internal allocations may throw.
- *
- * \par Object lifetimes
- * Invalidates references obtained by as_xxx and get_xxx functions,
- * but not the ones obtained by \ref field::operator field_view().
- */
- field& operator=(const field&) = default;
- /**
- * \brief Move assignment.
- * \par Exception safety
- * No-throw guarantee.
- *
- * \par Object lifetimes
- * Invalidates references to `*this` obtained by as_xxx and get_xxx functions,
- * but not the ones obtained by \ref field::operator field_view(). All references into `other`
- * are invalidated, including the ones obtained by calling get_xxx, as_xxx and
- * \ref field::operator field_view().
- */
- field& operator=(field&& other) = default;
- /// Destructor.
- ~field() = default;
- /**
- * \brief Constructs a `field` holding NULL.
- * \details
- * Caution: `field(NULL)` will __NOT__ match this overload. It will try to construct
- * a `string_view` from a NULL C string, causing undefined behavior.
- *
- * \par Exception safety
- * No-throw guarantee.
- */
- explicit field(std::nullptr_t) noexcept {}
- /**
- * \brief Constructs a `field` holding an `int64`.
- * \par Exception safety
- * No-throw guarantee.
- */
- explicit field(signed char v) noexcept : repr_(std::int64_t(v)) {}
- /// \copydoc field(signed char)
- explicit field(short v) noexcept : repr_(std::int64_t(v)) {}
- /// \copydoc field(signed char)
- explicit field(int v) noexcept : repr_(std::int64_t(v)) {}
- /// \copydoc field(signed char)
- explicit field(long v) noexcept : repr_(std::int64_t(v)) {}
- /// \copydoc field(signed char)
- explicit field(long long v) noexcept : repr_(std::int64_t(v)) {}
- /**
- * \brief Constructs a `field` holding an `uint64`.
- * \par Exception safety
- * No-throw guarantee.
- */
- explicit field(unsigned char v) noexcept : repr_(std::uint64_t(v)) {}
- /// \copydoc field(unsigned char)
- explicit field(unsigned short v) noexcept : repr_(std::uint64_t(v)) {}
- /// \copydoc field(unsigned char)
- explicit field(unsigned int v) noexcept : repr_(std::uint64_t(v)) {}
- /// \copydoc field(unsigned char)
- explicit field(unsigned long v) noexcept : repr_(std::uint64_t(v)) {}
- /// \copydoc field(unsigned char)
- explicit field(unsigned long long v) noexcept : repr_(std::uint64_t(v)) {}
- /**
- * \brief Constructors from character types would incorrectly construct a `field` holding an integer,
- * so they are not allowed.
- */
- explicit field(char) = delete;
- /// \copydoc field(char)
- explicit field(wchar_t) = delete;
- /// \copydoc field(char)
- explicit field(char16_t) = delete;
- /// \copydoc field(char)
- explicit field(char32_t) = delete;
- #ifdef __cpp_char8_t
- /// \copydoc field(char)
- explicit field(char8_t) = delete;
- #endif
- /**
- * \brief Constructs a `field` holding a string.
- * \par Exception safety
- * Strong guarantee. Internal allocations may throw.
- */
- explicit field(const std::string& v) : repr_(v) {}
- /**
- * \brief Constructs a `field` holding a string.
- * \details v is moved into an internal `std::string` object.
- * \par Exception safety
- * No-throw guarantee.
- */
- explicit field(std::string&& v) noexcept : repr_(std::move(v)) {}
- /// \copydoc field(const std::string&)
- explicit field(const char* v) : repr_(boost::variant2::in_place_type_t<std::string>(), v) {}
- /// \copydoc field(const std::string&)
- explicit field(string_view v) : repr_(boost::variant2::in_place_type_t<std::string>(), v) {}
- #ifndef BOOST_NO_CXX17_HDR_STRING_VIEW
- /// \copydoc field(const std::string&)
- explicit field(std::string_view v) : repr_(boost::variant2::in_place_type_t<std::string>(), v) {}
- #endif
- /**
- * \brief Constructs a `field` holding a `blob`.
- * \details v is moved into an internal `blob` object.
- * \par Exception safety
- * No-throw guarantee.
- */
- explicit field(blob v) noexcept : repr_(std::move(v)) {}
- /**
- * \brief Constructs a `field` holding a `float`.
- * \par Exception safety
- * No-throw guarantee.
- */
- explicit field(float v) noexcept : repr_(v) {}
- /**
- * \brief Constructs a `field` holding a `double`.
- * \par Exception safety
- * No-throw guarantee.
- */
- explicit field(double v) noexcept : repr_(v) {}
- /**
- * \brief Constructs a `field` holding a `date`.
- * \par Exception safety
- * No-throw guarantee.
- */
- explicit field(const date& v) noexcept : repr_(v) {}
- /**
- * \brief Constructs a `field` holding a `datetime`.
- * \par Exception safety
- * No-throw guarantee.
- */
- explicit field(const datetime& v) noexcept : repr_(v) {}
- /**
- * \brief Constructs a `field` holding a `time`.
- * \par Exception safety
- * No-throw guarantee.
- */
- explicit field(const time& v) noexcept : repr_(v) {}
- /**
- * \brief Constructs a `field` from a \ref field_view.
- * \details The resulting `field` has the same kind and value as the original `field_view`.
- *
- * \par Exception safety
- * Strong guarantee. Internal allocations may throw.
- *
- * \par Object lifetimes
- * The resulting `field` is guaranteed to be valid even after `v` becomes invalid.
- */
- field(const field_view& v) { from_view(v); }
- /**
- * \brief Replaces `*this` with a `NULL`, changing the kind to `null` and destroying any
- * previous contents.
- *
- * \par Exception safety
- * No-throw guarantee.
- *
- * \par Object lifetimes
- * Invalidates references obtained by as_xxx and get_xxx functions,
- * but not the ones obtained by \ref field::operator field_view().
- */
- field& operator=(std::nullptr_t) noexcept
- {
- repr_.data.emplace<detail::field_impl::null_t>();
- return *this;
- }
- /**
- * \brief Replaces `*this` with `v`, changing the kind to `int64` and destroying any
- * previous contents.
- *
- * \par Exception safety
- * No-throw guarantee.
- *
- * \par Object lifetimes
- * Invalidates references obtained by as_xxx and get_xxx functions,
- * but not the ones obtained by \ref field::operator field_view().
- */
- field& operator=(signed char v) noexcept
- {
- repr_.data.emplace<std::int64_t>(v);
- return *this;
- }
- /// \copydoc operator=(signed char)
- field& operator=(short v) noexcept
- {
- repr_.data.emplace<std::int64_t>(v);
- return *this;
- }
- /// \copydoc operator=(signed char)
- field& operator=(int v) noexcept
- {
- repr_.data.emplace<std::int64_t>(v);
- return *this;
- }
- /// \copydoc operator=(signed char)
- field& operator=(long v) noexcept
- {
- repr_.data.emplace<std::int64_t>(v);
- return *this;
- }
- /// \copydoc operator=(signed char)
- field& operator=(long long v) noexcept
- {
- repr_.data.emplace<std::int64_t>(v);
- return *this;
- }
- /**
- * \brief Replaces `*this` with `v`, changing the kind to `uint64` and destroying any
- * previous contents.
- *
- * \par Exception safety
- * No-throw guarantee.
- *
- * \par Object lifetimes
- * Invalidates references obtained by as_xxx and get_xxx functions,
- * but not the ones obtained by \ref field::operator field_view().
- */
- field& operator=(unsigned char v) noexcept
- {
- repr_.data.emplace<std::uint64_t>(v);
- return *this;
- }
- /// \copydoc operator=(unsigned char)
- field& operator=(unsigned short v) noexcept
- {
- repr_.data.emplace<std::uint64_t>(v);
- return *this;
- }
- /// \copydoc operator=(unsigned char)
- field& operator=(unsigned int v) noexcept
- {
- repr_.data.emplace<std::uint64_t>(v);
- return *this;
- }
- /// \copydoc operator=(unsigned char)
- field& operator=(unsigned long v) noexcept
- {
- repr_.data.emplace<std::uint64_t>(v);
- return *this;
- }
- /// \copydoc operator=(unsigned char)
- field& operator=(unsigned long long v) noexcept
- {
- repr_.data.emplace<std::uint64_t>(v);
- return *this;
- }
- /**
- * \brief Assignments from character types would incorrectly assign an integer,
- * so they are not allowed.
- */
- field& operator=(char) = delete;
- /// \copydoc operator=(char)
- field& operator=(wchar_t) = delete;
- /// \copydoc operator=(char)
- field& operator=(char16_t) = delete;
- /// \copydoc operator=(char)
- field& operator=(char32_t) = delete;
- #ifdef __cpp_char8_t
- /// \copydoc operator=(char)
- field& operator=(char8_t) = delete;
- #endif
- /**
- * \brief Replaces `*this` with `v`, changing the kind to `string` and destroying any previous
- * contents.
- *
- * \par Exception safety
- * Basic guarantee. Internal allocations may throw.
- *
- * \par Object lifetimes
- * Invalidates references obtained by as_xxx and get_xxx functions,
- * but not the ones obtained by \ref field::operator field_view().
- */
- field& operator=(const std::string& v)
- {
- repr_.data.emplace<std::string>(v);
- return *this;
- }
- /// \copydoc operator=(const std::string&)
- field& operator=(std::string&& v)
- {
- repr_.data.emplace<std::string>(std::move(v));
- return *this;
- }
- /// \copydoc operator=(const std::string&)
- field& operator=(const char* v)
- {
- repr_.data.emplace<std::string>(v);
- return *this;
- }
- /// \copydoc operator=(const std::string&)
- field& operator=(string_view v)
- {
- repr_.data.emplace<std::string>(v);
- return *this;
- }
- #ifndef BOOST_NO_CXX17_HDR_STRING_VIEW
- /// \copydoc operator=(const std::string&)
- field& operator=(std::string_view v)
- {
- repr_.data.emplace<std::string>(v);
- return *this;
- }
- #endif
- /**
- * \brief Replaces `*this` with `v`, changing the kind to `blob` and destroying any
- * previous contents.
- *
- * \par Exception safety
- * Basic guarantee. Internal allocations may throw.
- *
- * \par Object lifetimes
- * Invalidates references obtained by as_xxx and get_xxx functions,
- * but not the ones obtained by \ref field::operator field_view().
- */
- field& operator=(blob v)
- {
- repr_.data.emplace<blob>(std::move(v));
- return *this;
- }
- /**
- * \brief Replaces `*this` with `v`, changing the kind to `float_` and destroying any
- * previous contents.
- *
- * \par Exception safety
- * No-throw guarantee.
- *
- * \par Object lifetimes
- * Invalidates references obtained by as_xxx and get_xxx functions,
- * but not the ones obtained by \ref field::operator field_view().
- */
- field& operator=(float v) noexcept
- {
- repr_.data.emplace<float>(v);
- return *this;
- }
- /**
- * \brief Replaces `*this` with `v`, changing the kind to `double` and destroying any
- * previous contents.
- *
- * \par Exception safety
- * No-throw guarantee.
- *
- * \par Object lifetimes
- * Invalidates references obtained by as_xxx and get_xxx functions,
- * but not the ones obtained by \ref field::operator field_view().
- */
- field& operator=(double v) noexcept
- {
- repr_.data.emplace<double>(v);
- return *this;
- }
- /**
- * \brief Replaces `*this` with `v`, changing the kind to `date` and destroying any
- * previous contents.
- *
- * \par Exception safety
- * No-throw guarantee.
- *
- * \par Object lifetimes
- * Invalidates references obtained by as_xxx and get_xxx functions,
- * but not the ones obtained by \ref field::operator field_view().
- */
- field& operator=(const date& v) noexcept
- {
- repr_.data.emplace<date>(v);
- return *this;
- }
- /**
- * \brief Replaces `*this` with `v`, changing the kind to `datetime` and destroying any
- * previous contents.
- *
- * \par Exception safety
- * No-throw guarantee.
- *
- * \par Object lifetimes
- * Invalidates references obtained by as_xxx and get_xxx functions,
- * but not the ones obtained by \ref field::operator field_view().
- */
- field& operator=(const datetime& v) noexcept
- {
- repr_.data.emplace<datetime>(v);
- return *this;
- }
- /**
- * \brief Replaces `*this` with `v`, changing the kind to `time` and destroying any
- * previous contents.
- *
- * \par Exception safety
- * No-throw guarantee.
- *
- * \par Object lifetimes
- * Invalidates references obtained by as_xxx and get_xxx functions, but not
- */
- field& operator=(const time& v) noexcept
- {
- repr_.data.emplace<time>(v);
- return *this;
- }
- /**
- * \brief Replaces `*this` with `v`, changing the kind to `v.kind()` and destroying any previous
- * contents.
- *
- * \par Exception safety
- * Basic guarantee. Internal allocations may throw.
- *
- * \par Object lifetimes
- * Invalidates references to `*this` obtained by as_xxx and get_xxx functions, but not
- * the ones obtained by \ref field::operator field_view().
- *\n
- * `*this` is guaranteed to be valid even after `v` becomes invalid.
- */
- field& operator=(const field_view& v)
- {
- from_view(v);
- return *this;
- }
- /**
- * \brief Returns the type of the value this `field` is holding.
- * \par Exception safety
- * No-throw guarantee.
- */
- field_kind kind() const noexcept { return repr_.kind(); }
- /**
- * \brief Returns whether this `field` is holding a `NULL` value.
- * \par Exception safety
- * No-throw guarantee.
- */
- bool is_null() const noexcept { return kind() == field_kind::null; }
- /**
- * \brief Returns whether this `field` is holding a `int64` value.
- * \par Exception safety
- * No-throw guarantee.
- */
- bool is_int64() const noexcept { return kind() == field_kind::int64; }
- /**
- * \brief Returns whether this `field` is holding a `uint64` value.
- * \par Exception safety
- * No-throw guarantee.
- */
- bool is_uint64() const noexcept { return kind() == field_kind::uint64; }
- /**
- * \brief Returns whether this `field` is holding a string value.
- * \par Exception safety
- * No-throw guarantee.
- */
- bool is_string() const noexcept { return kind() == field_kind::string; }
- /**
- * \brief Returns whether this `field` is holding a blob value.
- * \par Exception safety
- * No-throw guarantee.
- */
- bool is_blob() const noexcept { return kind() == field_kind::blob; }
- /**
- * \brief Returns whether this `field` is holding a `float` value.
- * \par Exception safety
- * No-throw guarantee.
- */
- bool is_float() const noexcept { return kind() == field_kind::float_; }
- /**
- * \brief Returns whether this `field` is holding a `double` value.
- * \par Exception safety
- * No-throw guarantee.
- */
- bool is_double() const noexcept { return kind() == field_kind::double_; }
- /**
- * \brief Returns whether this `field` is holding a `date` value.
- * \par Exception safety
- * No-throw guarantee.
- */
- bool is_date() const noexcept { return kind() == field_kind::date; }
- /**
- * \brief Returns whether this `field` is holding a `datetime` value.
- * \par Exception safety
- * No-throw guarantee.
- */
- bool is_datetime() const noexcept { return kind() == field_kind::datetime; }
- /**
- * \brief Returns whether this `field` is holding a `time` value.
- * \par Exception safety
- * No-throw guarantee.
- */
- bool is_time() const noexcept { return kind() == field_kind::time; }
- /**
- * \brief Retrieves a reference to the underlying `std::int64_t` value or throws an exception.
- * \par Exception safety
- * Strong guarantee. Throws on type mismatch.
- * \throws bad_field_access If `!this->is_int64()`
- *
- * \par Object lifetimes
- * The returned reference is valid as long as `*this` is alive and no function that invalidates
- * references is called on `*this`.
- */
- const std::int64_t& as_int64() const { return repr_.as<std::int64_t>(); }
- /**
- * \brief Retrieves a reference to the underlying `std::uint64_t` value or throws an exception.
- * \par Exception safety
- * Strong guarantee. Throws on type mismatch.
- * \throws bad_field_access If `!this->is_uint64()`
- *
- * \par Object lifetimes
- * The returned reference is valid as long as `*this` is alive and no function that invalidates
- * references is called on `*this`.
- */
- const std::uint64_t& as_uint64() const { return repr_.as<std::uint64_t>(); }
- /**
- * \brief Retrieves a reference to the underlying `std::string` value or throws an exception.
- * \par Exception safety
- * Strong guarantee. Throws on type mismatch.
- * \throws bad_field_access If `!this->is_string()`
- *
- * \par Object lifetimes
- * The returned reference is valid as long as `*this` is alive and no function that invalidates
- * references is called on `*this`.
- */
- const std::string& as_string() const { return repr_.as<std::string>(); }
- /**
- * \brief Retrieves a reference to the underlying `blob` value or throws an exception.
- * \par Exception safety
- * Strong guarantee. Throws on type mismatch.
- * \throws bad_field_access If `!this->is_blob()`
- *
- * \par Object lifetimes
- * The returned reference is valid as long as `*this` is alive and no function that invalidates
- * references is called on `*this`.
- */
- const blob& as_blob() const { return repr_.as<blob>(); }
- /**
- * \brief Retrieves a reference to the underlying `float` value or throws an exception.
- * \par Exception safety
- * Strong guarantee. Throws on type mismatch.
- * \throws bad_field_access If `!this->is_float()`
- *
- * \par Object lifetimes
- * The returned reference is valid as long as `*this` is alive and no function that invalidates
- * references is called on `*this`.
- */
- const float& as_float() const { return repr_.as<float>(); }
- /**
- * \brief Retrieves a reference to the underlying `double` value or throws an exception.
- * \par Exception safety
- * Strong guarantee. Throws on type mismatch.
- * \throws bad_field_access If `!this->is_double()`
- *
- * \par Object lifetimes
- * The returned reference is valid as long as `*this` is alive and no function that invalidates
- * references is called on `*this`.
- */
- const double& as_double() const { return repr_.as<double>(); }
- /**
- * \brief Retrieves a reference to the underlying `date` value or throws an exception.
- * \par Exception safety
- * Strong guarantee. Throws on type mismatch.
- * \throws bad_field_access If `!this->is_date()`
- *
- * \par Object lifetimes
- * The returned reference is valid as long as `*this` is alive and no function that invalidates
- * references is called on `*this`.
- */
- const date& as_date() const { return repr_.as<date>(); }
- /**
- * \brief Retrieves a reference to the underlying `datetime` value or throws an exception.
- * \par Exception safety
- * Strong guarantee. Throws on type mismatch.
- * \throws bad_field_access If `!this->is_datetime()`
- *
- * \par Object lifetimes
- * The returned reference is valid as long as `*this` is alive and no function that invalidates
- * references is called on `*this`.
- */
- const datetime& as_datetime() const { return repr_.as<datetime>(); }
- /**
- * \brief Retrieves a reference to the underlying `time` value or throws an exception.
- * \par Exception safety
- * Strong guarantee. Throws on type mismatch.
- * \throws bad_field_access If `!this->is_time()`
- *
- * \par Object lifetimes
- * The returned reference is valid as long as `*this` is alive and no function that invalidates
- * references is called on `*this`.
- */
- const time& as_time() const { return repr_.as<time>(); }
- /// \copydoc as_int64
- std::int64_t& as_int64() { return repr_.as<std::int64_t>(); }
- /// \copydoc as_uint64
- std::uint64_t& as_uint64() { return repr_.as<std::uint64_t>(); }
- /// \copydoc as_string
- std::string& as_string() { return repr_.as<std::string>(); }
- /// \copydoc as_blob
- blob& as_blob() { return repr_.as<blob>(); }
- /// \copydoc as_float
- float& as_float() { return repr_.as<float>(); }
- /// \copydoc as_double
- double& as_double() { return repr_.as<double>(); }
- /// \copydoc as_date
- date& as_date() { return repr_.as<date>(); }
- /// \copydoc as_datetime
- datetime& as_datetime() { return repr_.as<datetime>(); }
- /// \copydoc as_time
- time& as_time() { return repr_.as<time>(); }
- /**
- * \brief Retrieves a reference to the underlying `std::int64_t` value (unchecked access).
- * \par Preconditions
- * `this->is_int64() == true` (if violated, results in undefined behavior).
- *
- * \par Exception safety
- * No-throw guarantee.
- *
- * \par Object lifetimes
- * The returned reference is valid as long as `*this` is alive and no function that invalidates
- * references is called on `*this`.
- */
- const std::int64_t& get_int64() const noexcept { return repr_.get<std::int64_t>(); }
- /**
- * \brief Retrieves a reference to the underlying `std::uint64_t` value (unchecked access).
- * \par Preconditions
- * `this->is_uint64() == true` (if violated, results in undefined behavior).
- *
- * \par Exception safety
- * No-throw guarantee.
- *
- * \par Object lifetimes
- * The returned reference is valid as long as `*this` is alive and no function that invalidates
- * references is called on `*this`.
- */
- const std::uint64_t& get_uint64() const noexcept { return repr_.get<std::uint64_t>(); }
- /**
- * \brief Retrieves a reference to the underlying `std::string` value (unchecked access).
- * \par Preconditions
- * `this->is_string() == true` (if violated, results in undefined behavior).
- *
- * \par Exception safety
- * No-throw guarantee.
- *
- * \par Object lifetimes
- * The returned reference is valid as long as `*this` is alive and no function that invalidates
- * references is called on `*this`.
- */
- const std::string& get_string() const noexcept { return repr_.get<std::string>(); }
- /**
- * \brief Retrieves a reference to the underlying `blob` value (unchecked access).
- * \par Preconditions
- * `this->is_blob() == true` (if violated, results in undefined behavior).
- *
- * \par Exception safety
- * No-throw guarantee.
- *
- * \par Object lifetimes
- * The returned reference is valid as long as `*this` is alive and no function that invalidates
- * references is called on `*this`.
- */
- const blob& get_blob() const noexcept { return repr_.get<blob>(); }
- /**
- * \brief Retrieves a reference to the underlying `float` value (unchecked access).
- * \par Preconditions
- * `this->is_float() == true` (if violated, results in undefined behavior).
- *
- * \par Exception safety
- * No-throw guarantee.
- *
- * \par Object lifetimes
- * The returned reference is valid as long as `*this` is alive and no function that invalidates
- * references is called on `*this`.
- */
- const float& get_float() const noexcept { return repr_.get<float>(); }
- /**
- * \brief Retrieves a reference to the underlying `double` value (unchecked access).
- * \par Preconditions
- * `this->is_double() == true` (if violated, results in undefined behavior).
- *
- * \par Exception safety
- * No-throw guarantee.
- *
- * \par Object lifetimes
- * The returned reference is valid as long as `*this` is alive and no function that invalidates
- * references is called on `*this`.
- */
- const double& get_double() const noexcept { return repr_.get<double>(); }
- /**
- * \brief Retrieves a reference to the underlying `date` value (unchecked access).
- * \par Preconditions
- * `this->is_date() == true` (if violated, results in undefined behavior).
- *
- * \par Exception safety
- * No-throw guarantee.
- *
- * \par Object lifetimes
- * The returned reference is valid as long as `*this` is alive and no function that invalidates
- * references is called on `*this`.
- */
- const date& get_date() const noexcept { return repr_.get<date>(); }
- /**
- * \brief Retrieves a reference to the underlying `datetime` value (unchecked access).
- * \par Preconditions
- * `this->is_datetime() == true` (if violated, results in undefined behavior).
- *
- * \par Exception safety
- * No-throw guarantee.
- *
- * \par Object lifetimes
- * The returned reference is valid as long as `*this` is alive and no function that invalidates
- * references is called on `*this`.
- */
- const datetime& get_datetime() const noexcept { return repr_.get<datetime>(); }
- /**
- * \brief Retrieves a reference to the underlying `time` value (unchecked access).
- * \par Preconditions
- * `this->is_time() == true` (if violated, results in undefined behavior).
- *
- * \par Exception safety
- * No-throw guarantee.
- *
- * \par Object lifetimes
- * The returned reference is valid as long as `*this` is alive and no function that invalidates
- * references is called on `*this`.
- */
- const time& get_time() const noexcept { return repr_.get<time>(); }
- /// \copydoc get_int64
- std::int64_t& get_int64() noexcept { return repr_.get<std::int64_t>(); }
- /// \copydoc get_uint64
- std::uint64_t& get_uint64() noexcept { return repr_.get<std::uint64_t>(); }
- /// \copydoc get_string
- std::string& get_string() noexcept { return repr_.get<std::string>(); }
- /// \copydoc get_blob
- blob& get_blob() noexcept { return repr_.get<blob>(); }
- /// \copydoc get_float
- float& get_float() noexcept { return repr_.get<float>(); }
- /// \copydoc get_double
- double& get_double() noexcept { return repr_.get<double>(); }
- /// \copydoc get_date
- date& get_date() noexcept { return repr_.get<date>(); }
- /// \copydoc get_datetime
- datetime& get_datetime() noexcept { return repr_.get<datetime>(); }
- /// \copydoc get_time
- time& get_time() noexcept { return repr_.get<time>(); }
- /**
- * \brief Constructs a \ref field_view pointing to `*this`.
- * \details The resulting `field_view` has the same kind and value as `*this`.
- *
- * \par Exception safety
- * No-throw guarantee.
- *
- * \par Object lifetimes
- * The returned object acts as a
- * reference to `*this`, and will be valid as long as `*this` is alive.
- */
- inline operator field_view() const noexcept { return field_view(&repr_); }
- private:
- detail::field_impl repr_;
- BOOST_MYSQL_DECL
- void from_view(const field_view& v);
- };
- /**
- * \relates field
- * \brief Tests for equality.
- * \details The same considerations as \ref field_view::operator== apply.
- *
- * \par Exception safety
- * No-throw guarantee.
- */
- inline bool operator==(const field& lhs, const field& rhs) noexcept
- {
- return field_view(lhs) == field_view(rhs);
- }
- /**
- * \relates field
- * \brief Tests for inequality.
- * \par Exception safety
- * No-throw guarantee.
- */
- inline bool operator!=(const field& lhs, const field& rhs) noexcept { return !(lhs == rhs); }
- /**
- * \relates field
- * \brief Tests for equality.
- * \details The same considerations as \ref field_view::operator== apply.
- *
- * \par Exception safety
- * No-throw guarantee.
- */
- inline bool operator==(const field_view& lhs, const field& rhs) noexcept { return lhs == field_view(rhs); }
- /**
- * \relates field
- * \brief Tests for inequality.
- * \par Exception safety
- * No-throw guarantee.
- */
- inline bool operator!=(const field_view& lhs, const field& rhs) noexcept { return !(lhs == rhs); }
- /**
- * \relates field
- * \brief Tests for equality.
- * \details The same considerations as \ref field_view::operator== apply.
- * \par Exception safety
- * No-throw guarantee.
- */
- inline bool operator==(const field& lhs, const field_view& rhs) noexcept { return field_view(lhs) == rhs; }
- /**
- * \relates field
- * \brief Tests for inequality.
- * \par Exception safety
- * No-throw guarantee.
- */
- inline bool operator!=(const field& lhs, const field_view& rhs) noexcept { return !(lhs == rhs); }
- /**
- * \relates field
- * \brief Streams a `field`.
- */
- BOOST_MYSQL_DECL
- std::ostream& operator<<(std::ostream& os, const field& v);
- } // namespace mysql
- } // namespace boost
- #ifdef BOOST_MYSQL_HEADER_ONLY
- #include <boost/mysql/impl/field.ipp>
- #endif
- #endif
|