From 2696e4e6ba42165d11047aced7fa22fcd18d42b5 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sun, 9 Dec 2018 16:41:45 +0900 Subject: [PATCH] split storage from value also, quit inheritance in storage class --- toml/storage.hpp | 42 ++++++++++++++++++++++++++++++++++++++++++ toml/value.hpp | 34 ++-------------------------------- 2 files changed, 44 insertions(+), 32 deletions(-) create mode 100644 toml/storage.hpp diff --git a/toml/storage.hpp b/toml/storage.hpp new file mode 100644 index 0000000..4e3f114 --- /dev/null +++ b/toml/storage.hpp @@ -0,0 +1,42 @@ +#ifndef TOML11_STORAGE_HPP +#define TOML11_STORAGE_HPP +#include "utility.hpp" + +namespace toml +{ +namespace detail +{ + +// this contains pointer and deep-copy the content if copied. +// to avoid recursive pointer. +template +struct storage +{ + using value_type = T; + + storage(value_type const& v): ptr(toml::make_unique(v)) {} + storage(value_type&& v): ptr(toml::make_unique(std::move(v))) {} + ~storage() = default; + + storage(const storage& rhs): ptr(toml::make_unique(*rhs.ptr)) {} + storage& operator=(const storage& rhs) + { + this->ptr = toml::make_unique(*rhs.ptr); + return *this; + } + storage(storage&&) = default; + storage& operator=(storage&&) = default; + + bool is_ok() const noexcept {return static_cast(ptr);} + + value_type& value() & noexcept {return *ptr;} + value_type const& value() const& noexcept {return *ptr;} + value_type&& value() && noexcept {return std::move(*ptr);} + + private: + std::unique_ptr ptr; +}; + +} // detail +} // toml +#endif// TOML11_STORAGE_HPP diff --git a/toml/value.hpp b/toml/value.hpp index 67d0c6b..1fe42f8 100644 --- a/toml/value.hpp +++ b/toml/value.hpp @@ -3,6 +3,8 @@ #include "traits.hpp" #include "utility.hpp" #include "exception.hpp" +#include "storage.hpp" +#include "region.hpp" #include "types.hpp" #include #include @@ -13,38 +15,6 @@ namespace toml { -namespace detail -{ -struct storage_base -{ - storage_base(): type(toml::value_t::Empty){} - storage_base(toml::value_t t): type(t){} - virtual ~storage_base() = default; - toml::value_t type; -}; - -template -struct storage : public storage_base -{ - static_assert(std::is_same::value || - std::is_same::value, - "toml::detail::storage is for toml::Array or toml::Table!"); - typedef T value_type; - - storage() = default; - ~storage() noexcept override = default; - storage(storage const&) = default; - storage(storage&&) = default; - storage& operator=(storage const&) = default; - storage& operator=(storage&&) = default; - - storage(value_type const& v) : value(v){} - storage(value_type&& v) : value(std::move(v)){} - - value_type value; -}; -} // detail - template struct value_traits {