From 78f25dd66d2d83969f8f5c3a6c51696d7035b3b1 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 21 Apr 2017 13:14:53 +0900 Subject: [PATCH] split files --- toml.hpp | 45 +++++ toml/exception.hpp | 52 ++++++ toml/from_toml.hpp | 108 +++++++++++ toml/get.hpp | 45 +++++ toml/to_toml.hpp | 52 ++++++ toml/traits.hpp | 54 ++++++ toml/utility.hpp | 16 ++ toml/{toml.hpp => value.hpp} | 339 ++--------------------------------- 8 files changed, 389 insertions(+), 322 deletions(-) create mode 100644 toml.hpp create mode 100644 toml/exception.hpp create mode 100644 toml/from_toml.hpp create mode 100644 toml/get.hpp create mode 100644 toml/to_toml.hpp create mode 100644 toml/traits.hpp create mode 100644 toml/utility.hpp rename toml/{toml.hpp => value.hpp} (67%) diff --git a/toml.hpp b/toml.hpp new file mode 100644 index 0000000..045f0ca --- /dev/null +++ b/toml.hpp @@ -0,0 +1,45 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017 Toru Niina + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef TOML_FOR_MODERN_CPP +#define TOML_FOR_MODERN_CPP + +#ifndef __cplusplus +# error "__cplusplus is not defined" +#endif + +#if __cplusplus < 201103L +# error "cannot use C++11" +#endif + +#include "toml/traits.hpp" +#include "toml/utility.hpp" +#include "toml/exception.hpp" +#include "toml/datetime.hpp" +#include "toml/value.hpp" +#include "toml/to_toml.hpp" +#include "toml/from_toml.hpp" +#include "toml/get.hpp" + +#endif// TOML_FOR_MODERN_CPP diff --git a/toml/exception.hpp b/toml/exception.hpp new file mode 100644 index 0000000..7c9f221 --- /dev/null +++ b/toml/exception.hpp @@ -0,0 +1,52 @@ +#ifndef TOML11_EXCEPTION +#define TOML11_EXCEPTION +#include +#include + +namespace toml +{ + +struct exception : public std::exception +{ + public: + virtual ~exception() override = default; + virtual const char* what() const noexcept override {return "";} +}; + +struct syntax_error : public toml::exception +{ + public: + explicit syntax_error(const std::string& what_arg) : what_(what_arg){} + explicit syntax_error(const char* what_arg) : what_(what_arg){} + virtual ~syntax_error() override = default; + virtual const char* what() const noexcept override {return what_.c_str();} + + protected: + std::string what_; +}; + +struct type_error : public toml::exception +{ + public: + explicit type_error(const std::string& what_arg) : what_(what_arg){} + explicit type_error(const char* what_arg) : what_(what_arg){} + virtual ~type_error() override = default; + virtual const char* what() const noexcept override {return what_.c_str();} + + protected: + std::string what_; +}; + +struct internal_error : public toml::exception +{ + public: + explicit internal_error(const std::string& what_arg) : what_(what_arg){} + explicit internal_error(const char* what_arg) : what_(what_arg){} + virtual ~internal_error() override = default; + virtual const char* what() const noexcept override {return what_.c_str();} + protected: + std::string what_; +}; + +} // toml +#endif // TOML_EXCEPTION diff --git a/toml/from_toml.hpp b/toml/from_toml.hpp new file mode 100644 index 0000000..58fb370 --- /dev/null +++ b/toml/from_toml.hpp @@ -0,0 +1,108 @@ +#ifndef TOML11_FROM_TOML +#define TOML11_FROM_TOML +#include "value.hpp" + +namespace toml +{ + +template(), + typename std::enable_if<(vT != toml::value_t::Unknown && + vT != value_t::Empty), std::nullptr_t>::type = nullptr> +void from_toml(T& x, const toml::value& v) +{ + if(v.type() != vT) + throw type_error("from_toml: value type: " + stringize(v.type()) + + std::string(" is not arguemnt type: ") + stringize(vT)); + x = v.cast(); + return; +} + +template(), + typename std::enable_if<(vT == toml::value_t::Unknown) && + (!toml::detail::is_map::value) && + toml::detail::is_container::value, std::nullptr_t>::type = nullptr> +void from_toml(T& x, const toml::value& v) +{ + // TODO the case of x is not dynamic container case + if(v.type() != value_t::Array) + throw type_error("from_toml: value type: " + stringize(v.type()) + + std::string(" is not argument type: Array")); + const auto& ar = v.cast(); + for(const auto& val : ar) + { + typename T::value_type v; + from_toml(v, val); + x.push_back(std::move(v)); + } + return; +} + +template(), + typename std::enable_if<(vT == toml::value_t::Unknown) && + toml::detail::is_map::value, std::nullptr_t>::type = nullptr> +void from_toml(T& x, const toml::value& v) +{ + if(v.type() != value_t::Table) + throw type_error("from_toml: value type: " + stringize(v.type()) + + std::string(" is not argument type: Table")); + x.clear(); + const auto& tb = v.cast(); + for(const auto& kv : tb) + { + x.insert(kv); + } + return; +} + +namespace detail +{ + +template +constexpr toml::value_t determine_castable_type() +{ + return check_type() != toml::value_t::Unknown ? check_type() : + toml::detail::is_map::value ? toml::value_t::Table : + toml::detail::is_container::value ? toml::value_t::Array : + toml::value_t::Unknown; +} + +template +struct from_toml_tie_impl +{ + constexpr static std::size_t index = sizeof...(Ts) - N; + constexpr static toml::value_t type_index = + determine_castable_type< + typename std::tuple_element>::type>(); + + static void invoke(std::tuple tie, const toml::value& v) + { + if(type_index == v.type()) + { + from_toml(std::get(tie), v); + return; + } + return from_toml_tie_impl::invoke(tie, v); + } +}; + +template +struct from_toml_tie_impl<0, Ts...> +{ + static void invoke(std::tuple tie, const toml::value& v) + { + throw type_error("from_toml(tie, value): no match"); + } +}; + +} // detail + +template +void from_toml(std::tuple tie, const toml::value& v) +{ + detail::from_toml_tie_impl::invoke(tie, v); + return; +} + + +} // toml +#endif // TOML11_FROM_TOML diff --git a/toml/get.hpp b/toml/get.hpp new file mode 100644 index 0000000..f93a3fc --- /dev/null +++ b/toml/get.hpp @@ -0,0 +1,45 @@ +#ifndef TOML11_GET +#define TOML11_GET +#include "value.hpp" +#include "from_toml.hpp" + +namespace toml +{ + +template(), + typename std::enable_if<(vT != toml::value_t::Unknown && + vT != value_t::Empty), std::nullptr_t>::type = nullptr> +inline T get(const toml::value& v) +{ + return static_cast(v.cast()); +} + +template(), + typename std::enable_if<(vT == toml::value_t::Unknown) && + (!toml::detail::is_map::value) && + toml::detail::is_container::value, std::nullptr_t>::type = nullptr> +T get(const toml::value& v) +{ + if(v.type() != value_t::Array) + throw type_error("from_toml: value type: " + stringize(v.type()) + + std::string(" is not argument type: Array")); + T tmp; + from_toml(tmp, v); + return tmp; +} + +template(), + typename std::enable_if<(vT == toml::value_t::Unknown) && + toml::detail::is_map::value, std::nullptr_t>::type = nullptr> +T get(const toml::value& v) +{ + if(v.type() != value_t::Table) + throw type_error("from_toml: value type: " + stringize(v.type()) + + std::string(" is not argument type: Table")); + T tmp; + from_toml(tmp, v); + return tmp; +} + +} // toml +#endif// TOML11_GET diff --git a/toml/to_toml.hpp b/toml/to_toml.hpp new file mode 100644 index 0000000..9f373dd --- /dev/null +++ b/toml/to_toml.hpp @@ -0,0 +1,52 @@ +#ifndef TOML11_TO_TOML +#define TOML11_TO_TOML +#include "value.hpp" + +namespace toml +{ + +template(), + typename std::enable_if<(vT != toml::value_t::Unknown && + vT != value_t::Empty), std::nullptr_t>::type = nullptr> +inline toml::value to_toml(T&& x) +{ + return toml::value(std::forward(x)); +} + +template(), + typename std::enable_if<(vT == toml::value_t::Unknown) && + (!toml::detail::is_map::value) && + toml::detail::is_container::value, std::nullptr_t>::type = nullptr> +toml::value to_toml(T&& x) +{ + toml::Array tmp; tmp.reserve(std::distance(std::begin(x), std::end(x))); + for(auto iter = std::begin(x); iter != std::end(x); ++iter) + tmp.emplace_back(*iter); + return toml::value(std::move(tmp)); +} + +template(), + typename std::enable_if<(vT == toml::value_t::Unknown) && + toml::detail::is_map::value, std::nullptr_t>::type = nullptr> +toml::value to_toml(T&& x) +{ + toml::Table tmp; + for(auto iter = std::begin(x); iter != std::end(x); ++iter) + tmp.emplace(iter->first, to_toml(iter->second)); + return toml::value(std::move(tmp)); +} + +template +inline toml::value to_toml(std::initializer_list init) +{ + return toml::value(std::move(init)); +} + +inline toml::value +to_toml(std::initializer_list> init) +{ + return toml::value(std::move(init)); +} + +} // toml +#endif // TOML11_TO_TOML diff --git a/toml/traits.hpp b/toml/traits.hpp new file mode 100644 index 0000000..88b38c9 --- /dev/null +++ b/toml/traits.hpp @@ -0,0 +1,54 @@ +#ifndef TOML11_TRAITS +#define TOML11_TRAITS +#include + +namespace toml +{ +namespace detail +{ + +template +using unwrap_t = typename std::decay::type; + +struct has_iterator_impl +{ + template static std::true_type check(typename T::iterator*); + template static std::false_type check(...); +}; +struct has_value_type_impl +{ + template static std::true_type check(typename T::value_type*); + template static std::false_type check(...); +}; +struct has_key_type_impl +{ + template static std::true_type check(typename T::key_type*); + template static std::false_type check(...); +}; +struct has_mapped_type_impl +{ + template static std::true_type check(typename T::mapped_type*); + template static std::false_type check(...); +}; + +template +struct has_iterator : decltype(has_iterator_impl::check(nullptr)){}; +template +struct has_value_type : decltype(has_value_type_impl::check(nullptr)){}; +template +struct has_key_type : decltype(has_key_type_impl::check(nullptr)){}; +template +struct has_mapped_type : decltype(has_mapped_type_impl::check(nullptr)){}; + +template +struct is_container : std::integral_constant::value && has_value_type::value>{}; + +template +struct is_map : std::integral_constant::value && has_key_type::value && + has_mapped_type::value>{}; + +}// detail +}//toml +#endif // TOML_TRAITS diff --git a/toml/utility.hpp b/toml/utility.hpp new file mode 100644 index 0000000..80b5843 --- /dev/null +++ b/toml/utility.hpp @@ -0,0 +1,16 @@ +#ifndef TOML11_UTILITY +#define TOML11_UTILITY +#include +#include + +namespace toml +{ + +template +inline std::unique_ptr make_unique(Ts&& ... args) +{ + return std::unique_ptr(new T(std::forward(args)...)); +} + +}// toml +#endif // TOML11_UTILITY diff --git a/toml/toml.hpp b/toml/value.hpp similarity index 67% rename from toml/toml.hpp rename to toml/value.hpp index bbfdbf0..8b631eb 100644 --- a/toml/toml.hpp +++ b/toml/value.hpp @@ -1,35 +1,9 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2017 Toru Niina - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef TOML_FOR_MODERN_CPP -#define TOML_FOR_MODERN_CPP +#ifndef TOML11_VALUE +#define TOML11_VALUE #include "datetime.hpp" -#include -#include -#include -#include -#include +#include "traits.hpp" +#include "utility.hpp" +#include "exception.hpp" #include #include #include @@ -105,9 +79,6 @@ stringize(value_t t) namespace detail { -template -using unwrap_t = typename std::decay::type; - template constexpr inline value_t check_type() { @@ -131,56 +102,6 @@ template<> struct is_toml_array : std::true_type {}; template struct is_toml_table : std::false_type{}; template<> struct is_toml_table : std::true_type {}; -template struct toml_default_type{}; -template<> struct toml_default_type{typedef Boolean type;}; -template<> struct toml_default_type{typedef Integer type;}; -template<> struct toml_default_type{typedef Float type;}; -template<> struct toml_default_type{typedef String type;}; -template<> struct toml_default_type{typedef Datetime type;}; -template<> struct toml_default_type{typedef Array type;}; -template<> struct toml_default_type{typedef Table type;}; -template<> struct toml_default_type{typedef void type;}; -template<> struct toml_default_type{typedef void type;}; - -struct has_iterator_impl -{ - template static std::true_type check(typename T::iterator*); - template static std::false_type check(...); -}; -struct has_value_type_impl -{ - template static std::true_type check(typename T::value_type*); - template static std::false_type check(...); -}; -struct has_key_type_impl -{ - template static std::true_type check(typename T::key_type*); - template static std::false_type check(...); -}; -struct has_mapped_type_impl -{ - template static std::true_type check(typename T::mapped_type*); - template static std::false_type check(...); -}; - -template -struct has_iterator : decltype(has_iterator_impl::check(nullptr)){}; -template -struct has_value_type : decltype(has_value_type_impl::check(nullptr)){}; -template -struct has_key_type : decltype(has_key_type_impl::check(nullptr)){}; -template -struct has_mapped_type : decltype(has_mapped_type_impl::check(nullptr)){}; - -template -struct is_container : std::integral_constant::value && has_value_type::value>{}; - -template -struct is_map : std::integral_constant::value && has_key_type::value && - has_mapped_type::value>{}; - struct is_key_convertible_impl { template @@ -192,6 +113,17 @@ struct is_key_convertible_impl template struct is_key_convertible : decltype(is_key_convertible_impl::check(nullptr)){}; +template struct toml_default_type{}; +template<> struct toml_default_type{typedef Boolean type;}; +template<> struct toml_default_type{typedef Integer type;}; +template<> struct toml_default_type{typedef Float type;}; +template<> struct toml_default_type{typedef String type;}; +template<> struct toml_default_type{typedef Datetime type;}; +template<> struct toml_default_type{typedef Array type;}; +template<> struct toml_default_type{typedef Table type;}; +template<> struct toml_default_type{typedef void type;}; +template<> struct toml_default_type{typedef void type;}; + struct storage_base { storage_base(): type(toml::value_t::Empty){} @@ -221,56 +153,6 @@ struct storage : public storage_base }; } // detail -/* -------------------------------------------------------------------------- */ -template -inline std::unique_ptr make_unique(Ts&& ... args) -{ - return std::unique_ptr(new T(std::forward(args)...)); -} - -struct exception : public std::exception -{ - public: - virtual ~exception() override = default; - virtual const char* what() const noexcept override {return "";} -}; - -struct syntax_error : public toml::exception -{ - public: - explicit syntax_error(const std::string& what_arg) : what_(what_arg){} - explicit syntax_error(const char* what_arg) : what_(what_arg){} - virtual ~syntax_error() override = default; - virtual const char* what() const noexcept override {return what_.c_str();} - - protected: - std::string what_; -}; - -struct type_error : public toml::exception -{ - public: - explicit type_error(const std::string& what_arg) : what_(what_arg){} - explicit type_error(const char* what_arg) : what_(what_arg){} - virtual ~type_error() override = default; - virtual const char* what() const noexcept override {return what_.c_str();} - - protected: - std::string what_; -}; - -struct internal_error : public toml::exception -{ - public: - explicit internal_error(const std::string& what_arg) : what_(what_arg){} - explicit internal_error(const char* what_arg) : what_(what_arg){} - virtual ~internal_error() override = default; - virtual const char* what() const noexcept override {return what_.c_str();} - protected: - std::string what_; -}; -/* -------------------------------------------------------------------------- */ - template struct value_traits { @@ -820,192 +702,5 @@ inline bool operator>=(const toml::value& lhs, const toml::value& rhs) return !(lhs < rhs); } -/* ------------------------------- to_toml ---------------------------------- */ - -template(), - typename std::enable_if<(vT != toml::value_t::Unknown && - vT != value_t::Empty), std::nullptr_t>::type = nullptr> -inline toml::value to_toml(T&& x) -{ - return toml::value(std::forward(x)); -} - -template(), - typename std::enable_if<(vT == toml::value_t::Unknown) && - (!toml::detail::is_map::value) && - toml::detail::is_container::value, std::nullptr_t>::type = nullptr> -toml::value to_toml(T&& x) -{ - toml::Array tmp; tmp.reserve(std::distance(std::begin(x), std::end(x))); - for(auto iter = std::begin(x); iter != std::end(x); ++iter) - tmp.emplace_back(*iter); - return toml::value(std::move(tmp)); -} - -template(), - typename std::enable_if<(vT == toml::value_t::Unknown) && - toml::detail::is_map::value, std::nullptr_t>::type = nullptr> -toml::value to_toml(T&& x) -{ - toml::Table tmp; - for(auto iter = std::begin(x); iter != std::end(x); ++iter) - tmp.emplace(iter->first, to_toml(iter->second)); - return toml::value(std::move(tmp)); -} - -template -inline toml::value to_toml(std::initializer_list init) -{ - return toml::value(std::move(init)); -} - -inline toml::value -to_toml(std::initializer_list> init) -{ - return toml::value(std::move(init)); -} - -/* ------------------------------ from_toml --------------------------------- */ - - -template(), - typename std::enable_if<(vT != toml::value_t::Unknown && - vT != value_t::Empty), std::nullptr_t>::type = nullptr> -void from_toml(T& x, const toml::value& v) -{ - if(v.type() != vT) - throw type_error("from_toml: value type: " + stringize(v.type()) + - std::string(" is not arguemnt type: ") + stringize(vT)); - x = v.cast(); - return; -} - -template(), - typename std::enable_if<(vT == toml::value_t::Unknown) && - (!toml::detail::is_map::value) && - toml::detail::is_container::value, std::nullptr_t>::type = nullptr> -void from_toml(T& x, const toml::value& v) -{ - // TODO the case of x is not dynamic container case - if(v.type() != value_t::Array) - throw type_error("from_toml: value type: " + stringize(v.type()) + - std::string(" is not argument type: Array")); - const auto& ar = v.cast(); - for(const auto& val : ar) - { - typename T::value_type v; - from_toml(v, val); - x.push_back(std::move(v)); - } - return; -} - -template(), - typename std::enable_if<(vT == toml::value_t::Unknown) && - toml::detail::is_map::value, std::nullptr_t>::type = nullptr> -void from_toml(T& x, const toml::value& v) -{ - if(v.type() != value_t::Table) - throw type_error("from_toml: value type: " + stringize(v.type()) + - std::string(" is not argument type: Table")); - x.clear(); - const auto& tb = v.cast(); - for(const auto& kv : tb) - { - x.insert(kv); - } - return; -} - -namespace detail -{ - -template -constexpr toml::value_t determine_castable_type() -{ - return check_type() != toml::value_t::Unknown ? check_type() : - toml::detail::is_map::value ? toml::value_t::Table : - toml::detail::is_container::value ? toml::value_t::Array : - toml::value_t::Unknown; -} - -template -struct from_toml_tie_impl -{ - constexpr static std::size_t index = sizeof...(Ts) - N; - constexpr static toml::value_t type_index = - determine_castable_type< - typename std::tuple_element>::type>(); - - static void invoke(std::tuple tie, const toml::value& v) - { - if(type_index == v.type()) - { - from_toml(std::get(tie), v); - return; - } - return from_toml_tie_impl::invoke(tie, v); - } -}; - -template -struct from_toml_tie_impl<0, Ts...> -{ - static void invoke(std::tuple tie, const toml::value& v) - { - throw type_error("from_toml(tie, value): no match"); - } -}; - -} // detail - -template -void from_toml(std::tuple tie, const toml::value& v) -{ - detail::from_toml_tie_impl::invoke(tie, v); - return; -} - -/* ------------------------------ get --------------------------------- */ - -template(), - typename std::enable_if<(vT != toml::value_t::Unknown && - vT != value_t::Empty), std::nullptr_t>::type = nullptr> -inline T get(const toml::value& v) -{ - return static_cast(v.cast()); -} - -template(), - typename std::enable_if<(vT == toml::value_t::Unknown) && - (!toml::detail::is_map::value) && - toml::detail::is_container::value, std::nullptr_t>::type = nullptr> -T get(const toml::value& v) -{ - if(v.type() != value_t::Array) - throw type_error("from_toml: value type: " + stringize(v.type()) + - std::string(" is not argument type: Array")); - T tmp; - from_toml(tmp, v); - return tmp; -} - -template(), - typename std::enable_if<(vT == toml::value_t::Unknown) && - toml::detail::is_map::value, std::nullptr_t>::type = nullptr> -T get(const toml::value& v) -{ - if(v.type() != value_t::Table) - throw type_error("from_toml: value type: " + stringize(v.type()) + - std::string(" is not argument type: Table")); - T tmp; - from_toml(tmp, v); - return tmp; -} - - - - - }// toml -#endif// TOML_FOR_MODERN_CPP +#endif// TOML11_VALUE