File indexing completed on 2025-01-18 09:42:39
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef BOOST_MYSQL_DETAIL_FIELD_IMPL_HPP
0009 #define BOOST_MYSQL_DETAIL_FIELD_IMPL_HPP
0010
0011 #include <boost/mysql/bad_field_access.hpp>
0012 #include <boost/mysql/blob.hpp>
0013 #include <boost/mysql/date.hpp>
0014 #include <boost/mysql/datetime.hpp>
0015 #include <boost/mysql/field_kind.hpp>
0016 #include <boost/mysql/time.hpp>
0017
0018 #include <boost/mp11/algorithm.hpp>
0019 #include <boost/throw_exception.hpp>
0020 #include <boost/variant2/variant.hpp>
0021
0022 #include <string>
0023 #include <type_traits>
0024
0025 namespace boost {
0026 namespace mysql {
0027 namespace detail {
0028
0029
0030 struct field_impl
0031 {
0032 using null_t = boost::variant2::monostate;
0033
0034 using variant_type = boost::variant2::variant<
0035 null_t,
0036 std::int64_t,
0037 std::uint64_t,
0038 std::string,
0039
0040 blob,
0041 float,
0042 double,
0043 date,
0044 datetime,
0045 time
0046 >;
0047
0048 variant_type data;
0049
0050 field_impl() = default;
0051
0052 template <typename... Args>
0053 field_impl(Args&&... args) noexcept(std::is_nothrow_constructible<variant_type, Args...>::value)
0054 : data(std::forward<Args>(args)...)
0055 {
0056 }
0057
0058 field_kind kind() const noexcept { return static_cast<field_kind>(data.index()); }
0059
0060 template <typename T>
0061 const T& as() const
0062 {
0063 const T* res = boost::variant2::get_if<T>(&data);
0064 if (!res)
0065 BOOST_THROW_EXCEPTION(bad_field_access());
0066 return *res;
0067 }
0068
0069 template <typename T>
0070 T& as()
0071 {
0072 T* res = boost::variant2::get_if<T>(&data);
0073 if (!res)
0074 BOOST_THROW_EXCEPTION(bad_field_access());
0075 return *res;
0076 }
0077
0078 template <typename T>
0079 const T& get() const noexcept
0080 {
0081 constexpr auto I = mp11::mp_find<variant_type, T>::value;
0082 return boost::variant2::unsafe_get<I>(data);
0083 }
0084
0085 template <typename T>
0086 T& get() noexcept
0087 {
0088 constexpr auto I = mp11::mp_find<variant_type, T>::value;
0089 return boost::variant2::unsafe_get<I>(data);
0090 }
0091 };
0092
0093 }
0094 }
0095 }
0096
0097 #endif