Line data Source code
1 : /**
2 : Copyright (c) 2020-2022 Roman Katuntsev <sbkarr@stappler.org>
3 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
4 :
5 : Permission is hereby granted, free of charge, to any person obtaining a copy
6 : of this software and associated documentation files (the "Software"), to deal
7 : in the Software without restriction, including without limitation the rights
8 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 : copies of the Software, and to permit persons to whom the Software is
10 : furnished to do so, subject to the following conditions:
11 :
12 : The above copyright notice and this permission notice shall be included in
13 : all copies or substantial portions of the Software.
14 :
15 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 : THE SOFTWARE.
22 : **/
23 :
24 : #ifndef STAPPLER_CORE_MEMORY_SPMEMINTERFACE_H_
25 : #define STAPPLER_CORE_MEMORY_SPMEMINTERFACE_H_
26 :
27 : #include "SPMemDict.h"
28 : #include "SPMemFunction.h"
29 : #include "SPMemMap.h"
30 : #include "SPMemSet.h"
31 : #include "SPMemString.h"
32 : #include "SPMemStringStream.h"
33 : #include "SPMemVector.h"
34 :
35 : namespace STAPPLER_VERSIONIZED stappler::memory {
36 :
37 : struct PoolInterface : public memory::AllocPool {
38 : using AllocBaseType = memory::AllocPool;
39 : using StringType = memory::string;
40 : using WideStringType = memory::u16string;
41 : using BytesType = memory::vector<uint8_t>;
42 :
43 : template <typename Value> using BasicStringType = memory::basic_string<Value>;
44 : template <typename Value> using ArrayType = memory::vector<Value>;
45 : template <typename Value> using DictionaryType = memory::map<StringType, Value, std::less<>>;
46 : template <typename Value> using VectorType = memory::vector<Value>;
47 :
48 : template <typename K, typename V, typename Compare = std::less<>>
49 : using MapType = memory::map<K, V, Compare>;
50 :
51 : template <typename T, typename Compare = std::less<>>
52 : using SetType = memory::set<T, Compare>;
53 :
54 : template <typename T>
55 : using FunctionType = memory::function<T>;
56 :
57 : using StringStreamType = memory::ostringstream;
58 :
59 : static constexpr bool usesMemoryPool() { return true; }
60 : };
61 :
62 : struct StandartInterface : public memory::AllocBase {
63 : using AllocBaseType = memory::AllocBase;
64 : using StringType = std::string;
65 : using WideStringType = std::u16string;
66 : using BytesType = std::vector<uint8_t>;
67 :
68 : template <typename Value> using BasicStringType = std::basic_string<Value>;
69 : template <typename Value> using ArrayType = std::vector<Value>;
70 : template <typename Value> using DictionaryType = std::map<StringType, Value, std::less<>>;
71 : template <typename Value> using VectorType = std::vector<Value>;
72 :
73 : template <typename K, typename V, typename Compare = std::less<>>
74 : using MapType = std::map<K, V, Compare>;
75 :
76 : template <typename T, typename Compare = std::less<>>
77 : using SetType = std::set<T, Compare>;
78 :
79 : template <typename T>
80 : using FunctionType = std::function<T>;
81 :
82 : using StringStreamType = std::ostringstream;
83 :
84 : static constexpr bool usesMemoryPool() { return false; }
85 : };
86 :
87 : }
88 :
89 : namespace STAPPLER_VERSIONIZED stappler {
90 :
91 : template <typename InterfaceType>
92 : struct InterfaceObject {
93 : using Interface = InterfaceType;
94 :
95 : using String = typename Interface::StringType;
96 : using WideString = typename Interface::WideStringType;
97 : using Bytes = typename Interface::BytesType;
98 :
99 : template <typename Value>
100 : using BasicString = typename Interface::template BasicStringType<Value>;
101 :
102 : template <typename Value>
103 : using Vector = typename Interface::template VectorType<Value>;
104 :
105 : template <typename K, typename V, typename Compare = std::less<>>
106 : using Map = typename Interface::template MapType<K, V, Compare>;
107 :
108 : template <typename T, typename Compare = std::less<>>
109 : using Set = typename Interface::template SetType<T, Compare>;
110 :
111 : template <typename T>
112 : using Function = typename Interface::template FunctionType<T>;
113 :
114 : using StringStream = typename Interface::StringStreamType;
115 : };
116 :
117 : }
118 :
119 : namespace STAPPLER_VERSIONIZED stappler::traits {
120 :
121 : template <typename StringType>
122 : struct SelectStringStream;
123 :
124 : template <>
125 : struct SelectStringStream<std::string> {
126 : using Type = std::ostringstream;
127 : };
128 :
129 : template <>
130 : struct SelectStringStream<std::u16string> {
131 : using Type = std::basic_ostringstream<char16_t>;
132 : };
133 :
134 : template <>
135 : struct SelectStringStream<memory::string> {
136 : using Type = memory::ostringstream;
137 : };
138 :
139 : template <>
140 : struct SelectStringStream<memory::basic_string<char16_t>> {
141 : using Type = memory::basic_ostringstream<char16_t>;
142 : };
143 :
144 :
145 : }
146 :
147 : namespace STAPPLER_VERSIONIZED stappler {
148 :
149 : template <typename T>
150 : using Callback = memory::callback<T>;
151 :
152 150 : template <typename T> auto StringToNumber(const memory::StandartInterface::StringType &str) -> T {
153 150 : return StringToNumber<T>(str.data(), nullptr, 0);
154 : }
155 :
156 : template <typename T> auto StringToNumber(const memory::PoolInterface::StringType &str) -> T {
157 : return StringToNumber<T>(str.data(), nullptr, 0);
158 : }
159 :
160 175 : template <typename T> auto StringToNumber(const char *str) -> T {
161 175 : return StringToNumber<T>(str, nullptr, 0);
162 : }
163 :
164 : }
165 :
166 : #endif /* STAPPLER_CORE_MEMORY_SPMEMINTERFACE_H_ */
|