openMSX
StringStorage.hh
Go to the documentation of this file.
1#ifndef STRINGSTORAGE_HH
2#define STRINGSTORAGE_HH
3
4#include "ranges.hh"
5#include <cstdlib>
6#include <memory>
7#include <string_view>
8
14{
15 void operator()(char* p) { free(p); }
16};
17using StringStorage = std::unique_ptr<char, FreeStringStorage>;
18
19
23{
24 return StringStorage(static_cast<char*>(malloc(size)));
25}
26
32inline StringStorage allocate_c_string(std::string_view s)
33{
34 auto result = allocate_string_storage(s.size() + 1);
35 char* p = result.get();
36 char* z = ranges::copy(s, p);
37 *z = '\0';
38 return result;
39}
40
41#endif
std::unique_ptr< char, FreeStringStorage > StringStorage
StringStorage allocate_c_string(std::string_view s)
Allocate memory for and copy a c-string (zero-terminated string).
StringStorage allocate_string_storage(size_t size)
Allocate a 'StringStorage' large enough for 'size' characters.
auto copy(InputRange &&range, OutputIter out)
Definition ranges.hh:250
StringStorage: Acts like a 'const char*', but in addition calls free() when the pointer goes out of s...
void operator()(char *p)