Class basic_value

Synopsis

#include <toml/value.hpp>

template<typename Comment, // discard/preserve_comment
         template<typename ...> class Table = std::unordered_map,
         template<typename ...> class Array = std::vector>
class basic_value

Description

No description yet.

Mentioned in

Methods

basic_value overload
~basic_value
as_array overload

Mentioned in

as_boolean overload

Mentioned in

as_floating overload

Mentioned in

as_integer overload

Mentioned in

as_local_date overload

Mentioned in

as_local_datetime overload

Mentioned in

as_local_time overload

Mentioned in

as_offset_datetime overload

Mentioned in

as_string overload

Mentioned in

as_table overload

Mentioned in

at overload

Mentioned in

  • Getting Started / at()
cast overload
comments overload
contains

Mentioned in

count

Mentioned in

emplace_back
is overload

Mentioned in

is_array

Mentioned in

is_boolean

Mentioned in

is_floating

Mentioned in

is_integer

Mentioned in

is_local_date

Mentioned in

is_local_datetime

Mentioned in

is_local_time

Mentioned in

is_offset_datetime

Mentioned in

is_string

Mentioned in

is_table

Mentioned in

is_uninitialized

Mentioned in

location
operator= overload
operator[] overload
push_back overload
size
type

Mentioned in

Source

Lines 168-1750 in toml/value.hpp.

template<typename Comment, // discard/preserve_comment
         template<typename ...> class Table = std::unordered_map,
         template<typename ...> class Array = std::vector>
class basic_value
{
    template<typename T, typename U>
    static void assigner(T& dst, U&& v)
    {
        const auto tmp = ::new(std::addressof(dst)) T(std::forward<U>(v));
        assert(tmp == std::addressof(dst));
        (void)tmp;
    }

    using region_base = detail::region_base;

    template<typename C, template<typename ...> class T,
             template<typename ...> class A>
    friend class basic_value;

  public:

    using comment_type         = Comment;
    using key_type             = ::toml::key;
    using value_type           = basic_value<comment_type, Table, Array>;
    using boolean_type         = ::toml::boolean;
    using integer_type         = ::toml::integer;
    using floating_type        = ::toml::floating;
    using string_type          = ::toml::string;
    using local_time_type      = ::toml::local_time;
    using local_date_type      = ::toml::local_date;
    using local_datetime_type  = ::toml::local_datetime;
    using offset_datetime_type = ::toml::offset_datetime;
    using array_type           = Array<value_type>;
    using table_type           = Table<key_type, value_type>;

  public:

    basic_value() noexcept
        : type_(value_t::empty),
          region_info_(std::make_shared<region_base>(region_base{}))
    {}
    ~basic_value() noexcept {this->cleanup();}

    basic_value(const basic_value& v)
        : type_(v.type()), region_info_(v.region_info_), comments_(v.comments_)
    {
        switch(v.type())
        {
            case value_t::boolean        : assigner(boolean_        , v.boolean_        ); break;
            case value_t::integer        : assigner(integer_        , v.integer_        ); break;
            case value_t::floating       : assigner(floating_       , v.floating_       ); break;
            case value_t::string         : assigner(string_         , v.string_         ); break;
            case value_t::offset_datetime: assigner(offset_datetime_, v.offset_datetime_); break;
            case value_t::local_datetime : assigner(local_datetime_ , v.local_datetime_ ); break;
            case value_t::local_date     : assigner(local_date_     , v.local_date_     ); break;
            case value_t::local_time     : assigner(local_time_     , v.local_time_     ); break;
            case value_t::array          : assigner(array_          , v.array_          ); break;
            case value_t::table          : assigner(table_          , v.table_          ); break;
            default: break;
        }
    }
    basic_value(basic_value&& v)
        : type_(v.type()), region_info_(std::move(v.region_info_)),
          comments_(std::move(v.comments_))
    {
        switch(this->type_) // here this->type_ is already initialized
        {
            case value_t::boolean        : assigner(boolean_        , std::move(v.boolean_        )); break;
            case value_t::integer        : assigner(integer_        , std::move(v.integer_        )); break;
            case value_t::floating       : assigner(floating_       , std::move(v.floating_       )); break;
            case value_t::string         : assigner(string_         , std::move(v.string_         )); break;
            case value_t::offset_datetime: assigner(offset_datetime_, std::move(v.offset_datetime_)); break;
            case value_t::local_datetime : assigner(local_datetime_ , std::move(v.local_datetime_ )); break;
            case value_t::local_date     : assigner(local_date_     , std::move(v.local_date_     )); break;
            case value_t::local_time     : assigner(local_time_     , std::move(v.local_time_     )); break;
            case value_t::array          : assigner(array_          , std::move(v.array_          )); break;
            case value_t::table          : assigner(table_          , std::move(v.table_          )); break;
            default: break;
        }
    }
    basic_value& operator=(const basic_value& v)
    {
        this->cleanup();
        this->region_info_ = v.region_info_;
        this->comments_ = v.comments_;
        this->type_ = v.type();
        switch(this->type_)
        {
            case value_t::boolean        : assigner(boolean_        , v.boolean_        ); break;
            case value_t::integer        : assigner(integer_        , v.integer_        ); break;
            case value_t::floating       : assigner(floating_       , v.floating_       ); break;
            case value_t::string         : assigner(string_         , v.string_         ); break;
            case value_t::offset_datetime: assigner(offset_datetime_, v.offset_datetime_); break;
            case value_t::local_datetime : assigner(local_datetime_ , v.local_datetime_ ); break;
            case value_t::local_date     : assigner(local_date_     , v.local_date_     ); break;
            case value_t::local_time     : assigner(local_time_     , v.local_time_     ); break;
            case value_t::array          : assigner(array_          , v.array_          ); break;
            case value_t::table          : assigner(table_          , v.table_          ); break;
            default: break;
        }
        return *this;
    }
    basic_value& operator=(basic_value&& v)
    {
        this->cleanup();
        this->region_info_ = std::move(v.region_info_);
        this->comments_ = std::move(v.comments_);
        this->type_ = v.type();
        switch(this->type_)
        {
            case value_t::boolean        : assigner(boolean_        , std::move(v.boolean_        )); break;
            case value_t::integer        : assigner(integer_        , std::move(v.integer_        )); break;
            case value_t::floating       : assigner(floating_       , std::move(v.floating_       )); break;
            case value_t::string         : assigner(string_         , std::move(v.string_         )); break;
            case value_t::offset_datetime: assigner(offset_datetime_, std::move(v.offset_datetime_)); break;
            case value_t::local_datetime : assigner(local_datetime_ , std::move(v.local_datetime_ )); break;
            case value_t::local_date     : assigner(local_date_     , std::move(v.local_date_     )); break;
            case value_t::local_time     : assigner(local_time_     , std::move(v.local_time_     )); break;
            case value_t::array          : assigner(array_          , std::move(v.array_          )); break;
            case value_t::table          : assigner(table_          , std::move(v.table_          )); break;
            default: break;
        }
        return *this;
    }

