You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

328 lines
10 KiB

11 months ago
  1. // Copyright Toru Niina 2017.
  2. // Distributed under the MIT License.
  3. #ifndef TOML11_TRAITS_HPP
  4. #define TOML11_TRAITS_HPP
  5. #include "from.hpp"
  6. #include "into.hpp"
  7. #include "version.hpp"
  8. #include <chrono>
  9. #include <forward_list>
  10. #include <string>
  11. #include <tuple>
  12. #include <type_traits>
  13. #include <utility>
  14. #if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
  15. #if __has_include(<string_view>)
  16. #include <string_view>
  17. #endif // has_include(<string_view>)
  18. #endif // cplusplus >= C++17
  19. namespace toml
  20. {
  21. template<typename C, template<typename ...> class T, template<typename ...> class A>
  22. class basic_value;
  23. namespace detail
  24. {
  25. // ---------------------------------------------------------------------------
  26. // check whether type T is a kind of container/map class
  27. struct has_iterator_impl
  28. {
  29. template<typename T> static std::true_type check(typename T::iterator*);
  30. template<typename T> static std::false_type check(...);
  31. };
  32. struct has_value_type_impl
  33. {
  34. template<typename T> static std::true_type check(typename T::value_type*);
  35. template<typename T> static std::false_type check(...);
  36. };
  37. struct has_key_type_impl
  38. {
  39. template<typename T> static std::true_type check(typename T::key_type*);
  40. template<typename T> static std::false_type check(...);
  41. };
  42. struct has_mapped_type_impl
  43. {
  44. template<typename T> static std::true_type check(typename T::mapped_type*);
  45. template<typename T> static std::false_type check(...);
  46. };
  47. struct has_reserve_method_impl
  48. {
  49. template<typename T> static std::false_type check(...);
  50. template<typename T> static std::true_type check(
  51. decltype(std::declval<T>().reserve(std::declval<std::size_t>()))*);
  52. };
  53. struct has_push_back_method_impl
  54. {
  55. template<typename T> static std::false_type check(...);
  56. template<typename T> static std::true_type check(
  57. decltype(std::declval<T>().push_back(std::declval<typename T::value_type>()))*);
  58. };
  59. struct is_comparable_impl
  60. {
  61. template<typename T> static std::false_type check(...);
  62. template<typename T> static std::true_type check(
  63. decltype(std::declval<T>() < std::declval<T>())*);
  64. };
  65. struct has_from_toml_method_impl
  66. {
  67. template<typename T, typename C,
  68. template<typename ...> class Tb, template<typename ...> class A>
  69. static std::true_type check(
  70. decltype(std::declval<T>().from_toml(
  71. std::declval<::toml::basic_value<C, Tb, A>>()))*);
  72. template<typename T, typename C,
  73. template<typename ...> class Tb, template<typename ...> class A>
  74. static std::false_type check(...);
  75. };
  76. struct has_into_toml_method_impl
  77. {
  78. template<typename T>
  79. static std::true_type check(decltype(std::declval<T>().into_toml())*);
  80. template<typename T>
  81. static std::false_type check(...);
  82. };
  83. struct has_specialized_from_impl
  84. {
  85. template<typename T>
  86. static std::false_type check(...);
  87. template<typename T, std::size_t S = sizeof(::toml::from<T>)>
  88. static std::true_type check(::toml::from<T>*);
  89. };
  90. struct has_specialized_into_impl
  91. {
  92. template<typename T>
  93. static std::false_type check(...);
  94. template<typename T, std::size_t S = sizeof(::toml::into<T>)>
  95. static std::true_type check(::toml::from<T>*);
  96. };
  97. /// Intel C++ compiler can not use decltype in parent class declaration, here
  98. /// is a hack to work around it. https://stackoverflow.com/a/23953090/4692076
  99. #ifdef __INTEL_COMPILER
  100. #define decltype(...) std::enable_if<true, decltype(__VA_ARGS__)>::type
  101. #endif
  102. template<typename T>
  103. struct has_iterator : decltype(has_iterator_impl::check<T>(nullptr)){};
  104. template<typename T>
  105. struct has_value_type : decltype(has_value_type_impl::check<T>(nullptr)){};
  106. template<typename T>
  107. struct has_key_type : decltype(has_key_type_impl::check<T>(nullptr)){};
  108. template<typename T>
  109. struct has_mapped_type : decltype(has_mapped_type_impl::check<T>(nullptr)){};
  110. template<typename T>
  111. struct has_reserve_method : decltype(has_reserve_method_impl::check<T>(nullptr)){};
  112. template<typename T>
  113. struct has_push_back_method : decltype(has_push_back_method_impl::check<T>(nullptr)){};
  114. template<typename T>
  115. struct is_comparable : decltype(is_comparable_impl::check<T>(nullptr)){};
  116. template<typename T, typename C,
  117. template<typename ...> class Tb, template<typename ...> class A>
  118. struct has_from_toml_method
  119. : decltype(has_from_toml_method_impl::check<T, C, Tb, A>(nullptr)){};
  120. template<typename T>
  121. struct has_into_toml_method
  122. : decltype(has_into_toml_method_impl::check<T>(nullptr)){};
  123. template<typename T>
  124. struct has_specialized_from : decltype(has_specialized_from_impl::check<T>(nullptr)){};
  125. template<typename T>
  126. struct has_specialized_into : decltype(has_specialized_into_impl::check<T>(nullptr)){};
  127. #ifdef __INTEL_COMPILER
  128. #undef decltype
  129. #endif
  130. // ---------------------------------------------------------------------------
  131. // C++17 and/or/not
  132. #if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
  133. using std::conjunction;
  134. using std::disjunction;
  135. using std::negation;
  136. #else
  137. template<typename ...> struct conjunction : std::true_type{};
  138. template<typename T> struct conjunction<T> : T{};
  139. template<typename T, typename ... Ts>
  140. struct conjunction<T, Ts...> :
  141. std::conditional<static_cast<bool>(T::value), conjunction<Ts...>, T>::type
  142. {};
  143. template<typename ...> struct disjunction : std::false_type{};
  144. template<typename T> struct disjunction<T> : T {};
  145. template<typename T, typename ... Ts>
  146. struct disjunction<T, Ts...> :
  147. std::conditional<static_cast<bool>(T::value), T, disjunction<Ts...>>::type
  148. {};
  149. template<typename T>
  150. struct negation : std::integral_constant<bool, !static_cast<bool>(T::value)>{};
  151. #endif
  152. // ---------------------------------------------------------------------------
  153. // type checkers
  154. template<typename T> struct is_std_pair : std::false_type{};
  155. template<typename T1, typename T2>
  156. struct is_std_pair<std::pair<T1, T2>> : std::true_type{};
  157. template<typename T> struct is_std_tuple : std::false_type{};
  158. template<typename ... Ts>
  159. struct is_std_tuple<std::tuple<Ts...>> : std::true_type{};
  160. template<typename T> struct is_std_forward_list : std::false_type{};
  161. template<typename T>
  162. struct is_std_forward_list<std::forward_list<T>> : std::true_type{};
  163. template<typename T> struct is_chrono_duration: std::false_type{};
  164. template<typename Rep, typename Period>
  165. struct is_chrono_duration<std::chrono::duration<Rep, Period>>: std::true_type{};
  166. template<typename T>
  167. struct is_map : conjunction< // map satisfies all the following conditions
  168. has_iterator<T>, // has T::iterator
  169. has_value_type<T>, // has T::value_type
  170. has_key_type<T>, // has T::key_type
  171. has_mapped_type<T> // has T::mapped_type
  172. >{};
  173. template<typename T> struct is_map<T&> : is_map<T>{};
  174. template<typename T> struct is_map<T const&> : is_map<T>{};
  175. template<typename T> struct is_map<T volatile&> : is_map<T>{};
  176. template<typename T> struct is_map<T const volatile&> : is_map<T>{};
  177. template<typename T>
  178. struct is_container : conjunction<
  179. negation<is_map<T>>, // not a map
  180. negation<std::is_same<T, std::string>>, // not a std::string
  181. #if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L
  182. #if __has_include(<string_view>)
  183. negation<std::is_same<T, std::string_view>>, // not a std::string_view
  184. #endif // has_include(<string_view>)
  185. #endif
  186. has_iterator<T>, // has T::iterator
  187. has_value_type<T> // has T::value_type
  188. >{};
  189. template<typename T> struct is_container<T&> : is_container<T>{};
  190. template<typename T> struct is_container<T const&> : is_container<T>{};
  191. template<typename T> struct is_container<T volatile&> : is_container<T>{};
  192. template<typename T> struct is_container<T const volatile&> : is_container<T>{};
  193. template<typename T>
  194. struct is_basic_value: std::false_type{};
  195. template<typename T> struct is_basic_value<T&> : is_basic_value<T>{};
  196. template<typename T> struct is_basic_value<T const&> : is_basic_value<T>{};
  197. template<typename T> struct is_basic_value<T volatile&> : is_basic_value<T>{};
  198. template<typename T> struct is_basic_value<T const volatile&> : is_basic_value<T>{};
  199. template<typename C, template<typename ...> class M, template<typename ...> class V>
  200. struct is_basic_value<::toml::basic_value<C, M, V>>: std::true_type{};
  201. // ---------------------------------------------------------------------------
  202. // C++14 index_sequence
  203. #if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201402L
  204. using std::index_sequence;
  205. using std::make_index_sequence;
  206. #else
  207. template<std::size_t ... Ns> struct index_sequence{};
  208. template<typename IS, std::size_t N> struct push_back_index_sequence{};
  209. template<std::size_t N, std::size_t ... Ns>
  210. struct push_back_index_sequence<index_sequence<Ns...>, N>
  211. {
  212. typedef index_sequence<Ns..., N> type;
  213. };
  214. template<std::size_t N>
  215. struct index_sequence_maker
  216. {
  217. typedef typename push_back_index_sequence<
  218. typename index_sequence_maker<N-1>::type, N>::type type;
  219. };
  220. template<>
  221. struct index_sequence_maker<0>
  222. {
  223. typedef index_sequence<0> type;
  224. };
  225. template<std::size_t N>
  226. using make_index_sequence = typename index_sequence_maker<N-1>::type;
  227. #endif // cplusplus >= 2014
  228. // ---------------------------------------------------------------------------
  229. // C++14 enable_if_t
  230. #if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201402L
  231. using std::enable_if_t;
  232. #else
  233. template<bool B, typename T>
  234. using enable_if_t = typename std::enable_if<B, T>::type;
  235. #endif // cplusplus >= 2014
  236. // ---------------------------------------------------------------------------
  237. // return_type_of_t
  238. #if TOML11_CPLUSPLUS_STANDARD_VERSION >= 201703L && defined(__cpp_lib_is_invocable) && __cpp_lib_is_invocable>=201703
  239. template<typename F, typename ... Args>
  240. using return_type_of_t = std::invoke_result_t<F, Args...>;
  241. #else
  242. // result_of is deprecated after C++17
  243. template<typename F, typename ... Args>
  244. using return_type_of_t = typename std::result_of<F(Args...)>::type;
  245. #endif
  246. // ---------------------------------------------------------------------------
  247. // is_string_literal
  248. //
  249. // to use this, pass `typename remove_reference<T>::type` to T.
  250. template<typename T>
  251. struct is_string_literal:
  252. disjunction<
  253. std::is_same<const char*, T>,
  254. conjunction<
  255. std::is_array<T>,
  256. std::is_same<const char, typename std::remove_extent<T>::type>
  257. >
  258. >{};
  259. // ---------------------------------------------------------------------------
  260. // C++20 remove_cvref_t
  261. template<typename T>
  262. struct remove_cvref
  263. {
  264. using type = typename std::remove_cv<
  265. typename std::remove_reference<T>::type>::type;
  266. };
  267. template<typename T>
  268. using remove_cvref_t = typename remove_cvref<T>::type;
  269. }// detail
  270. }//toml
  271. #endif // TOML_TRAITS