Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
TypeTraits.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2022 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_UTIL_TYPETRAITS_HPP
18#define TOOLBOX_UTIL_TYPETRAITS_HPP
19
21
22#include <string>
23#include <type_traits>
24
25namespace toolbox {
26inline namespace util {
27
28template <typename ValueT>
29struct TypeTraits {
30 template <typename StringT>
31 static constexpr auto from_string(StringT&& s) noexcept(noexcept(ValueT{s}))
32 {
33 return ValueT{s};
34 }
35};
36
37template <typename ValueT>
38 requires std::integral<ValueT>
39struct TypeTraits<ValueT> {
40 static constexpr auto from_string(std::string_view sv) noexcept { return ston<ValueT>(sv); }
41};
42
43template <>
45 static auto from_string(std::string_view sv) noexcept { return stob(sv); }
46};
47
48template <>
50 static auto from_string(std::string_view sv) noexcept { return stod(sv); }
51};
52
53template <>
54struct TypeTraits<std::string_view> {
55 static constexpr auto from_string(std::string_view sv) noexcept { return sv; }
58 static std::string_view from_string(const std::string& s) = delete;
59};
60
61template <>
62struct TypeTraits<std::string> {
63 static std::string from_string(std::string_view sv) { return {sv.data(), sv.size()}; }
64 static std::string from_string(const std::string& s) { return s; }
65};
66
67template <typename T, template <typename...> class Tpl>
68struct is_instantiation_of_helper : std::false_type {};
69
70template <typename... Ts, template <typename...> class Tpl>
71struct is_instantiation_of_helper<Tpl<Ts...>, Tpl> : std::true_type {};
72
73template <typename T, template <typename...> class Tpl>
74struct is_instantiation_of : is_instantiation_of_helper<std::remove_cv_t<T>, Tpl> {};
75
76template <typename T, template <typename...> class Tpl>
78
79template <typename T>
81
82template <typename T>
83inline constexpr bool is_string_v = is_string<T>::value;
84
85template <typename T>
87
88template <typename T>
90
91template <typename T>
92struct is_decay_to_cstring_helper : std::false_type {};
93
94template <typename T>
95struct is_decay_to_cstring_helper<T*> : std::is_same<std::remove_cv_t<T>, char> {};
96
97template <typename T>
98struct is_decay_to_cstring : is_decay_to_cstring_helper<std::remove_cv_t<std::decay_t<T>>> {};
99
100template <typename T>
102
103template <typename T>
104struct is_string_type : std::integral_constant<bool, is_string_v<T> ||
105 is_string_view_v<T> ||
106 is_decay_to_cstring_v<T>> {};
107
108template <typename T>
110
111} // namespace util
112} // namespace toolbox
113
114#endif // TOOLBOX_UTIL_TYPETRAITS_HPP
STL namespace.
bool stob(string_view sv, bool dfl) noexcept
Definition Utility.cpp:26
constexpr bool is_decay_to_cstring_v
constexpr bool is_instantiation_of_v
constexpr bool is_string_type_v
constexpr bool is_string_view_v
std::string_view sv
Definition Tokeniser.hpp:26
double stod(std::string_view sv, double dfl) noexcept
Definition Utility.cpp:89
constexpr auto bind() noexcept
Definition Slot.hpp:92
constexpr bool is_string_v
static constexpr auto from_string(std::string_view sv) noexcept
static auto from_string(std::string_view sv) noexcept
static auto from_string(std::string_view sv) noexcept
static std::string from_string(const std::string &s)
static std::string from_string(std::string_view sv)
static constexpr auto from_string(std::string_view sv) noexcept
static std::string_view from_string(const std::string &s)=delete
static constexpr auto from_string(StringT &&s) noexcept(noexcept(ValueT{s}))