    // overwrite comments ----------------------------------------------------

    basic_value(const basic_value& v, std::vector<std::string> com)
        : type_(v.type()), region_info_(v.region_info_),
          comments_(std::move(com))
    {
        switch(v.type())
        {
            case value_t::boolean        : assigner(boolean_        , v.boolean_        ); break;
            case value_t::integer        : assigner(integer_        , v.integer_        ); break;
            case value_t::floating       : assigner(floating_       , v.floating_       ); break;
            case value_t::string         : assigner(string_         , v.string_         ); break;
            case value_t::offset_datetime: assigner(offset_datetime_, v.offset_datetime_); break;
            case value_t::local_datetime : assigner(local_datetime_ , v.local_datetime_ ); break;
            case value_t::local_date     : assigner(local_date_     , v.local_date_     ); break;
            case value_t::local_time     : assigner(local_time_     , v.local_time_     ); break;
            case value_t::array          : assigner(array_          , v.array_          ); break;
            case value_t::table          : assigner(table_          , v.table_          ); break;
            default: break;
        }
    }

    basic_value(basic_value&& v, std::vector<std::string> com)
        : type_(v.type()), region_info_(std::move(v.region_info_)),
          comments_(std::move(com))
    {
        switch(this->type_) // here this->type_ is already initialized
        {
            case value_t::boolean        : assigner(boolean_        , std::move(v.boolean_        )); break;
            case value_t::integer        : assigner(integer_        , std::move(v.integer_        )); break;
            case value_t::floating       : assigner(floating_       , std::move(v.floating_       )); break;
            case value_t::string         : assigner(string_         , std::move(v.string_         )); break;
            case value_t::offset_datetime: assigner(offset_datetime_, std::move(v.offset_datetime_)); break;
            case value_t::local_datetime : assigner(local_datetime_ , std::move(v.local_datetime_ )); break;
            case value_t::local_date     : assigner(local_date_     , std::move(v.local_date_     )); break;
            case value_t::local_time     : assigner(local_time_     , std::move(v.local_time_     )); break;
            case value_t::array          : assigner(array_          , std::move(v.array_          )); break;
            case value_t::table          : assigner(table_          , std::move(v.table_          )); break;
            default: break;
        }
    }

    // -----------------------------------------------------------------------
    // conversion between different basic_values.
    template<typename C,
             template<typename ...> class T,
             template<typename ...> class A>
    basic_value(const basic_value<C, T, A>& v)
        : type_(v.type()), region_info_(v.region_info_), comments_(v.comments())
    {
        switch(v.type())
        {
            case value_t::boolean        : assigner(boolean_        , v.boolean_        ); break;
            case value_t::integer        : assigner(integer_        , v.integer_        ); break;
            case value_t::floating       : assigner(floating_       , v.floating_       ); break;
            case value_t::string         : assigner(string_         , v.string_         ); break;
            case value_t::offset_datetime: assigner(offset_datetime_, v.offset_datetime_); break;
            case value_t::local_datetime : assigner(local_datetime_ , v.local_datetime_ ); break;
            case value_t::local_date     : assigner(local_date_     , v.local_date_     ); break;
            case value_t::local_time     : assigner(local_time_     , v.local_time_     ); break;
            case value_t::array          :
            {
                array_type tmp(v.as_array(std::nothrow).begin(),
                               v.as_array(std::nothrow).end());
                assigner(array_, std::move(tmp));
                break;
            }
            case value_t::table          :
            {
                table_type tmp(v.as_table(std::nothrow).begin(),
                               v.as_table(std::nothrow).end());
                assigner(table_, std::move(tmp));
                break;
            }
            default: break;
        }
    }
    template<typename C,
             template<typename ...> class T,
             template<typename ...> class A>
    basic_value(const basic_value<C, T, A>& v, std::vector<std::string> com)
        : type_(v.type()), region_info_(v.region_info_),
          comments_(std::move(com))
    {
        switch(v.type())
        {
            case value_t::boolean        : assigner(boolean_        , v.boolean_        ); break;
            case value_t::integer        : assigner(integer_        , v.integer_        ); break;
            case value_t::floating       : assigner(floating_       , v.floating_       ); break;
            case value_t::string         : assigner(string_         , v.string_         ); break;
            case value_t::offset_datetime: assigner(offset_datetime_, v.offset_datetime_); break;
            case value_t::local_datetime : assigner(local_datetime_ , v.local_datetime_ ); break;
            case value_t::local_date     : assigner(local_date_     , v.local_date_     ); break;
            case value_t::local_time     : assigner(local_time_     , v.local_time_     ); break;
            case value_t::array          :
            {
                array_type tmp(v.as_array(std::nothrow).begin(),
                               v.as_array(std::nothrow).end());
                assigner(array_, std::move(tmp));
                break;
            }
            case value_t::table          :
            {
                table_type tmp(v.as_table(std::nothrow).begin(),
                               v.as_table(std::nothrow).end());
                assigner(table_, std::move(tmp));
                break;
            }
            default: break;
        }
    }
    template<typename C,
             template<typename ...> class T,
             template<typename ...> class A>
    basic_value& operator=(const basic_value<C, T, A>& v)
    {
        this->region_info_ = v.region_info_;
        this->comments_    = comment_type(v.comments());
        this->type_        = v.type();
        switch(v.type())
        {
            case value_t::boolean        : assigner(boolean_        , v.boolean_        ); break;
            case value_t::integer        : assigner(integer_        , v.integer_        ); break;
            case value_t::floating       : assigner(floating_       , v.floating_       ); break;
            case value_t::string         : assigner(string_         , v.string_         ); break;
            case value_t::offset_datetime: assigner(offset_datetime_, v.offset_datetime_); break;
            case value_t::local_datetime : assigner(local_datetime_ , v.local_datetime_ ); break;
            case value_t::local_date     : assigner(local_date_     , v.local_date_     ); break;
            case value_t::local_time     : assigner(local_time_     , v.local_time_     ); break;
            case value_t::array          :
            {
                array_type tmp(v.as_array(std::nothrow).begin(),
                               v.as_array(std::nothrow).end());
                assigner(array_, std::move(tmp));
                break;
            }
            case value_t::table          :
            {
                table_type tmp(v.as_table(std::nothrow).begin(),
                               v.as_table(std::nothrow).end());
                assigner(table_, std::move(tmp));
                break;
            }
            default: break;
        }
        return *this;
    }

