From 8acc3481060e1dc1e30c3f2c7133514ded63dfde Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sat, 1 Jun 2019 13:33:57 +0900 Subject: [PATCH] feat: :boom: change interface around types - change value_t::typename from CamelCase to snake_case. - drop CamelCase typename supports. The changes are introduced to make the interfaces uniform. For some (historical) reasons, toml11 has both CamelCase names and snake_case names for types. Additionally, since `float` is a keyword, snake_case names uses `floating` to avoid collision and CamelCase name uses `Float` because toml official calls it `Float`. This is too confusing. Since it is a major upgrade, I think it is a big chance to make them uniform. --- toml/types.hpp | 191 ++++++++++++++++++++++--------------------------- 1 file changed, 85 insertions(+), 106 deletions(-) diff --git a/toml/types.hpp b/toml/types.hpp index 47c9248..f283df8 100644 --- a/toml/types.hpp +++ b/toml/types.hpp @@ -5,50 +5,46 @@ #include "datetime.hpp" #include "string.hpp" #include "traits.hpp" -#include -#include +#include "comments.hpp" namespace toml { -using character = char; +template Table, // map-like class + template Array> // vector-like class +class basic_value; -class value; +using character = char; using key = std::string; -using Boolean = bool; -using Integer = std::int64_t; -using Float = double; -using String = ::toml::string; -using Datetime = offset_datetime; -using OffsetDatetime = offset_datetime; -using LocalDatetime = local_datetime; -using LocalDate = local_date; -using LocalTime = local_time; -using Array = std::vector; -using Table = std::unordered_map; +using boolean = bool; +using integer = std::int64_t; +using floating = double; // "float" is a keyward, cannot use it here. +// the following stuffs are structs defined here, so aliases are not needed. +// - string +// - offset_datetime +// - offset_datetime +// - local_datetime +// - local_date +// - local_time -// alias for snake_case, consistency with STL/Boost, toml::key, toml::value -using boolean = Boolean; -using integer = Integer; -using floating = Float; // XXX `float` is keyword. we can't use it here -using array = Array; -using table = Table; +// default toml::value and default array/table +using value = basic_value; enum class value_t : std::uint8_t { - Empty = 0, - Boolean = 1, - Integer = 2, - Float = 3, - String = 4, - OffsetDatetime = 5, - LocalDatetime = 6, - LocalDate = 7, - LocalTime = 8, - Array = 9, - Table = 10, - Unknown = 255, + empty = 0, + boolean = 1, + integer = 2, + floating = 3, + string = 4, + offset_datetime = 5, + local_datetime = 6, + local_date = 7, + local_time = 8, + array = 9, + table = 10, }; template @@ -57,27 +53,27 @@ 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::OffsetDatetime: os << "offset_datetime"; return os; - case toml::value_t::LocalDatetime : os << "local_datetime"; return os; - case toml::value_t::LocalDate : os << "local_date"; return os; - case toml::value_t::LocalTime : os << "local_time"; 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; + case value_t::boolean : os << "boolean"; return os; + case value_t::integer : os << "integer"; return os; + case value_t::floating : os << "floating"; return os; + case value_t::string : os << "string"; return os; + case value_t::offset_datetime : os << "offset_datetime"; return os; + case value_t::local_datetime : os << "local_datetime"; return os; + case value_t::local_date : os << "local_date"; return os; + case value_t::local_time : os << "local_time"; return os; + case value_t::array : os << "array"; return os; + case value_t::table : os << "table"; return os; + case value_t::empty : os << "empty"; return os; + default : os << "unknown"; return os; } } -template, +template, typename alloc = std::allocator> inline std::basic_string stringize(value_t t) { - std::ostringstream oss; + std::basic_ostringstream oss; oss << t; return oss.str(); } @@ -85,60 +81,43 @@ inline std::basic_string stringize(value_t t) namespace detail { -template -constexpr inline value_t check_type() -{ - using type = typename std::remove_cv< - typename std::remove_reference::type - >::type; - return std::is_same::value ? value_t::Boolean : - std::is_integral::value ? value_t::Integer : - std::is_floating_point::value ? value_t::Float : - std::is_same::value ? value_t::String : - std::is_same::value ? value_t::String : - std::is_same::value ? value_t::LocalDate : - std::is_same::value ? value_t::LocalTime : - is_chrono_duration::value ? value_t::LocalTime : - std::is_same::value ? value_t::LocalDatetime : - std::is_same::value ? value_t::OffsetDatetime : - std::is_same::value ? value_t::OffsetDatetime : - std::is_convertible::value ? value_t::Array : - std::is_convertible::value ? value_t::Table : - value_t::Unknown; -} +// meta-function that convertes from value_t to the exact toml type that corresponds to. +// It takes toml::basic_value type because array and table types depend on it. +template struct enum_to_type {using type = void ;}; +template struct enum_to_type{using type = void ;}; +template struct enum_to_type{using type = boolean ;}; +template struct enum_to_type{using type = integer ;}; +template struct enum_to_type{using type = floating ;}; +template struct enum_to_type{using type = string ;}; +template struct enum_to_type{using type = offset_datetime ;}; +template struct enum_to_type{using type = local_datetime ;}; +template struct enum_to_type{using type = local_date ;}; +template struct enum_to_type{using type = local_time ;}; +template struct enum_to_type{using type = typename Value::array_type;}; +template struct enum_to_type{using type = typename Value::table_type;}; -constexpr inline bool is_valid(value_t vt) -{ - return vt != value_t::Unknown; -} +// meta-function that converts from an exact toml type to the enum that corresponds to. +template +struct type_to_enum : typename std::conditional< + std::is_same::value, // if T == array_type, + std::integral_constant, // then value_t::array + typename std::conditional< // else... + std::is_same::value, // if T == table_type + std::integral_constant, // then value_t::table + std::integral_constant // else value_t::empty + >::type + >::type {} +template struct type_to_enum: std::integral_constant {}; +template struct type_to_enum: std::integral_constant {}; +template struct type_to_enum: std::integral_constant {}; +template struct type_to_enum: std::integral_constant {}; +template struct type_to_enum: std::integral_constant {}; +template struct type_to_enum: std::integral_constant {}; +template struct type_to_enum: std::integral_constant {}; +template struct type_to_enum: std::integral_constant {}; -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 floating type;}; -template<> struct toml_default_type {typedef string type;}; -template<> struct toml_default_type{typedef offset_datetime type;}; -template<> struct toml_default_type {typedef local_datetime type;}; -template<> struct toml_default_type {typedef local_date type;}; -template<> struct toml_default_type {typedef local_time 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 toml_value_t {static constexpr value_t value = value_t::Unknown ;}; -template<> struct toml_value_t{static constexpr value_t value = value_t::Boolean ;}; -template<> struct toml_value_t{static constexpr value_t value = value_t::Integer ;}; -template<> struct toml_value_t{static constexpr value_t value = value_t::Float ;}; -template<> struct toml_value_t{static constexpr value_t value = value_t::String ;}; -template<> struct toml_value_t{static constexpr value_t value = value_t::OffsetDatetime;}; -template<> struct toml_value_t{static constexpr value_t value = value_t::LocalDatetime ;}; -template<> struct toml_value_t{static constexpr value_t value = value_t::LocalDate ;}; -template<> struct toml_value_t{static constexpr value_t value = value_t::LocalTime ;}; -template<> struct toml_value_t{static constexpr value_t value = value_t::Array ;}; -template<> struct toml_value_t{static constexpr value_t value = value_t::Table ;}; - -template +// meta-function that checks the type T is the same as one of the toml::* types. +template struct is_exact_toml_type : disjunction< std::is_same, std::is_same, @@ -148,13 +127,13 @@ 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_exact_toml_type : is_exact_toml_type{}; -template struct is_exact_toml_type : is_exact_toml_type{}; -template struct is_exact_toml_type : is_exact_toml_type{}; -template struct is_exact_toml_type: is_exact_toml_type{}; +template struct is_exact_toml_type : is_exact_toml_type{}; +template struct is_exact_toml_type : is_exact_toml_type{}; +template struct is_exact_toml_type : is_exact_toml_type{}; +template struct is_exact_toml_type: is_exact_toml_type{}; } // detail } // toml