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
24namespace toolbox {
25inline namespace util {
26
27template <typename ValueT>
28struct TypeTraits {
29 template <typename StringT>
30 static constexpr auto from_string(StringT&& s) noexcept(noexcept(ValueT{s}))
31 {
32 return ValueT{s};
33 }
34};
35
36template <typename ValueT>
37 requires std::integral<ValueT>
38struct TypeTraits<ValueT> {
39 static constexpr auto from_string(std::string_view sv) noexcept { return ston<ValueT>(sv); }
40};
41
42template <>
44 static auto from_string(std::string_view sv) noexcept { return stob(sv); }
45};
46
47template <>
49 static auto from_string(std::string_view sv) noexcept { return stod(sv); }
50};
51
52template <>
53struct TypeTraits<std::string_view> {
54 static constexpr auto from_string(std::string_view sv) noexcept { return sv; }
57 static std::string_view from_string(const std::string& s) = delete;
58};
59
60template <>
61struct TypeTraits<std::string> {
62 static std::string from_string(std::string_view sv) { return {sv.data(), sv.size()}; }
63 static std::string from_string(const std::string& s) { return s; }
64};
65
66template <typename TypeT>
67struct is_string : std::is_same<char*, std::remove_cv_t<typename std::decay_t<TypeT>>>::type {};
68
69template <>
70struct is_string<std::string> : std::true_type {};
71
72template <>
73struct is_string<std::string_view> : std::true_type {};
74
75template <typename TypeT, template <typename> class TemplateTypeT>
76struct is_instantiation_of : std::false_type {};
77
78template <typename TypeT, template <typename> class TemplateTypeT>
80
81} // namespace util
82} // namespace toolbox
83
84#endif // TOOLBOX_UTIL_TYPETRAITS_HPP
STL namespace.
bool stob(string_view sv, bool dfl) noexcept
Definition Utility.cpp:26
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
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}))