openMSX
TemporaryString.hh
Go to the documentation of this file.
1 #ifndef TEMPORARYSTRING_HH
2 #define TEMPORARYSTRING_HH
3 
4 #include "StringStorage.hh"
5 #include "zstring_view.hh"
6 #include <string_view>
7 
8 
24 public:
25  static constexpr size_t BUFSIZE = 127;
26 
27  template<typename FillOp>
28  TemporaryString(size_t n_, FillOp op)
29  : n(n_)
30  {
31  if (n <= BUFSIZE) {
32  ptr = buffer;
33  } else {
34  owner = allocate_string_storage(n + 1);
35  ptr = owner.get();
36  }
37  op(ptr);
38  ptr[n] = '\0';
39  }
40  TemporaryString(const TemporaryString&) = delete;
44 
45  [[nodiscard]] char* data() { return ptr; }
46  [[nodiscard]] const char* c_str() const { return ptr; }
47 
48  [[nodiscard]] operator std::string_view() const { return {ptr, n}; }
49  [[nodiscard]] operator zstring_view() const { return {ptr, n}; }
50 
51 private:
52  size_t n;
53  char* ptr;
54  StringStorage owner;
55  char buffer[BUFSIZE + 1];
56 
57 };
58 
59 #endif
std::unique_ptr< char, FreeStringStorage > StringStorage
StringStorage allocate_string_storage(size_t size)
Allocate a 'StringStorage' large enough for 'size' characters.
TemporaryString.
const char * c_str() const
static constexpr size_t BUFSIZE
TemporaryString(TemporaryString &&)=delete
TemporaryString & operator=(TemporaryString &&)=delete
TemporaryString(size_t n_, FillOp op)
TemporaryString(const TemporaryString &)=delete
TemporaryString & operator=(const TemporaryString &)=delete
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
Definition: zstring_view.hh:21