diff --git a/include/boost/mysql/detail/auxiliar/field_impl.hpp b/include/boost/mysql/detail/auxiliar/field_impl.hpp new file mode 100644 index 00000000..e1f80cd0 --- /dev/null +++ b/include/boost/mysql/detail/auxiliar/field_impl.hpp @@ -0,0 +1,86 @@ +// +// Copyright (c) 2019-2022 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_DETAIL_AUXILIAR_FIELD_IMPL_HPP +#define BOOST_MYSQL_DETAIL_AUXILIAR_FIELD_IMPL_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace mysql { +namespace detail { + +// Breaks a circular dependency between field_view and field +struct field_impl +{ + using null_t = boost::variant2::monostate; + + using variant_type = boost::variant2::variant< + null_t, // Any of the below when the value is NULL + std::int64_t, // signed TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT + std::uint64_t, // unsigned TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT, YEAR, BIT + std::string, // CHAR, VARCHAR, BINARY, VARBINARY, TEXT (all sizes), BLOB (all sizes), ENUM, SET, DECIMAL, GEOMTRY + float, // FLOAT + double, // DOUBLE + date, // DATE + datetime, // DATETIME, TIMESTAMP + time // TIME + >; + + variant_type data; + + field_impl() = default; + + template + field_impl(Arg&& arg) noexcept(std::is_nothrow_constructible::value) : data (std::forward(arg)) {} + + field_kind kind() const noexcept { return static_cast(data.index()); } + + template + const T& as() const + { + const T* res = boost::variant2::get_if(&data); + if (!res) + throw bad_field_access(); + return *res; + } + + template + T& as() + { + T* res = boost::variant2::get_if(&data); + if (!res) + throw bad_field_access(); + return *res; + } + + template + const T& get() const noexcept + { + constexpr auto I = mp11::mp_find::value; + return boost::variant2::unsafe_get(data); + } + + template + T& get() noexcept + { + constexpr auto I = mp11::mp_find::value; + return boost::variant2::unsafe_get(data); + } +}; + +} // detail +} // mysql +} // boost + +#endif diff --git a/include/boost/mysql/detail/auxiliar/string_view_offset.hpp b/include/boost/mysql/detail/auxiliar/string_view_offset.hpp index 3e6f7a42..5f1484f9 100644 --- a/include/boost/mysql/detail/auxiliar/string_view_offset.hpp +++ b/include/boost/mysql/detail/auxiliar/string_view_offset.hpp @@ -29,10 +29,6 @@ public: offset_(offset), size_(size) {} constexpr std::size_t offset() const noexcept { return offset_; } constexpr std::size_t size() const noexcept { return size_; } - constexpr boost::string_view to_string_view(const char* base) const noexcept - { - return boost::string_view(base + offset_, size_); - } constexpr bool operator==(string_view_offset rhs) const noexcept { return offset_ == rhs.offset_ && size_ == rhs.size_; diff --git a/include/boost/mysql/field.hpp b/include/boost/mysql/field.hpp index 375edd84..cef7dd83 100644 --- a/include/boost/mysql/field.hpp +++ b/include/boost/mysql/field.hpp @@ -8,11 +8,12 @@ #ifndef BOOST_MYSQL_FIELD_HPP #define BOOST_MYSQL_FIELD_HPP -#include -#include #include #include #include +#include +#include +#include #include #include #include @@ -32,7 +33,7 @@ public: field& operator=(field&&) = default; ~field() = default; - explicit field(std::nullptr_t) noexcept : repr_(null_t()) {} + explicit field(std::nullptr_t) noexcept {} field(signed char v) noexcept : repr_(std::int64_t(v)) {} field(short v) noexcept : repr_(std::int64_t(v)) {} field(int v) noexcept : repr_(std::int64_t(v)) {} @@ -51,26 +52,26 @@ public: field(const time& v) noexcept : repr_(v) {} field(const field_view& v) { from_view(v); } - field& operator=(std::nullptr_t) noexcept { repr_.emplace(null_t()); return *this; } - field& operator=(signed char v) noexcept { repr_.emplace(v); return *this; } - field& operator=(short v) noexcept { repr_.emplace(v); return *this; } - field& operator=(int v) noexcept { repr_.emplace(v); return *this; } - field& operator=(long v) noexcept { repr_.emplace(v); return *this; } - field& operator=(long long v) noexcept { repr_.emplace(v); return *this; } - field& operator=(unsigned char v) noexcept { repr_.emplace(v); return *this; } - field& operator=(unsigned short v) noexcept { repr_.emplace(v); return *this; } - field& operator=(unsigned int v) noexcept { repr_.emplace(v); return *this; } - field& operator=(unsigned long v) noexcept { repr_.emplace(v); return *this; } - field& operator=(unsigned long long v) noexcept { repr_.emplace(v); return *this; } - field& operator=(std::string v) { repr_.emplace(std::move(v)); return *this; } - field& operator=(float v) noexcept { repr_.emplace(v); return *this; } - field& operator=(double v) noexcept { repr_.emplace(v); return *this; } - field& operator=(const date& v) noexcept { repr_.emplace(v); return *this; } - field& operator=(const datetime& v) noexcept { repr_.emplace(v); return *this; } - field& operator=(const time& v) noexcept { repr_.emplace