diff --git a/toml/types.hpp b/toml/types.hpp new file mode 100644 index 00000000..d822823b --- /dev/null +++ b/toml/types.hpp @@ -0,0 +1,138 @@ +#ifndef TOML11_TYPES_H +#define TOML11_TYPES_H +#include "datetime.hpp" +#include +#include +#include +#include + +namespace toml +{ + +using character = char; + +class value; +using key = std::basic_string; + +using Boolean = bool; +using Integer = std::int64_t; +using Float = double; +using String = std::basic_string; +using Datetime = basic_datetime; +using Array = std::vector; +using Table = std::unordered_map; + +enum class value_t : std::uint8_t +{ + Boolean = 1, + Integer = 2, + Float = 3, + String = 4, + Datetime = 5, + Array = 6, + Table = 7, + Empty = 0, + Unknown = 255, +}; + +template> +inline std::basic_ostream& +operator<<(std::basic_ostream& os, value_t t) +{ + switch(t) + { + case toml::value_t::Boolean : os << "Boolean"; return os; + case toml::value_t::Integer : os << "Integer"; return os; + case toml::value_t::Float : os << "Float"; return os; + case toml::value_t::String : os << "String"; return os; + case toml::value_t::Datetime: os << "Datetime"; return os; + case toml::value_t::Array : os << "Array"; return os; + case toml::value_t::Table : os << "Table"; return os; + case toml::value_t::Empty : os << "Empty"; return os; + case toml::value_t::Unknown : os << "Unknown"; return os; + default : os << "Nothing"; return os; + } +} + +template, + typename alloc = std::allocator> +inline std::basic_string +stringize(value_t t) +{ + switch(t) + { + case toml::value_t::Boolean : return "Boolean"; + case toml::value_t::Integer : return "Integer"; + case toml::value_t::Float : return "Float"; + case toml::value_t::String : return "String"; + case toml::value_t::Datetime: return "Datetime"; + case toml::value_t::Array : return "Array"; + case toml::value_t::Table : return "Table"; + case toml::value_t::Empty : return "Empty"; + case toml::value_t::Unknown : return "Unknown"; + default : return "Nothing"; + } +} + +namespace detail +{ + +template +constexpr inline value_t check_type() +{ + return std::is_same, toml::Boolean >::value ? value_t::Boolean : + std::is_integral>::value ? value_t::Integer : + std::is_floating_point>::value ? value_t::Float : + std::is_convertible, toml::String >::value ? value_t::String : + std::is_convertible, toml::Datetime>::value ? value_t::Datetime: + std::is_convertible, toml::Array >::value ? value_t::Array : + std::is_convertible, toml::Table >::value ? value_t::Table : + value_t::Unknown; +} + +constexpr inline bool is_valid(value_t vt) +{ + return vt != value_t::Unknown; +} + +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;}; + +template +struct is_exact_toml_type : disjunction< + std::is_same, + std::is_same, + std::is_same, + std::is_same, + std::is_same, + std::is_same, + std::is_same + >{}; + +template +struct is_map : conjunction< + has_iterator, + has_value_type, + has_key_type, + has_mapped_type + >{}; + +template +struct is_container : conjunction< + negation>, + negation>, + has_iterator, + has_value_type + >{}; + +} // detail +} // toml +#endif// TOML11_TYPES_H diff --git a/toml/value.hpp b/toml/value.hpp index cd1e3857..67d0c6bf 100644 --- a/toml/value.hpp +++ b/toml/value.hpp @@ -1,9 +1,9 @@ #ifndef TOML11_VALUE #define TOML11_VALUE -#include "datetime.hpp" #include "traits.hpp" #include "utility.hpp" #include "exception.hpp" +#include "types.hpp" #include #include #include @@ -13,130 +13,8 @@ namespace toml { -using character = char; - -class value; -using key = std::basic_string; - -using Boolean = bool; -using Integer = std::int64_t; -using Float = double; -using String = std::basic_string; -using Datetime = basic_datetime; -using Array = std::vector; -using Table = std::unordered_map; - -enum class value_t : std::uint8_t -{ - Boolean = 1, - Integer = 2, - Float = 3, - String = 4, - Datetime = 5, - Array = 6, - Table = 7, - Empty = 0, - Unknown = 255, -}; - -template> -inline std::basic_ostream& -operator<<(std::basic_ostream& os, value_t t) -{ - switch(t) - { - case toml::value_t::Boolean : os << "Boolean"; return os; - case toml::value_t::Integer : os << "Integer"; return os; - case toml::value_t::Float : os << "Float"; return os; - case toml::value_t::String : os << "String"; return os; - case toml::value_t::Datetime: os << "Datetime"; return os; - case toml::value_t::Array : os << "Array"; return os; - case toml::value_t::Table : os << "Table"; return os; - case toml::value_t::Empty : os << "Empty"; return os; - case toml::value_t::Unknown : os << "Unknown"; return os; - default : os << "Nothing"; return os; - } -} - -template, - typename alloc = std::allocator> -inline std::basic_string -stringize(value_t t) -{ - switch(t) - { - case toml::value_t::Boolean : return "Boolean"; - case toml::value_t::Integer : return "Integer"; - case toml::value_t::Float : return "Float"; - case toml::value_t::String : return "String"; - case toml::value_t::Datetime: return "Datetime"; - case toml::value_t::Array : return "Array"; - case toml::value_t::Table : return "Table"; - case toml::value_t::Empty : return "Empty"; - case toml::value_t::Unknown : return "Unknown"; - default : return "Nothing"; - } -} - namespace detail { - -template -constexpr inline value_t check_type() -{ - return std::is_same, toml::Boolean >::value ? value_t::Boolean : - std::is_integral>::value ? value_t::Integer : - std::is_floating_point>::value ? value_t::Float : - std::is_convertible, toml::String >::value ? value_t::String : - std::is_convertible, toml::Datetime>::value ? value_t::Datetime: - std::is_convertible, toml::Array >::value ? value_t::Array : - std::is_convertible, toml::Table >::value ? value_t::Table : - value_t::Unknown; -} - -constexpr inline bool is_valid(value_t vt) -{ - return vt != value_t::Unknown; -} - -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;}; - -template -struct is_exact_toml_type : disjunction< - std::is_same, - std::is_same, - std::is_same, - std::is_same, - std::is_same, - std::is_same, - std::is_same - >{}; - -template -struct is_map : conjunction< - has_iterator, - has_value_type, - has_key_type, - has_mapped_type - >{}; - -template -struct is_container : conjunction< - negation>, - negation>, - has_iterator, - has_value_type - >{}; - struct storage_base { storage_base(): type(toml::value_t::Empty){}