    // boolean ==============================================================

    basic_value(boolean b)
        : type_(value_t::boolean),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        assigner(this->boolean_, b);
    }
    basic_value& operator=(boolean b)
    {
        this->cleanup();
        this->type_ = value_t::boolean;
        this->region_info_ = std::make_shared<region_base>(region_base{});
        assigner(this->boolean_, b);
        return *this;
    }
    basic_value(boolean b, std::vector<std::string> com)
        : type_(value_t::boolean),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        assigner(this->boolean_, b);
    }

    // integer ==============================================================

    template<typename T, typename std::enable_if<detail::conjunction<
        std::is_integral<T>, detail::negation<std::is_same<T, boolean>>>::value,
        std::nullptr_t>::type = nullptr>
    basic_value(T i)
        : type_(value_t::integer),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        assigner(this->integer_, static_cast<integer>(i));
    }

    template<typename T, typename std::enable_if<detail::conjunction<
        std::is_integral<T>, detail::negation<std::is_same<T, boolean>>>::value,
        std::nullptr_t>::type = nullptr>
    basic_value& operator=(T i)
    {
        this->cleanup();
        this->type_ = value_t::integer;
        this->region_info_ = std::make_shared<region_base>(region_base{});
        assigner(this->integer_, static_cast<integer>(i));
        return *this;
    }

    template<typename T, typename std::enable_if<detail::conjunction<
        std::is_integral<T>, detail::negation<std::is_same<T, boolean>>>::value,
        std::nullptr_t>::type = nullptr>
    basic_value(T i, std::vector<std::string> com)
        : type_(value_t::integer),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        assigner(this->integer_, static_cast<integer>(i));
    }

    // floating =============================================================

    template<typename T, typename std::enable_if<
        std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
    basic_value(T f)
        : type_(value_t::floating),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        assigner(this->floating_, static_cast<floating>(f));
    }


    template<typename T, typename std::enable_if<
        std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
    basic_value& operator=(T f)
    {
        this->cleanup();
        this->type_ = value_t::floating;
        this->region_info_ = std::make_shared<region_base>(region_base{});
        assigner(this->floating_, static_cast<floating>(f));
        return *this;
    }

    template<typename T, typename std::enable_if<
        std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
    basic_value(T f, std::vector<std::string> com)
        : type_(value_t::floating),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        assigner(this->floating_, f);
    }

    // string ===============================================================

    basic_value(toml::string s)
        : type_(value_t::string),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        assigner(this->string_, std::move(s));
    }
    basic_value& operator=(toml::string s)
    {
        this->cleanup();
        this->type_ = value_t::string ;
        this->region_info_ = std::make_shared<region_base>(region_base{});
        assigner(this->string_, s);
        return *this;
    }
    basic_value(toml::string s, std::vector<std::string> com)
        : type_(value_t::string),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        assigner(this->string_, std::move(s));
    }

    basic_value(std::string s)
        : type_(value_t::string),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        assigner(this->string_, toml::string(std::move(s)));
    }
    basic_value& operator=(std::string s)
    {
        this->cleanup();
        this->type_ = value_t::string ;
        this->region_info_ = std::make_shared<region_base>(region_base{});
        assigner(this->string_, toml::string(std::move(s)));
        return *this;
    }
    basic_value(std::string s, string_t kind)
        : type_(value_t::string),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        assigner(this->string_, toml::string(std::move(s), kind));
    }
    basic_value(std::string s, std::vector<std::string> com)
        : type_(value_t::string),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        assigner(this->string_, toml::string(std::move(s)));
    }
    basic_value(std::string s, string_t kind, std::vector<std::string> com)
        : type_(value_t::string),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        assigner(this->string_, toml::string(std::move(s), kind));
    }

    basic_value(const char* s)
        : type_(value_t::string),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        assigner(this->string_, toml::string(std::string(s)));
    }
    basic_value& operator=(const char* s)
    {
        this->cleanup();
        this->type_ = value_t::string ;
        this->region_info_ = std::make_shared<region_base>(region_base{});
        assigner(this->string_, toml::string(std::string(s)));
        return *this;
    }
    basic_value(const char* s, string_t kind)
        : type_(value_t::string),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        assigner(this->string_, toml::string(std::string(s), kind));
    }
    basic_value(const char* s, std::vector<std::string> com)
        : type_(value_t::string),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        assigner(this->string_, toml::string(std::string(s)));
    }
    basic_value(const char* s, string_t kind, std::vector<std::string> com)
        : type_(value_t::string),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        assigner(this->string_, toml::string(std::string(s), kind));
    }

#if __cplusplus >= 201703L
    basic_value(std::string_view s)
        : type_(value_t::string),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        assigner(this->string_, toml::string(s));
    }
    basic_value& operator=(std::string_view s)
    {
        this->cleanup();
        this->type_ = value_t::string ;
        this->region_info_ = std::make_shared<region_base>(region_base{});
        assigner(this->string_, toml::string(s));
        return *this;
    }
    basic_value(std::string_view s, std::vector<std::string> com)
        : type_(value_t::string),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        assigner(this->string_, toml::string(s));
    }
    basic_value(std::string_view s, string_t kind)
        : type_(value_t::string),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        assigner(this->string_, toml::string(s, kind));
    }
    basic_value(std::string_view s, string_t kind, std::vector<std::string> com)
        : type_(value_t::string),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        assigner(this->string_, toml::string(s, kind));
    }
