Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:42:39

0001 //
0002 // Copyright (c) 2019-2023 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
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 // Breaks a circular dependency between field_view and field
0030 struct field_impl
0031 {
0032     using null_t = boost::variant2::monostate;
0033 
0034     using variant_type = boost::variant2::variant<
0035         null_t,         // Any of the below when the value is NULL
0036         std::int64_t,   // signed TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT
0037         std::uint64_t,  // unsigned TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT, YEAR, BIT
0038         std::string,    // CHAR, VARCHAR,  TEXT (all sizes), , ENUM,
0039                         // SET, DECIMAL
0040         blob,           // BINARY, VARBINARY, BLOB (all sizes), GEOMETRY
0041         float,          // FLOAT
0042         double,         // DOUBLE
0043         date,           // DATE
0044         datetime,       // DATETIME, TIMESTAMP
0045         time            // 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 }  // namespace detail
0094 }  // namespace mysql
0095 }  // namespace boost
0096 
0097 #endif