openMSX
StringStorage.hh
Go to the documentation of this file.
1 #ifndef STRINGSTORAGE_HH
2 #define STRINGSTORAGE_HH
3 
4 #include <cstdlib>
5 #include <cstring>
6 #include <memory>
7 #include <string_view>
8 
14 {
15  void operator()(char* p) { free(p); }
16 };
17 using StringStorage = std::unique_ptr<char, FreeStringStorage>;
18 
19 
23 {
24  return StringStorage(static_cast<char*>(malloc(size)));
25 }
26 
32 inline StringStorage allocate_c_string(std::string_view s)
33 {
34  auto size = s.size();
35  auto result = allocate_string_storage(size + 1);
36  char* p = result.get();
37  memcpy(p, s.data(), size);
38  p[size] = '\0';
39  return result;
40 }
41 
42 #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.
size_t size(std::string_view utf8)
StringStorage: Acts like a 'const char*', but in addition calls free() when the pointer goes out of s...
void operator()(char *p)