#endif

    // local date ===========================================================

    basic_value(const local_date& ld)
        : type_(value_t::local_date),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        assigner(this->local_date_, ld);
    }
    basic_value& operator=(const local_date& ld)
    {
        this->cleanup();
        this->type_ = value_t::local_date;
        this->region_info_ = std::make_shared<region_base>(region_base{});
        assigner(this->local_date_, ld);
        return *this;
    }
    basic_value(const local_date& ld, std::vector<std::string> com)
        : type_(value_t::local_date),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        assigner(this->local_date_, ld);
    }

    // local time ===========================================================

    basic_value(const local_time& lt)
        : type_(value_t::local_time),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        assigner(this->local_time_, lt);
    }
    basic_value(const local_time& lt, std::vector<std::string> com)
        : type_(value_t::local_time),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        assigner(this->local_time_, lt);
    }
    basic_value& operator=(const local_time& lt)
    {
        this->cleanup();
        this->type_ = value_t::local_time;
        this->region_info_ = std::make_shared<region_base>(region_base{});
        assigner(this->local_time_, lt);
        return *this;
    }

    template<typename Rep, typename Period>
    basic_value(const std::chrono::duration<Rep, Period>& dur)
        : type_(value_t::local_time),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        assigner(this->local_time_, local_time(dur));
    }
    template<typename Rep, typename Period>
    basic_value(const std::chrono::duration<Rep, Period>& dur,
                std::vector<std::string> com)
        : type_(value_t::local_time),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        assigner(this->local_time_, local_time(dur));
    }
    template<typename Rep, typename Period>
    basic_value& operator=(const std::chrono::duration<Rep, Period>& dur)
    {
        this->cleanup();
        this->type_ = value_t::local_time;
        this->region_info_ = std::make_shared<region_base>(region_base{});
        assigner(this->local_time_, local_time(dur));
        return *this;
    }

    // local datetime =======================================================

    basic_value(const local_datetime& ldt)
        : type_(value_t::local_datetime),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        assigner(this->local_datetime_, ldt);
    }
    basic_value(const local_datetime& ldt, std::vector<std::string> com)
        : type_(value_t::local_datetime),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        assigner(this->local_datetime_, ldt);
    }
    basic_value& operator=(const local_datetime& ldt)
    {
        this->cleanup();
        this->type_ = value_t::local_datetime;
        this->region_info_ = std::make_shared<region_base>(region_base{});
        assigner(this->local_datetime_, ldt);
        return *this;
    }

    // offset datetime ======================================================

    basic_value(const offset_datetime& odt)
        : type_(value_t::offset_datetime),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        assigner(this->offset_datetime_, odt);
    }
    basic_value(const offset_datetime& odt, std::vector<std::string> com)
        : type_(value_t::offset_datetime),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        assigner(this->offset_datetime_, odt);
    }
    basic_value& operator=(const offset_datetime& odt)
    {
        this->cleanup();
        this->type_ = value_t::offset_datetime;
        this->region_info_ = std::make_shared<region_base>(region_base{});
        assigner(this->offset_datetime_, odt);
        return *this;
    }
    basic_value(const std::chrono::system_clock::time_point& tp)
        : type_(value_t::offset_datetime),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        assigner(this->offset_datetime_, offset_datetime(tp));
    }
    basic_value(const std::chrono::system_clock::time_point& tp,
                std::vector<std::string> com)
        : type_(value_t::offset_datetime),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        assigner(this->offset_datetime_, offset_datetime(tp));
    }
    basic_value& operator=(const std::chrono::system_clock::time_point& tp)
    {
        this->cleanup();
        this->type_ = value_t::offset_datetime;
        this->region_info_ = std::make_shared<region_base>(region_base{});
        assigner(this->offset_datetime_, offset_datetime(tp));
        return *this;
    }

    // array ================================================================

    basic_value(const array_type& ary)
        : type_(value_t::array),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        assigner(this->array_, ary);
    }
    basic_value(const array_type& ary, std::vector<std::string> com)
        : type_(value_t::array),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        assigner(this->array_, ary);
    }
    basic_value& operator=(const array_type& ary)
    {
        this->cleanup();
        this->type_ = value_t::array ;
        this->region_info_ = std::make_shared<region_base>(region_base{});
        assigner(this->array_, ary);
        return *this;
    }

    // array (initializer_list) ----------------------------------------------

    template<typename T, typename std::enable_if<
            std::is_convertible<T, value_type>::value,
        std::nullptr_t>::type = nullptr>
    basic_value(std::initializer_list<T> list)
        : type_(value_t::array),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        array_type ary(list.begin(), list.end());
        assigner(this->array_, std::move(ary));
    }
    template<typename T, typename std::enable_if<
            std::is_convertible<T, value_type>::value,
        std::nullptr_t>::type = nullptr>
    basic_value(std::initializer_list<T> list, std::vector<std::string> com)
        : type_(value_t::array),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        array_type ary(list.begin(), list.end());
        assigner(this->array_, std::move(ary));
    }
    template<typename T, typename std::enable_if<
            std::is_convertible<T, value_type>::value,
        std::nullptr_t>::type = nullptr>
    basic_value& operator=(std::initializer_list<T> list)
    {
        this->cleanup();
        this->type_ = value_t::array;
        this->region_info_ = std::make_shared<region_base>(region_base{});

        array_type ary(list.begin(), list.end());
        assigner(this->array_, std::move(ary));
        return *this;
    }

    // array (STL Containers) ------------------------------------------------

    template<typename T, typename std::enable_if<detail::conjunction<
            detail::negation<std::is_same<T, array_type>>,
            detail::is_container<T>
        >::value, std::nullptr_t>::type = nullptr>
    basic_value(const T& list)
        : type_(value_t::array),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        static_assert(std::is_convertible<typename T::value_type, value_type>::value,
            "elements of a container should be convertible to toml::value");

        array_type ary(list.size());
        std::copy(list.begin(), list.end(), ary.begin());
        assigner(this->array_, std::move(ary));
    }
    template<typename T, typename std::enable_if<detail::conjunction<
            detail::negation<std::is_same<T, array_type>>,
            detail::is_container<T>
        >::value, std::nullptr_t>::type = nullptr>
    basic_value(const T& list, std::vector<std::string> com)
        : type_(value_t::array),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        static_assert(std::is_convertible<typename T::value_type, value_type>::value,
            "elements of a container should be convertible to toml::value");

        array_type ary(list.size());
        std::copy(list.begin(), list.end(), ary.begin());
        assigner(this->array_, std::move(ary));
    }
    template<typename T, typename std::enable_if<detail::conjunction<
            detail::negation<std::is_same<T, array_type>>,
            detail::is_container<T>
        >::value, std::nullptr_t>::type = nullptr>
    basic_value& operator=(const T& list)
    {
        static_assert(std::is_convertible<typename T::value_type, value_type>::value,
            "elements of a container should be convertible to toml::value");

        this->cleanup();
        this->type_ = value_t::array;
        this->region_info_ = std::make_shared<region_base>(region_base{});

        array_type ary(list.size());
        std::copy(list.begin(), list.end(), ary.begin());
        assigner(this->array_, std::move(ary));
        return *this;
    }

    // table ================================================================

    basic_value(const table_type& tab)
        : type_(value_t::table),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        assigner(this->table_, tab);
    }
    basic_value(const table_type& tab, std::vector<std::string> com)
        : type_(value_t::table),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        assigner(this->table_, tab);
    }
    basic_value& operator=(const table_type& tab)
    {
        this->cleanup();
        this->type_ = value_t::table;
        this->region_info_ = std::make_shared<region_base>(region_base{});
        assigner(this->table_, tab);
        return *this;
    }

    // initializer-list ------------------------------------------------------

    basic_value(std::initializer_list<std::pair<key, basic_value>> list)
        : type_(value_t::table),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        table_type tab;
        for(const auto& elem : list) {tab[elem.first] = elem.second;}
        assigner(this->table_, std::move(tab));
    }

    basic_value(std::initializer_list<std::pair<key, basic_value>> list,
                std::vector<std::string> com)
        : type_(value_t::table),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        table_type tab;
        for(const auto& elem : list) {tab[elem.first] = elem.second;}
        assigner(this->table_, std::move(tab));
    }
    basic_value& operator=(std::initializer_list<std::pair<key, basic_value>> list)
    {
        this->cleanup();
        this->type_ = value_t::table;
        this->region_info_ = std::make_shared<region_base>(region_base{});

        table_type tab;
        for(const auto& elem : list) {tab[elem.first] = elem.second;}
        assigner(this->table_, std::move(tab));
        return *this;
    }

    // other table-like -----------------------------------------------------

    template<typename Map, typename std::enable_if<detail::conjunction<
            detail::negation<std::is_same<Map, table_type>>,
            detail::is_map<Map>
        >::value, std::nullptr_t>::type = nullptr>
    basic_value(const Map& mp)
        : type_(value_t::table),
          region_info_(std::make_shared<region_base>(region_base{}))
    {
        table_type tab;
        for(const auto& elem : mp) {tab[elem.first] = elem.second;}
        assigner(this->table_, std::move(tab));
    }
    template<typename Map, typename std::enable_if<detail::conjunction<
            detail::negation<std::is_same<Map, table_type>>,
            detail::is_map<Map>
        >::value, std::nullptr_t>::type = nullptr>
    basic_value(const Map& mp, std::vector<std::string> com)
        : type_(value_t::table),
          region_info_(std::make_shared<region_base>(region_base{})),
          comments_(std::move(com))
    {
        table_type tab;
        for(const auto& elem : mp) {tab[elem.first] = elem.second;}
        assigner(this->table_, std::move(tab));
    }
    template<typename Map, typename std::enable_if<detail::conjunction<
            detail::negation<std::is_same<Map, table_type>>,
            detail::is_map<Map>
        >::value, std::nullptr_t>::type = nullptr>
    basic_value& operator=(const Map& mp)
    {
        this->cleanup();
        this->type_ = value_t::table;
        this->region_info_ = std::make_shared<region_base>(region_base{});

        table_type tab;
        for(const auto& elem : mp) {tab[elem.first] = elem.second;}
        assigner(this->table_, std::move(tab));
        return *this;
    }

    // user-defined =========================================================

    // convert using into_toml() method -------------------------------------

    template<typename T, typename std::enable_if<
        detail::has_into_toml_method<T>::value, std::nullptr_t>::type = nullptr>
    basic_value(const T& ud): basic_value(ud.into_toml()) {}

    template<typename T, typename std::enable_if<
        detail::has_into_toml_method<T>::value, std::nullptr_t>::type = nullptr>
    basic_value(const T& ud, std::vector<std::string> com)
        : basic_value(ud.into_toml(), std::move(com))
    {}
    template<typename T, typename std::enable_if<
        detail::has_into_toml_method<T>::value, std::nullptr_t>::type = nullptr>
    basic_value& operator=(const T& ud)
    {
        *this = ud.into_toml();
        return *this;
    }

    // convert using into<T> struct -----------------------------------------

    template<typename T, std::size_t S = sizeof(::toml::into<T>)>
    basic_value(const T& ud): basic_value(::toml::into<T>::into_toml(ud)) {}
    template<typename T, std::size_t S = sizeof(::toml::into<T>)>
    basic_value(const T& ud, std::vector<std::string> com)
        : basic_value(::toml::into<T>::into_toml(ud), std::move(com))
    {}
    template<typename T, std::size_t S = sizeof(::toml::into<T>)>
    basic_value& operator=(const T& ud)
    {
        *this = ::toml::into<T>::into_toml(ud);
        return *this;
    }

    // for internal use ------------------------------------------------------
    //
    // Those constructors take detail::region that contains parse result.

    basic_value(boolean b, detail::region reg, std::vector<std::string> cm)
        : type_(value_t::boolean),
          region_info_(std::make_shared<detail::region>(std::move(reg))),
          comments_(std::move(cm))
    {
        assigner(this->boolean_, b);
    }
    template<typename T, typename std::enable_if<
        detail::conjunction<
            std::is_integral<T>, detail::negation<std::is_same<T, boolean>>
        >::value, std::nullptr_t>::type = nullptr>
    basic_value(T i, detail::region reg, std::vector<std::string> cm)
        : type_(value_t::integer),
          region_info_(std::make_shared<detail::region>(std::move(reg))),
          comments_(std::move(cm))
    {
        assigner(this->integer_, static_cast<integer>(i));
    }
    template<typename T, typename std::enable_if<
        std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
    basic_value(T f, detail::region reg, std::vector<std::string> cm)
        : type_(value_t::floating),
          region_info_(std::make_shared<detail::region>(std::move(reg))),
          comments_(std::move(cm))
    {
        assigner(this->floating_, static_cast<floating>(f));
    }
    basic_value(toml::string s, detail::region reg,
                std::vector<std::string> cm)
        : type_(value_t::string),
          region_info_(std::make_shared<detail::region>(std::move(reg))),
          comments_(std::move(cm))
    {
        assigner(this->string_, std::move(s));
    }
    basic_value(const local_date& ld, detail::region reg,
                std::vector<std::string> cm)
        : type_(value_t::local_date),
          region_info_(std::make_shared<detail::region>(std::move(reg))),
          comments_(std::move(cm))
    {
        assigner(this->local_date_, ld);
    }
    basic_value(const local_time& lt, detail::region reg,
                std::vector<std::string> cm)
        : type_(value_t::local_time),
          region_info_(std::make_shared<detail::region>(std::move(reg))),
          comments_(std::move(cm))
    {
        assigner(this->local_time_, lt);
    }
    basic_value(const local_datetime& ldt, detail::region reg,
                std::vector<std::string> cm)
        : type_(value_t::local_datetime),
          region_info_(std::make_shared<detail::region>(std::move(reg))),
          comments_(std::move(cm))
    {
        assigner(this->local_datetime_, ldt);
    }
    basic_value(const offset_datetime& odt, detail::region reg,
                std::vector<std::string> cm)
        : type_(value_t::offset_datetime),
          region_info_(std::make_shared<detail::region>(std::move(reg))),
          comments_(std::move(cm))
    {
        assigner(this->offset_datetime_, odt);
    }
    basic_value(const array_type& ary, detail::region reg,
                std::vector<std::string> cm)
        : type_(value_t::array),
          region_info_(std::make_shared<detail::region>(std::move(reg))),
          comments_(std::move(cm))
    {
        assigner(this->array_, ary);
    }
    basic_value(const table_type& tab, detail::region reg,
                std::vector<std::string> cm)
        : type_(value_t::table),
          region_info_(std::make_shared<detail::region>(std::move(reg))),
          comments_(std::move(cm))
    {
        assigner(this->table_, tab);
    }

    template<typename T, typename std::enable_if<
        detail::is_exact_toml_type<T, value_type>::value,
        std::nullptr_t>::type = nullptr>
    basic_value(std::pair<T, detail::region> parse_result, std::vector<std::string> com)
        : basic_value(std::move(parse_result.first),
                      std::move(parse_result.second),
                      std::move(com))
    {}

    // type checking and casting ============================================

    template<typename T, typename std::enable_if<
        detail::is_exact_toml_type<T, value_type>::value,
        std::nullptr_t>::type = nullptr>
    bool is() const noexcept
    {
        return detail::type_to_enum<T, value_type>::value == this->type_;
    }
    bool is(value_t t) const noexcept {return t == this->type_;}

    bool is_uninitialized()   const noexcept {return this->is(value_t::empty          );}
    bool is_boolean()         const noexcept {return this->is(value_t::boolean        );}
    bool is_integer()         const noexcept {return this->is(value_t::integer        );}
    bool is_floating()        const noexcept {return this->is(value_t::floating       );}
    bool is_string()          const noexcept {return this->is(value_t::string         );}
    bool is_offset_datetime() const noexcept {return this->is(value_t::offset_datetime);}
    bool is_local_datetime()  const noexcept {return this->is(value_t::local_datetime );}
    bool is_local_date()      const noexcept {return this->is(value_t::local_date     );}
    bool is_local_time()      const noexcept {return this->is(value_t::local_time     );}
    bool is_array()           const noexcept {return this->is(value_t::array          );}
    bool is_table()           const noexcept {return this->is(value_t::table          );}

    value_t type() const noexcept {return type_;}

    template<value_t T>
    typename detail::enum_to_type<T, value_type>::type&       cast() &
    {
        if(this->type_ != T)
        {
            detail::throw_bad_cast<T>("toml::value::cast: ", this->type_, *this);
        }
        return detail::switch_cast<T>::invoke(*this);
    }
    template<value_t T>
    typename detail::enum_to_type<T, value_type>::type const& cast() const&
    {
        if(this->type_ != T)
        {
            detail::throw_bad_cast<T>("toml::value::cast: ", this->type_, *this);
        }
        return detail::switch_cast<T>::invoke(*this);
    }
    template<value_t T>
    typename detail::enum_to_type<T, value_type>::type&&      cast() &&
    {
        if(this->type_ != T)
        {
            detail::throw_bad_cast<T>("toml::value::cast: ", this->type_, *this);
        }
        return detail::switch_cast<T>::invoke(std::move(*this));
    }

    // ------------------------------------------------------------------------
    // nothrow version

    boolean         const& as_boolean        (const std::nothrow_t&) const& noexcept {return this->boolean_;}
    integer         const& as_integer        (const std::nothrow_t&) const& noexcept {return this->integer_;}
    floating        const& as_floating       (const std::nothrow_t&) const& noexcept {return this->floating_;}
    string          const& as_string         (const std::nothrow_t&) const& noexcept {return this->string_;}
    offset_datetime const& as_offset_datetime(const std::nothrow_t&) const& noexcept {return this->offset_datetime_;}
    local_datetime  const& as_local_datetime (const std::nothrow_t&) const& noexcept {return this->local_datetime_;}
    local_date      const& as_local_date     (const std::nothrow_t&) const& noexcept {return this->local_date_;}
    local_time      const& as_local_time     (const std::nothrow_t&) const& noexcept {return this->local_time_;}
    array_type      const& as_array          (const std::nothrow_t&) const& noexcept {return this->array_.value();}
    table_type      const& as_table          (const std::nothrow_t&) const& noexcept {return this->table_.value();}

    boolean        & as_boolean        (const std::nothrow_t&) & noexcept {return this->boolean_;}
    integer        & as_integer        (const std::nothrow_t&) & noexcept {return this->integer_;}
    floating       & as_floating       (const std::nothrow_t&) & noexcept {return this->floating_;}
    string         & as_string         (const std::nothrow_t&) & noexcept {return this->string_;}
    offset_datetime& as_offset_datetime(const std::nothrow_t&) & noexcept {return this->offset_datetime_;}
    local_datetime & as_local_datetime (const std::nothrow_t&) & noexcept {return this->local_datetime_;}
    local_date     & as_local_date     (const std::nothrow_t&) & noexcept {return this->local_date_;}
    local_time     & as_local_time     (const std::nothrow_t&) & noexcept {return this->local_time_;}
    array_type     & as_array          (const std::nothrow_t&) & noexcept {return this->array_.value();}
    table_type     & as_table          (const std::nothrow_t&) & noexcept {return this->table_.value();}

    boolean        && as_boolean        (const std::nothrow_t&) && noexcept {return std::move(this->boolean_);}
    integer        && as_integer        (const std::nothrow_t&) && noexcept {return std::move(this->integer_);}
    floating       && as_floating       (const std::nothrow_t&) && noexcept {return std::move(this->floating_);}
    string         && as_string         (const std::nothrow_t&) && noexcept {return std::move(this->string_);}
    offset_datetime&& as_offset_datetime(const std::nothrow_t&) && noexcept {return std::move(this->offset_datetime_);}
    local_datetime && as_local_datetime (const std::nothrow_t&) && noexcept {return std::move(this->local_datetime_);}
    local_date     && as_local_date     (const std::nothrow_t&) && noexcept {return std::move(this->local_date_);}
    local_time     && as_local_time     (const std::nothrow_t&) && noexcept {return std::move(this->local_time_);}
    array_type     && as_array          (const std::nothrow_t&) && noexcept {return std::move(this->array_.value());}
    table_type     && as_table          (const std::nothrow_t&) && noexcept {return std::move(this->table_.value());}

    // ========================================================================
    // throw version
    // ------------------------------------------------------------------------
    // const reference {{{

    boolean const& as_boolean() const&
    {
        if(this->type_ != value_t::boolean)
        {
            detail::throw_bad_cast<value_t::boolean>(
                    "toml::value::as_boolean(): ", this->type_, *this);
        }
        return this->boolean_;
    }
    integer const& as_integer() const&
    {
        if(this->type_ != value_t::integer)
        {
            detail::throw_bad_cast<value_t::integer>(
                    "toml::value::as_integer(): ", this->type_, *this);
        }
        return this->integer_;
    }
    floating const& as_floating() const&
    {
        if(this->type_ != value_t::floating)
        {
            detail::throw_bad_cast<value_t::floating>(
                    "toml::value::as_floating(): ", this->type_, *this);
        }
        return this->floating_;
    }
    string const& as_string() const&
    {
        if(this->type_ != value_t::string)
        {
            detail::throw_bad_cast<value_t::string>(
                    "toml::value::as_string(): ", this->type_, *this);
        }
        return this->string_;
    }
    offset_datetime const& as_offset_datetime() const&
    {
        if(this->type_ != value_t::offset_datetime)
        {
            detail::throw_bad_cast<value_t::offset_datetime>(
                    "toml::value::as_offset_datetime(): ", this->type_, *this);
        }
        return this->offset_datetime_;
    }
    local_datetime const& as_local_datetime() const&
    {
        if(this->type_ != value_t::local_datetime)
        {
            detail::throw_bad_cast<value_t::local_datetime>(
                    "toml::value::as_local_datetime(): ", this->type_, *this);
        }
        return this->local_datetime_;
    }
    local_date const& as_local_date() const&
    {
        if(this->type_ != value_t::local_date)
        {
            detail::throw_bad_cast<value_t::local_date>(
                    "toml::value::as_local_date(): ", this->type_, *this);
        }
        return this->local_date_;
    }
    local_time const& as_local_time() const&
    {
        if(this->type_ != value_t::local_time)
        {
            detail::throw_bad_cast<value_t::local_time>(
                    "toml::value::as_local_time(): ", this->type_, *this);
        }
        return this->local_time_;
    }
    array_type const& as_array() const&
    {
        if(this->type_ != value_t::array)
        {
            detail::throw_bad_cast<value_t::array>(
                    "toml::value::as_array(): ", this->type_, *this);
        }
        return this->array_.value();
    }
    table_type const& as_table() const&
    {
        if(this->type_ != value_t::table)
        {
            detail::throw_bad_cast<value_t::table>(
                    "toml::value::as_table(): ", this->type_, *this);
        }
        return this->table_.value();
    }
    // }}}
    // ------------------------------------------------------------------------
    // nonconst reference {{{

    boolean & as_boolean() &
    {
        if(this->type_ != value_t::boolean)
        {
            detail::throw_bad_cast<value_t::boolean>(
                    "toml::value::as_boolean(): ", this->type_, *this);
        }
        return this->boolean_;
    }
    integer & as_integer() &
    {
        if(this->type_ != value_t::integer)
        {
            detail::throw_bad_cast<value_t::integer>(
                    "toml::value::as_integer(): ", this->type_, *this);
        }
        return this->integer_;
    }
    floating & as_floating() &
    {
        if(this->type_ != value_t::floating)
        {
            detail::throw_bad_cast<value_t::floating>(
                    "toml::value::as_floating(): ", this->type_, *this);
        }
        return this->floating_;
    }
    string & as_string() &
    {
        if(this->type_ != value_t::string)
        {
            detail::throw_bad_cast<value_t::string>(
                    "toml::value::as_string(): ", this->type_, *this);
        }
        return this->string_;
    }
    offset_datetime & as_offset_datetime() &
    {
        if(this->type_ != value_t::offset_datetime)
        {
            detail::throw_bad_cast<value_t::offset_datetime>(
                    "toml::value::as_offset_datetime(): ", this->type_, *this);
        }
        return this->offset_datetime_;
    }
    local_datetime & as_local_datetime() &
    {
        if(this->type_ != value_t::local_datetime)
        {
            detail::throw_bad_cast<value_t::local_datetime>(
                    "toml::value::as_local_datetime(): ", this->type_, *this);
        }
        return this->local_datetime_;
    }
    local_date & as_local_date() &
    {
        if(this->type_ != value_t::local_date)
        {
            detail::throw_bad_cast<value_t::local_date>(
                    "toml::value::as_local_date(): ", this->type_, *this);
        }
        return this->local_date_;
    }
    local_time & as_local_time() &
    {
        if(this->type_ != value_t::local_time)
        {
            detail::throw_bad_cast<value_t::local_time>(
                    "toml::value::as_local_time(): ", this->type_, *this);
        }
        return this->local_time_;
    }
    array_type & as_array() &
    {
        if(this->type_ != value_t::array)
        {
            detail::throw_bad_cast<value_t::array>(
                    "toml::value::as_array(): ", this->type_, *this);
        }
        return this->array_.value();
    }
    table_type & as_table() &
    {
        if(this->type_ != value_t::table)
        {
            detail::throw_bad_cast<value_t::table>(
                    "toml::value::as_table(): ", this->type_, *this);
        }
        return this->table_.value();
    }

    // }}}
    // ------------------------------------------------------------------------
    // rvalue reference {{{

    boolean && as_boolean() &&
    {
        if(this->type_ != value_t::boolean)
        {
            detail::throw_bad_cast<value_t::boolean>(
                    "toml::value::as_boolean(): ", this->type_, *this);
        }
        return std::move(this->boolean_);
    }
    integer && as_integer() &&
    {
        if(this->type_ != value_t::integer)
        {
            detail::throw_bad_cast<value_t::integer>(
                    "toml::value::as_integer(): ", this->type_, *this);
        }
        return std::move(this->integer_);
    }
    floating && as_floating() &&
    {
        if(this->type_ != value_t::floating)
        {
            detail::throw_bad_cast<value_t::floating>(
                    "toml::value::as_floating(): ", this->type_, *this);
        }
        return std::move(this->floating_);
    }
    string && as_string() &&
    {
        if(this->type_ != value_t::string)
        {
            detail::throw_bad_cast<value_t::string>(
                    "toml::value::as_string(): ", this->type_, *this);
        }
        return std::move(this->string_);
    }
    offset_datetime && as_offset_datetime() &&
    {
        if(this->type_ != value_t::offset_datetime)
        {
            detail::throw_bad_cast<value_t::offset_datetime>(
                    "toml::value::as_offset_datetime(): ", this->type_, *this);
        }
        return std::move(this->offset_datetime_);
    }
    local_datetime && as_local_datetime() &&
    {
        if(this->type_ != value_t::local_datetime)
        {
            detail::throw_bad_cast<value_t::local_datetime>(
                    "toml::value::as_local_datetime(): ", this->type_, *this);
        }
        return std::move(this->local_datetime_);
    }
    local_date && as_local_date() &&
    {
        if(this->type_ != value_t::local_date)
        {
            detail::throw_bad_cast<value_t::local_date>(
                    "toml::value::as_local_date(): ", this->type_, *this);
        }
        return std::move(this->local_date_);
    }
    local_time && as_local_time() &&
    {
        if(this->type_ != value_t::local_time)
        {
            detail::throw_bad_cast<value_t::local_time>(
                    "toml::value::as_local_time(): ", this->type_, *this);
        }
        return std::move(this->local_time_);
    }
    array_type && as_array() &&
    {
        if(this->type_ != value_t::array)
        {
            detail::throw_bad_cast<value_t::array>(
                    "toml::value::as_array(): ", this->type_, *this);
        }
        return std::move(this->array_.value());
    }
    table_type && as_table() &&
    {
        if(this->type_ != value_t::table)
        {
            detail::throw_bad_cast<value_t::table>(
                    "toml::value::as_table(): ", this->type_, *this);
        }
        return std::move(this->table_.value());
    }
    // }}}

    // accessors =============================================================
    //
    // may throw type_error or out_of_range
    //
    value_type&       at(const key& k)
    {
        if(!this->is_table())
        {
            detail::throw_bad_cast<value_t::table>(
                "toml::value::at(key): ", this->type_, *this);
        }
        if(this->as_table(std::nothrow).count(k) == 0)
        {
            detail::throw_key_not_found_error(*this, k);
        }
        return this->as_table(std::nothrow).at(k);
    }
    value_type const& at(const key& k) const
    {
        if(!this->is_table())
        {
            detail::throw_bad_cast<value_t::table>(
                "toml::value::at(key): ", this->type_, *this);
        }
        if(this->as_table(std::nothrow).count(k) == 0)
        {
            detail::throw_key_not_found_error(*this, k);
        }
        return this->as_table(std::nothrow).at(k);
    }
    value_type&       operator[](const key& k)
    {
        if(this->is_uninitialized())
        {
            *this = table_type{};
        }
        else if(!this->is_table()) // initialized, but not a table
        {
            detail::throw_bad_cast<value_t::table>(
                "toml::value::operator[](key): ", this->type_, *this);
        }
        return this->as_table(std::nothrow)[k];
    }

    value_type&       at(const std::size_t idx)
    {
        if(!this->is_array())
        {
            detail::throw_bad_cast<value_t::array>(
                "toml::value::at(idx): ", this->type_, *this);
        }
        if(this->as_array(std::nothrow).size() <= idx)
        {
            throw std::out_of_range(detail::format_underline(
                "toml::value::at(idx): no element corresponding to the index", {
                    {this->location(), concat_to_string("the length is ",
                        this->as_array(std::nothrow).size(),
                        ", and the specified index is ", idx)}
                }));
        }
        return this->as_array().at(idx);
    }
    value_type const& at(const std::size_t idx) const
    {
        if(!this->is_array())
        {
            detail::throw_bad_cast<value_t::array>(
                "toml::value::at(idx): ", this->type_, *this);
        }
        if(this->as_array(std::nothrow).size() <= idx)
        {
            throw std::out_of_range(detail::format_underline(
                "toml::value::at(idx): no element corresponding to the index", {
                    {this->location(), concat_to_string("the length is ",
                        this->as_array(std::nothrow).size(),
                        ", and the specified index is ", idx)}
                }));
        }
        return this->as_array(std::nothrow).at(idx);
    }

    value_type&       operator[](const std::size_t idx) noexcept
    {
        // no check...
        return this->as_array(std::nothrow)[idx];
    }
    value_type const& operator[](const std::size_t idx) const noexcept
    {
        // no check...
        return this->as_array(std::nothrow)[idx];
    }

    void push_back(const value_type& x)
    {
        if(!this->is_array())
        {
            detail::throw_bad_cast<value_t::array>(
                "toml::value::push_back(value): ", this->type_, *this);
        }
        this->as_array(std::nothrow).push_back(x);
        return;
    }
    void push_back(value_type&& x)
    {
        if(!this->is_array())
        {
            detail::throw_bad_cast<value_t::array>(
                "toml::value::push_back(value): ", this->type_, *this);
        }
        this->as_array(std::nothrow).push_back(std::move(x));
        return;
    }

    template<typename ... Ts>
    value_type& emplace_back(Ts&& ... args)
    {
        if(!this->is_array())
        {
            detail::throw_bad_cast<value_t::array>(
                "toml::value::emplace_back(...): ", this->type_, *this);
        }
        this->as_array(std::nothrow).emplace_back(std::forward<Ts>(args) ...);
        return this->as_array(std::nothrow).back();
    }

    std::size_t size() const
    {
        switch(this->type_)
        {
            case value_t::array:
            {
                return this->as_array(std::nothrow).size();
            }
            case value_t::table:
            {
                return this->as_table(std::nothrow).size();
            }
            case value_t::string:
            {
                return this->as_string(std::nothrow).str.size();
            }
            default:
            {
                throw type_error(detail::format_underline(
                    "toml::value::size(): bad_cast to container types", {
                        {this->location(),
                         concat_to_string("the actual type is ", this->type_)}
                    }), this->location());
            }
        }
    }

    std::size_t count(const key_type& k) const
    {
        if(!this->is_table())
        {
            detail::throw_bad_cast<value_t::table>(
                "toml::value::count(key): ", this->type_, *this);
        }
        return this->as_table(std::nothrow).count(k);
    }

    bool contains(const key_type& k) const
    {
        if(!this->is_table())
        {
            detail::throw_bad_cast<value_t::table>(
                "toml::value::contains(key): ", this->type_, *this);
        }
        return (this->as_table(std::nothrow).count(k) != 0);
    }

    source_location location() const
    {
        return source_location(this->region_info_.get());
    }

    comment_type const& comments() const noexcept {return this->comments_;}
    comment_type&       comments()       noexcept {return this->comments_;}

  private:

    void cleanup() noexcept
    {
        switch(this->type_)
        {
            case value_t::string  : {string_.~string();       return;}
            case value_t::array   : {array_.~array_storage(); return;}
            case value_t::table   : {table_.~table_storage(); return;}
            default              : return;
        }
    }

    // for error messages
    template<typename Value>
    friend region_base const* detail::get_region(const Value& v);

    template<typename Value>
    friend void detail::change_region(Value& v, detail::region reg);

  private:

    using array_storage = detail::storage<array_type>;
    using table_storage = detail::storage<table_type>;

    value_t type_;
    union
    {
        boolean         boolean_;
        integer         integer_;
        floating        floating_;
        string          string_;
        offset_datetime offset_datetime_;
        local_datetime  local_datetime_;
        local_date      local_date_;
        local_time      local_time_;
        array_storage   array_;
        table_storage   table_;
    };
    std::shared_ptr<region_base> region_info_;
    comment_type                 comments_;
};





Add Discussion as Guest

Log in