12 #ifndef OPENVDB_AX_CODEGEN_STRING_HAS_BEEN_INCLUDED
13 #define OPENVDB_AX_CODEGEN_STRING_HAS_BEEN_INCLUDED
15 #include <openvdb/version.h>
35 static constexpr int64_t SSO_LENGTH = 16;
36 static_assert(SSO_LENGTH >= 2,
"SSO should be greater than or equal to 2");
44 template<std::
size_t S>
46 String(
const std::string& str) :
String(str.c_str(), str.size()) {}
48 ~String() {
if (!this->isLocal()) std::free(ptr); }
52 const std::string
str()
const {
return std::string(this->ptr, this->len); }
53 const char*
c_str()
const {
return this->ptr; }
54 int64_t
size()
const {
return this->len; }
55 bool isLocal()
const {
return this->ptr == this->SSO; }
56 void clear() { this->reset(
"", 0); }
60 inline operator const char*()
const {
return this->ptr; }
64 this->reset(str.c_str(), str.size());
70 this->reset(other.
ptr, other.
len);
76 return std::strcmp(this->ptr, other.
ptr) == 0;
88 std::memcpy(s.
ptr, this->c_str(), this->size());
89 std::memcpy(s.
ptr + this->size(), other.
c_str(), other.
size());
100 String(
const char* str,
const int64_t size)
102 assert(str !=
nullptr);
103 this->ptr = this->SSO;
104 this->reset(str, size);
107 inline void reset(
const char* str,
const int64_t size)
110 std::memcpy(this->ptr, str, size);
113 inline void alloc(
const size_t size)
115 if (!this->isLocal()) std::free(this->ptr);
116 if (size > SSO_LENGTH-1) this->ptr =
static_cast<char*
>(std::malloc(size + 1));
117 else this->ptr = this->SSO;
118 this->ptr[size] =
'\0';
123 char SSO[SSO_LENGTH];
135 #endif // OPENVDB_AX_CODEGEN_STRING_HAS_BEEN_INCLUDED