318
|
1 // Copyright Toru Niina 2017.
|
|
2 // Distributed under the MIT License.
|
|
3 #ifndef TOML11_TYPES_HPP
|
|
4 #define TOML11_TYPES_HPP
|
|
5 #include <unordered_map>
|
|
6 #include <vector>
|
|
7
|
|
8 #include "comments.hpp"
|
|
9 #include "datetime.hpp"
|
|
10 #include "string.hpp"
|
|
11 #include "traits.hpp"
|
|
12
|
|
13 namespace toml
|
|
14 {
|
|
15
|
|
16 template<typename Comment, // discard/preserve_comment
|
|
17 template<typename ...> class Table, // map-like class
|
|
18 template<typename ...> class Array> // vector-like class
|
|
19 class basic_value;
|
|
20
|
|
21 using character = char;
|
|
22 using key = std::string;
|
|
23
|
|
24 #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ <= 4
|
|
25 # pragma GCC diagnostic push
|
|
26 # pragma GCC diagnostic ignored "-Wshadow"
|
|
27 #endif
|
|
28
|
|
29 using boolean = bool;
|
|
30 using integer = std::int64_t;
|
|
31 using floating = double; // "float" is a keyword, cannot use it here.
|
|
32 // the following stuffs are structs defined here, so aliases are not needed.
|
|
33 // - string
|
|
34 // - offset_datetime
|
|
35 // - offset_datetime
|
|
36 // - local_datetime
|
|
37 // - local_date
|
|
38 // - local_time
|
|
39
|
|
40 #if defined(__GNUC__) && !defined(__clang__)
|
|
41 # pragma GCC diagnostic pop
|
|
42 #endif
|
|
43
|
|
44 // default toml::value and default array/table. these are defined after defining
|
|
45 // basic_value itself.
|
|
46 // using value = basic_value<discard_comments, std::unordered_map, std::vector>;
|
|
47 // using array = typename value::array_type;
|
|
48 // using table = typename value::table_type;
|
|
49
|
|
50 // to avoid warnings about `value_t::integer` is "shadowing" toml::integer in
|
|
51 // GCC -Wshadow=global.
|
|
52 #if defined(__GNUC__) && !defined(__clang__)
|
|
53 # pragma GCC diagnostic push
|
|
54 # if 7 <= __GNUC__
|
|
55 # pragma GCC diagnostic ignored "-Wshadow=global"
|
|
56 # else // gcc-6 or older
|
|
57 # pragma GCC diagnostic ignored "-Wshadow"
|
|
58 # endif
|
|
59 #endif
|
|
60 enum class value_t : std::uint8_t
|
|
61 {
|
|
62 empty = 0,
|
|
63 boolean = 1,
|
|
64 integer = 2,
|
|
65 floating = 3,
|
|
66 string = 4,
|
|
67 offset_datetime = 5,
|
|
68 local_datetime = 6,
|
|
69 local_date = 7,
|
|
70 local_time = 8,
|
|
71 array = 9,
|
|
72 table = 10,
|
|
73 };
|
|
74 #if defined(__GNUC__) && !defined(__clang__)
|
|
75 # pragma GCC diagnostic pop
|
|
76 #endif
|
|
77
|
|
78 template<typename charT, typename traits>
|
|
79 inline std::basic_ostream<charT, traits>&
|
|
80 operator<<(std::basic_ostream<charT, traits>& os, value_t t)
|
|
81 {
|
|
82 switch(t)
|
|
83 {
|
|
84 case value_t::boolean : os << "boolean"; return os;
|
|
85 case value_t::integer : os << "integer"; return os;
|
|
86 case value_t::floating : os << "floating"; return os;
|
|
87 case value_t::string : os << "string"; return os;
|
|
88 case value_t::offset_datetime : os << "offset_datetime"; return os;
|
|
89 case value_t::local_datetime : os << "local_datetime"; return os;
|
|
90 case value_t::local_date : os << "local_date"; return os;
|
|
91 case value_t::local_time : os << "local_time"; return os;
|
|
92 case value_t::array : os << "array"; return os;
|
|
93 case value_t::table : os << "table"; return os;
|
|
94 case value_t::empty : os << "empty"; return os;
|
|
95 default : os << "unknown"; return os;
|
|
96 }
|
|
97 }
|
|
98
|
|
99 template<typename charT = char,
|
|
100 typename traits = std::char_traits<charT>,
|
|
101 typename alloc = std::allocator<charT>>
|
|
102 inline std::basic_string<charT, traits, alloc> stringize(value_t t)
|
|
103 {
|
|
104 std::basic_ostringstream<charT, traits, alloc> oss;
|
|
105 oss << t;
|
|
106 return oss.str();
|
|
107 }
|
|
108
|
|
109 namespace detail
|
|
110 {
|
|
111
|
|
112 // helper to define a type that represents a value_t value.
|
|
113 template<value_t V>
|
|
114 using value_t_constant = std::integral_constant<value_t, V>;
|
|
115
|
|
116 // meta-function that convertes from value_t to the exact toml type that corresponds to.
|
|
117 // It takes toml::basic_value type because array and table types depend on it.
|
|
118 template<value_t t, typename Value> struct enum_to_type {using type = void ;};
|
|
119 template<typename Value> struct enum_to_type<value_t::empty , Value>{using type = void ;};
|
|
120 template<typename Value> struct enum_to_type<value_t::boolean , Value>{using type = boolean ;};
|
|
121 template<typename Value> struct enum_to_type<value_t::integer , Value>{using type = integer ;};
|
|
122 template<typename Value> struct enum_to_type<value_t::floating , Value>{using type = floating ;};
|
|
123 template<typename Value> struct enum_to_type<value_t::string , Value>{using type = string ;};
|
|
124 template<typename Value> struct enum_to_type<value_t::offset_datetime, Value>{using type = offset_datetime ;};
|
|
125 template<typename Value> struct enum_to_type<value_t::local_datetime , Value>{using type = local_datetime ;};
|
|
126 template<typename Value> struct enum_to_type<value_t::local_date , Value>{using type = local_date ;};
|
|
127 template<typename Value> struct enum_to_type<value_t::local_time , Value>{using type = local_time ;};
|
|
128 template<typename Value> struct enum_to_type<value_t::array , Value>{using type = typename Value::array_type;};
|
|
129 template<typename Value> struct enum_to_type<value_t::table , Value>{using type = typename Value::table_type;};
|
|
130
|
|
131 // meta-function that converts from an exact toml type to the enum that corresponds to.
|
|
132 template<typename T, typename Value>
|
|
133 struct type_to_enum : std::conditional<
|
|
134 std::is_same<T, typename Value::array_type>::value, // if T == array_type,
|
|
135 value_t_constant<value_t::array>, // then value_t::array
|
|
136 typename std::conditional< // else...
|
|
137 std::is_same<T, typename Value::table_type>::value, // if T == table_type
|
|
138 value_t_constant<value_t::table>, // then value_t::table
|
|
139 value_t_constant<value_t::empty> // else value_t::empty
|
|
140 >::type
|
|
141 >::type {};
|
|
142 template<typename Value> struct type_to_enum<boolean , Value>: value_t_constant<value_t::boolean > {};
|
|
143 template<typename Value> struct type_to_enum<integer , Value>: value_t_constant<value_t::integer > {};
|
|
144 template<typename Value> struct type_to_enum<floating , Value>: value_t_constant<value_t::floating > {};
|
|
145 template<typename Value> struct type_to_enum<string , Value>: value_t_constant<value_t::string > {};
|
|
146 template<typename Value> struct type_to_enum<offset_datetime, Value>: value_t_constant<value_t::offset_datetime> {};
|
|
147 template<typename Value> struct type_to_enum<local_datetime , Value>: value_t_constant<value_t::local_datetime > {};
|
|
148 template<typename Value> struct type_to_enum<local_date , Value>: value_t_constant<value_t::local_date > {};
|
|
149 template<typename Value> struct type_to_enum<local_time , Value>: value_t_constant<value_t::local_time > {};
|
|
150
|
|
151 // meta-function that checks the type T is the same as one of the toml::* types.
|
|
152 template<typename T, typename Value>
|
|
153 struct is_exact_toml_type : disjunction<
|
|
154 std::is_same<T, boolean >,
|
|
155 std::is_same<T, integer >,
|
|
156 std::is_same<T, floating >,
|
|
157 std::is_same<T, string >,
|
|
158 std::is_same<T, offset_datetime>,
|
|
159 std::is_same<T, local_datetime >,
|
|
160 std::is_same<T, local_date >,
|
|
161 std::is_same<T, local_time >,
|
|
162 std::is_same<T, typename Value::array_type>,
|
|
163 std::is_same<T, typename Value::table_type>
|
|
164 >{};
|
|
165 template<typename T, typename V> struct is_exact_toml_type<T&, V> : is_exact_toml_type<T, V>{};
|
|
166 template<typename T, typename V> struct is_exact_toml_type<T const&, V> : is_exact_toml_type<T, V>{};
|
|
167 template<typename T, typename V> struct is_exact_toml_type<T volatile&, V> : is_exact_toml_type<T, V>{};
|
|
168 template<typename T, typename V> struct is_exact_toml_type<T const volatile&, V>: is_exact_toml_type<T, V>{};
|
|
169
|
|
170 } // detail
|
|
171 } // toml
|
|
172
|
|
173 #endif// TOML11_TYPES_H
|