SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_strings.h
1#ifndef SDL3PP_STRINGS_H_
2#define SDL3PP_STRINGS_H_
3
4#include <span>
5#include <string>
6#include <string_view>
7#include <variant>
8#include <SDL3/SDL_stdinc.h>
9#include "SDL3pp_ownPtr.h"
10
11namespace SDL {
12
21#ifndef SDL3PP_ENABLE_STRING_PARAM
22
23#ifndef SDL3PP_DISABLE_STRING_PARAM
24#define SDL3PP_ENABLE_STRING_PARAM
25#endif // SDL3PP_DISABLE_STRING_PARAM
26
27#endif // SDL3PP_ENABLE_STRING_PARAM
28
29#ifdef SDL3PP_ENABLE_STRING_PARAM
30
43{
44 std::variant<const char*, std::string> data;
45
46public:
48 constexpr StringParam(std::nullptr_t = nullptr)
49 : data("")
50 {
51 }
52
62 constexpr StringParam(const char* str)
63 : data(str ?: "")
64 {
65 }
66
77 constexpr StringParam(const std::string& str)
78 : StringParam(str.c_str())
79 {
80 }
81
90 constexpr StringParam(std::string&& str)
91 : data(std::move(str))
92 {
93 }
94
104 StringParam(std::string_view str)
105 : StringParam(std::string{str})
106 {
107 }
108
109 StringParam(const StringParam&) = delete;
110 StringParam& operator=(const StringParam&) = delete;
111
114
117
124 constexpr const char* c_str() const
125 {
126 struct Visitor
127 {
128 const char* operator()(const char* a) const { return a; }
129 const char* operator()(const std::string& s) const { return s.c_str(); }
130 };
131 return std::visit(Visitor{}, data);
132 }
133
140 constexpr operator const char*() const { return c_str(); }
141};
142
143#else // SDL3PP_ENABLE_STRING_PARAM
144
145using StringParam = const char*;
146
147#endif // SDL3PP_ENABLE_STRING_PARAM
148
152struct StringResult : OwnArray<char>
153{
154
156 using OwnArray::OwnArray;
157
160 : StringResult(std::string_view(other))
161 {
162 }
163
165 constexpr StringResult(StringResult&& other)
166 : OwnArray(other.release(), other.size())
167 {
168 }
169
171 StringResult(std::string_view other)
172 : OwnArray(other.empty() ? nullptr
173 : SDL_strndup(other.data(), other.size()))
174 {
175 }
176
178 constexpr operator StringParam() const { return std::string_view{*this}; }
179
181 constexpr operator std::string_view() const
182 {
183 return std::string_view{data(), size()};
184 }
185
187 StringResult& operator+=(std::string_view other)
188 {
189 if (empty()) {
190 reset(StringResult(other).release());
191 } else if (!other.empty()) {
192 size_t lhsSz = size();
193 size_t rhsSz = other.size();
194 size_t finalSize = lhsSz + lhsSz + 1;
195 auto newBuf = static_cast<char*>(SDL_realloc(data(), finalSize));
196 newBuf += lhsSz;
197 SDL_memcpy(newBuf, other.data(), rhsSz);
198 newBuf += rhsSz;
199 *newBuf = 0;
200 reset(newBuf, finalSize - 1);
201 }
202 return *this;
203 }
204
207 {
208 return (*this) += std::string_view{&ch, 1};
209 }
210
212 StringResult operator+(std::string_view other) const
213 {
214 StringResult t{*this};
215 t += other;
216 return t;
217 }
218
220 StringResult operator+(char ch) { return (*this) + std::string_view{&ch, 1}; }
221
223 std::string str() const { return std::string{data(), size()}; }
224
226 const char* c_str() const
227 {
228 if (empty()) return "";
229 return data();
230 }
231};
232
239{
240 const void* data;
241 size_t size_bytes;
242
244 constexpr SourceBytes() = default;
245
247 constexpr SourceBytes(std::nullptr_t)
248 : SourceBytes()
249 {
250 }
251
258 template<class T, size_t N>
259 constexpr SourceBytes(std::span<T, N> span)
260 : SourceBytes(span.data(), span.size_bytes())
261 {
262 }
263
269 template<class T>
270 constexpr SourceBytes(const T& data)
271 : SourceBytes(std::span{data.data(), data.size()})
272 {
273 }
279 template<class T, size_t N>
280 constexpr SourceBytes(T (&data)[N])
281 : SourceBytes(std::span<T, N>{static_cast<T*>(data), N})
282 {
283 }
284
292 constexpr SourceBytes(const void* data, size_t size_bytes)
293 : data(size_bytes > 0 ? data : nullptr)
294 , size_bytes(data != nullptr ? size_bytes : 0)
295 {
296 }
297};
298
305{
306 void* data;
307 size_t size_bytes;
308
310 constexpr TargetBytes() = default;
311
313 constexpr TargetBytes(std::nullptr_t)
314 : TargetBytes()
315 {
316 }
317
319 template<class T, size_t N>
320 constexpr TargetBytes(std::span<const T, N> span)
321 {
322 static_assert(false, "Non-const type is required");
323 }
324
331 template<class T, size_t N>
332 constexpr TargetBytes(std::span<T, N> span)
333 : TargetBytes(span.data(), span.size_bytes())
334 {
335 }
336
342 template<class T>
343 constexpr TargetBytes(T& data)
344 : TargetBytes(std::span{data.data(), data.size()})
345 {
346 }
352 template<class T, size_t N>
353 constexpr TargetBytes(T (&data)[N])
354 : TargetBytes(std::span<T, N>{static_cast<T*>(data), N})
355 {
356 }
357
365 constexpr TargetBytes(void* data, size_t size_bytes)
366 : data(size_bytes > 0 ? data : nullptr)
367 , size_bytes(data != nullptr ? size_bytes : 0)
368 {
369 }
370};
371
373
374} // namespace SDL
375
376#endif /* SDL3PP_STRINGS_H_ */
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:43
constexpr bool empty() const
True if size() == 0.
Definition SDL3pp_ownPtr.h:70
char * release()
Release control on object.
Definition SDL3pp_ownPtr.h:87
void reset(char *newValue=nullptr)
Reset object.
Definition SDL3pp_ownPtr.h:95
constexpr size_t size() const
Size.
Definition SDL3pp_ownPtr.h:79
constexpr char * data()
Data.
Definition SDL3pp_ownPtr.h:73
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:43
constexpr const char * c_str() const
Converts to a null terminated C string.
Definition SDL3pp_strings.h:124
StringParam & operator=(StringParam &&)=default
Move assignment.
constexpr StringParam(const char *str)
Constructs from a C string.
Definition SDL3pp_strings.h:62
StringParam(std::string_view str)
Constructs from std::string_view object.
Definition SDL3pp_strings.h:104
constexpr StringParam(std::string &&str)
Constructs from std::string object.
Definition SDL3pp_strings.h:90
constexpr StringParam(const std::string &str)
Constructs from std::string reference.
Definition SDL3pp_strings.h:77
StringParam(StringParam &&)=default
Move ctor.
constexpr StringParam(std::nullptr_t=nullptr)
Default ctor.
Definition SDL3pp_strings.h:48
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
Source byte stream.
Definition SDL3pp_strings.h:239
constexpr SourceBytes(std::span< T, N > span)
From span.
Definition SDL3pp_strings.h:259
constexpr SourceBytes(const T &data)
From container-like.
Definition SDL3pp_strings.h:270
constexpr SourceBytes(std::nullptr_t)
Disambiguate between multiple pointer types.
Definition SDL3pp_strings.h:247
size_t size_bytes
The size in bytes.
Definition SDL3pp_strings.h:241
constexpr SourceBytes(const void *data, size_t size_bytes)
From data + size in bytes.
Definition SDL3pp_strings.h:292
constexpr SourceBytes()=default
Default ctor.
constexpr SourceBytes(T(&data)[N])
From array.
Definition SDL3pp_strings.h:280
const void * data
The data copied from.
Definition SDL3pp_strings.h:240
A simple std::string-like interface for SDL allocated strings.
Definition SDL3pp_strings.h:153
StringResult(std::string_view other)
Constructs from string view.
Definition SDL3pp_strings.h:171
const char * c_str() const
Convert to c-string.
Definition SDL3pp_strings.h:226
constexpr StringResult(StringResult &&other)
Move ctor.
Definition SDL3pp_strings.h:165
StringResult & operator+=(std::string_view other)
Append string.
Definition SDL3pp_strings.h:187
std::string str() const
Convert to string.
Definition SDL3pp_strings.h:223
StringResult(const StringResult &other)
Copy ctor.
Definition SDL3pp_strings.h:159
StringResult operator+(std::string_view other) const
Append string.
Definition SDL3pp_strings.h:212
StringResult & operator+=(char ch)
Append char.
Definition SDL3pp_strings.h:206
StringResult operator+(char ch)
Append char.
Definition SDL3pp_strings.h:220
constexpr operator std::string_view() const
Convert to std::string_view.
Definition SDL3pp_strings.h:181
Target byte stream.
Definition SDL3pp_strings.h:305
constexpr TargetBytes(T &data)
From container-like.
Definition SDL3pp_strings.h:343
constexpr TargetBytes(std::span< T, N > span)
From span.
Definition SDL3pp_strings.h:332
constexpr TargetBytes()=default
Default ctor.
constexpr TargetBytes(std::nullptr_t)
Disambiguate between multiple pointer types.
Definition SDL3pp_strings.h:313
constexpr TargetBytes(T(&data)[N])
From array.
Definition SDL3pp_strings.h:353
size_t size_bytes
The size in bytes.
Definition SDL3pp_strings.h:307
void * data
The address to have data copied to it.
Definition SDL3pp_strings.h:306
constexpr TargetBytes(void *data, size_t size_bytes)
From data + size in bytes.
Definition SDL3pp_strings.h:365
constexpr TargetBytes(std::span< const T, N > span)
Just to have better error message.
Definition SDL3pp_strings.h:320