318
+ − 1 // Copyright Toru Niina 2017.
+ − 2 // Distributed under the MIT License.
+ − 3 #ifndef TOML11_STORAGE_HPP
+ − 4 #define TOML11_STORAGE_HPP
+ − 5 #include "utility.hpp"
+ − 6
+ − 7 namespace toml
+ − 8 {
+ − 9 namespace detail
+ − 10 {
+ − 11
+ − 12 // this contains pointer and deep-copy the content if copied.
+ − 13 // to avoid recursive pointer.
+ − 14 template<typename T>
+ − 15 struct storage
+ − 16 {
+ − 17 using value_type = T;
+ − 18
+ − 19 explicit storage(value_type const& v): ptr(toml::make_unique<T>(v)) {}
+ − 20 explicit storage(value_type&& v): ptr(toml::make_unique<T>(std::move(v))) {}
+ − 21 ~storage() = default;
+ − 22 storage(const storage& rhs): ptr(toml::make_unique<T>(*rhs.ptr)) {}
+ − 23 storage& operator=(const storage& rhs)
+ − 24 {
+ − 25 this->ptr = toml::make_unique<T>(*rhs.ptr);
+ − 26 return *this;
+ − 27 }
+ − 28 storage(storage&&) = default;
+ − 29 storage& operator=(storage&&) = default;
+ − 30
+ − 31 bool is_ok() const noexcept {return static_cast<bool>(ptr);}
+ − 32
+ − 33 value_type& value() & noexcept {return *ptr;}
+ − 34 value_type const& value() const& noexcept {return *ptr;}
+ − 35 value_type&& value() && noexcept {return std::move(*ptr);}
+ − 36
+ − 37 private:
+ − 38 std::unique_ptr<value_type> ptr;
+ − 39 };
+ − 40
+ − 41 } // detail
+ − 42 } // toml
+ − 43 #endif// TOML11_STORAGE_HPP