mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-17 17:58:09 +08:00
feat: 💥 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.
This commit is contained in:
191
toml/types.hpp
191
toml/types.hpp
@@ -5,50 +5,46 @@
|
|||||||
#include "datetime.hpp"
|
#include "datetime.hpp"
|
||||||
#include "string.hpp"
|
#include "string.hpp"
|
||||||
#include "traits.hpp"
|
#include "traits.hpp"
|
||||||
#include <vector>
|
#include "comments.hpp"
|
||||||
#include <unordered_map>
|
|
||||||
|
|
||||||
namespace toml
|
namespace toml
|
||||||
{
|
{
|
||||||
|
|
||||||
using character = char;
|
template<typename Comment, // discard/preserve_comment
|
||||||
|
template<typename ...> Table, // map-like class
|
||||||
|
template<typename ...> Array> // vector-like class
|
||||||
|
class basic_value;
|
||||||
|
|
||||||
class value;
|
using character = char;
|
||||||
using key = std::string;
|
using key = std::string;
|
||||||
|
|
||||||
using Boolean = bool;
|
using boolean = bool;
|
||||||
using Integer = std::int64_t;
|
using integer = std::int64_t;
|
||||||
using Float = double;
|
using floating = double; // "float" is a keyward, cannot use it here.
|
||||||
using String = ::toml::string;
|
// the following stuffs are structs defined here, so aliases are not needed.
|
||||||
using Datetime = offset_datetime;
|
// - string
|
||||||
using OffsetDatetime = offset_datetime;
|
// - offset_datetime
|
||||||
using LocalDatetime = local_datetime;
|
// - offset_datetime
|
||||||
using LocalDate = local_date;
|
// - local_datetime
|
||||||
using LocalTime = local_time;
|
// - local_date
|
||||||
using Array = std::vector<value>;
|
// - local_time
|
||||||
using Table = std::unordered_map<key, value>;
|
|
||||||
|
|
||||||
// alias for snake_case, consistency with STL/Boost, toml::key, toml::value
|
// default toml::value and default array/table
|
||||||
using boolean = Boolean;
|
using value = basic_value<discard_comment, std::unordered_map, std::vector>;
|
||||||
using integer = Integer;
|
|
||||||
using floating = Float; // XXX `float` is keyword. we can't use it here
|
|
||||||
using array = Array;
|
|
||||||
using table = Table;
|
|
||||||
|
|
||||||
enum class value_t : std::uint8_t
|
enum class value_t : std::uint8_t
|
||||||
{
|
{
|
||||||
Empty = 0,
|
empty = 0,
|
||||||
Boolean = 1,
|
boolean = 1,
|
||||||
Integer = 2,
|
integer = 2,
|
||||||
Float = 3,
|
floating = 3,
|
||||||
String = 4,
|
string = 4,
|
||||||
OffsetDatetime = 5,
|
offset_datetime = 5,
|
||||||
LocalDatetime = 6,
|
local_datetime = 6,
|
||||||
LocalDate = 7,
|
local_date = 7,
|
||||||
LocalTime = 8,
|
local_time = 8,
|
||||||
Array = 9,
|
array = 9,
|
||||||
Table = 10,
|
table = 10,
|
||||||
Unknown = 255,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename charT, typename traits>
|
template<typename charT, typename traits>
|
||||||
@@ -57,27 +53,27 @@ operator<<(std::basic_ostream<charT, traits>& os, value_t t)
|
|||||||
{
|
{
|
||||||
switch(t)
|
switch(t)
|
||||||
{
|
{
|
||||||
case toml::value_t::Boolean : os << "boolean"; return os;
|
case value_t::boolean : os << "boolean"; return os;
|
||||||
case toml::value_t::Integer : os << "integer"; return os;
|
case value_t::integer : os << "integer"; return os;
|
||||||
case toml::value_t::Float : os << "float"; return os;
|
case value_t::floating : os << "floating"; return os;
|
||||||
case toml::value_t::String : os << "string"; return os;
|
case value_t::string : os << "string"; return os;
|
||||||
case toml::value_t::OffsetDatetime: os << "offset_datetime"; return os;
|
case value_t::offset_datetime : os << "offset_datetime"; return os;
|
||||||
case toml::value_t::LocalDatetime : os << "local_datetime"; return os;
|
case value_t::local_datetime : os << "local_datetime"; return os;
|
||||||
case toml::value_t::LocalDate : os << "local_date"; return os;
|
case value_t::local_date : os << "local_date"; return os;
|
||||||
case toml::value_t::LocalTime : os << "local_time"; return os;
|
case value_t::local_time : os << "local_time"; return os;
|
||||||
case toml::value_t::Array : os << "array"; return os;
|
case value_t::array : os << "array"; return os;
|
||||||
case toml::value_t::Table : os << "table"; return os;
|
case value_t::table : os << "table"; return os;
|
||||||
case toml::value_t::Empty : os << "empty"; return os;
|
case value_t::empty : os << "empty"; return os;
|
||||||
case toml::value_t::Unknown : os << "unknown"; return os;
|
default : os << "unknown"; return os;
|
||||||
default : os << "nothing"; return os;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename charT = character, typename traits = std::char_traits<charT>,
|
template<typename charT = char,
|
||||||
|
typename traits = std::char_traits<charT>,
|
||||||
typename alloc = std::allocator<charT>>
|
typename alloc = std::allocator<charT>>
|
||||||
inline std::basic_string<charT, traits, alloc> stringize(value_t t)
|
inline std::basic_string<charT, traits, alloc> stringize(value_t t)
|
||||||
{
|
{
|
||||||
std::ostringstream oss;
|
std::basic_ostringstream<charT, traits, alloc> oss;
|
||||||
oss << t;
|
oss << t;
|
||||||
return oss.str();
|
return oss.str();
|
||||||
}
|
}
|
||||||
@@ -85,60 +81,43 @@ inline std::basic_string<charT, traits, alloc> stringize(value_t t)
|
|||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
|
|
||||||
template<typename T>
|
// meta-function that convertes from value_t to the exact toml type that corresponds to.
|
||||||
constexpr inline value_t check_type()
|
// It takes toml::basic_value type because array and table types depend on it.
|
||||||
{
|
template<value_t t, typename Value> struct enum_to_type {using type = void ;};
|
||||||
using type = typename std::remove_cv<
|
template<typename Value> struct enum_to_type<value_t::empty , Value>{using type = void ;};
|
||||||
typename std::remove_reference<T>::type
|
template<typename Value> struct enum_to_type<value_t::boolean , Value>{using type = boolean ;};
|
||||||
>::type;
|
template<typename Value> struct enum_to_type<value_t::integer , Value>{using type = integer ;};
|
||||||
return std::is_same<type, toml::boolean>::value ? value_t::Boolean :
|
template<typename Value> struct enum_to_type<value_t::floating , Value>{using type = floating ;};
|
||||||
std::is_integral<type>::value ? value_t::Integer :
|
template<typename Value> struct enum_to_type<value_t::string , Value>{using type = string ;};
|
||||||
std::is_floating_point<type>::value ? value_t::Float :
|
template<typename Value> struct enum_to_type<value_t::offset_datetime, Value>{using type = offset_datetime ;};
|
||||||
std::is_same<type, std::string>::value ? value_t::String :
|
template<typename Value> struct enum_to_type<value_t::local_datetime , Value>{using type = local_datetime ;};
|
||||||
std::is_same<type, toml::string>::value ? value_t::String :
|
template<typename Value> struct enum_to_type<value_t::local_date , Value>{using type = local_date ;};
|
||||||
std::is_same<type, toml::local_date>::value ? value_t::LocalDate :
|
template<typename Value> struct enum_to_type<value_t::local_time , Value>{using type = local_time ;};
|
||||||
std::is_same<type, toml::local_time>::value ? value_t::LocalTime :
|
template<typename Value> struct enum_to_type<value_t::array , Value>{using type = typename Value::array_type;};
|
||||||
is_chrono_duration<type>::value ? value_t::LocalTime :
|
template<typename Value> struct enum_to_type<value_t::table , Value>{using type = typename Value::table_type;};
|
||||||
std::is_same<type, toml::local_datetime>::value ? value_t::LocalDatetime :
|
|
||||||
std::is_same<type, toml::offset_datetime>::value ? value_t::OffsetDatetime :
|
|
||||||
std::is_same<type, std::chrono::system_clock::time_point>::value ? value_t::OffsetDatetime :
|
|
||||||
std::is_convertible<type, toml::array>::value ? value_t::Array :
|
|
||||||
std::is_convertible<type, toml::table>::value ? value_t::Table :
|
|
||||||
value_t::Unknown;
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr inline bool is_valid(value_t vt)
|
// meta-function that converts from an exact toml type to the enum that corresponds to.
|
||||||
{
|
template<typename T, typename Value>
|
||||||
return vt != value_t::Unknown;
|
struct type_to_enum : typename std::conditional<
|
||||||
}
|
std::is_same<T, typename Value::array_type>::value, // if T == array_type,
|
||||||
|
std::integral_constant<value_t, value_t::array>, // then value_t::array
|
||||||
|
typename std::conditional< // else...
|
||||||
|
std::is_same<T, typename Value::table_type>::value, // if T == table_type
|
||||||
|
std::integral_constant<value_t, value_t::table>, // then value_t::table
|
||||||
|
std::integral_constant<value_t, value_t::empty> // else value_t::empty
|
||||||
|
>::type
|
||||||
|
>::type {}
|
||||||
|
template<typename Value> struct type_to_enum<boolean , Value>: std::integral_constant<value_t, value_t::boolean > {};
|
||||||
|
template<typename Value> struct type_to_enum<integer , Value>: std::integral_constant<value_t, value_t::integer > {};
|
||||||
|
template<typename Value> struct type_to_enum<floating , Value>: std::integral_constant<value_t, value_t::floating > {};
|
||||||
|
template<typename Value> struct type_to_enum<string , Value>: std::integral_constant<value_t, value_t::string > {};
|
||||||
|
template<typename Value> struct type_to_enum<offset_datetime, Value>: std::integral_constant<value_t, value_t::offset_datetime> {};
|
||||||
|
template<typename Value> struct type_to_enum<local_datetime , Value>: std::integral_constant<value_t, value_t::local_datetime > {};
|
||||||
|
template<typename Value> struct type_to_enum<local_date , Value>: std::integral_constant<value_t, value_t::local_date > {};
|
||||||
|
template<typename Value> struct type_to_enum<local_time , Value>: std::integral_constant<value_t, value_t::local_time > {};
|
||||||
|
|
||||||
template<value_t t> struct toml_default_type;
|
// meta-function that checks the type T is the same as one of the toml::* types.
|
||||||
template<> struct toml_default_type<value_t::Boolean > {typedef boolean type;};
|
template<typename T, typename Value>
|
||||||
template<> struct toml_default_type<value_t::Integer > {typedef integer type;};
|
|
||||||
template<> struct toml_default_type<value_t::Float > {typedef floating type;};
|
|
||||||
template<> struct toml_default_type<value_t::String > {typedef string type;};
|
|
||||||
template<> struct toml_default_type<value_t::OffsetDatetime>{typedef offset_datetime type;};
|
|
||||||
template<> struct toml_default_type<value_t::LocalDatetime> {typedef local_datetime type;};
|
|
||||||
template<> struct toml_default_type<value_t::LocalDate> {typedef local_date type;};
|
|
||||||
template<> struct toml_default_type<value_t::LocalTime> {typedef local_time type;};
|
|
||||||
template<> struct toml_default_type<value_t::Array > {typedef array type;};
|
|
||||||
template<> struct toml_default_type<value_t::Table > {typedef table type;};
|
|
||||||
template<> struct toml_default_type<value_t::Empty > {typedef void type;};
|
|
||||||
template<> struct toml_default_type<value_t::Unknown > {typedef void type;};
|
|
||||||
|
|
||||||
template<typename T> struct toml_value_t {static constexpr value_t value = value_t::Unknown ;};
|
|
||||||
template<> struct toml_value_t<boolean >{static constexpr value_t value = value_t::Boolean ;};
|
|
||||||
template<> struct toml_value_t<integer >{static constexpr value_t value = value_t::Integer ;};
|
|
||||||
template<> struct toml_value_t<floating >{static constexpr value_t value = value_t::Float ;};
|
|
||||||
template<> struct toml_value_t<string >{static constexpr value_t value = value_t::String ;};
|
|
||||||
template<> struct toml_value_t<offset_datetime>{static constexpr value_t value = value_t::OffsetDatetime;};
|
|
||||||
template<> struct toml_value_t<local_datetime >{static constexpr value_t value = value_t::LocalDatetime ;};
|
|
||||||
template<> struct toml_value_t<local_date >{static constexpr value_t value = value_t::LocalDate ;};
|
|
||||||
template<> struct toml_value_t<local_time >{static constexpr value_t value = value_t::LocalTime ;};
|
|
||||||
template<> struct toml_value_t<array >{static constexpr value_t value = value_t::Array ;};
|
|
||||||
template<> struct toml_value_t<table >{static constexpr value_t value = value_t::Table ;};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
struct is_exact_toml_type : disjunction<
|
struct is_exact_toml_type : disjunction<
|
||||||
std::is_same<T, boolean >,
|
std::is_same<T, boolean >,
|
||||||
std::is_same<T, integer >,
|
std::is_same<T, integer >,
|
||||||
@@ -148,13 +127,13 @@ struct is_exact_toml_type : disjunction<
|
|||||||
std::is_same<T, local_datetime >,
|
std::is_same<T, local_datetime >,
|
||||||
std::is_same<T, local_date >,
|
std::is_same<T, local_date >,
|
||||||
std::is_same<T, local_time >,
|
std::is_same<T, local_time >,
|
||||||
std::is_same<T, array >,
|
std::is_same<T, typename Value::array_type>,
|
||||||
std::is_same<T, table >
|
std::is_same<T, typename Value::table_type>
|
||||||
>{};
|
>{};
|
||||||
template<typename T> struct is_exact_toml_type<T&> : is_exact_toml_type<T>{};
|
template<typename T, typename V> struct is_exact_toml_type<T&, V> : is_exact_toml_type<T, V>{};
|
||||||
template<typename T> struct is_exact_toml_type<T const&> : is_exact_toml_type<T>{};
|
template<typename T, typename V> struct is_exact_toml_type<T const&, V> : is_exact_toml_type<T, V>{};
|
||||||
template<typename T> struct is_exact_toml_type<T volatile&> : is_exact_toml_type<T>{};
|
template<typename T, typename V> struct is_exact_toml_type<T volatile&, V> : is_exact_toml_type<T, V>{};
|
||||||
template<typename T> struct is_exact_toml_type<T const volatile&>: is_exact_toml_type<T>{};
|
template<typename T, typename V> struct is_exact_toml_type<T const volatile&, V>: is_exact_toml_type<T, V>{};
|
||||||
|
|
||||||
} // detail
|
} // detail
|
||||||
} // toml
|
} // toml
|
||||||
|
Reference in New Issue
Block a user