OpenVDB  9.1.1
String.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
4 /// @file codegen/String.h
5 ///
6 /// @authors Nick Avramoussis
7 ///
8 /// @brief Provides the class definition for the equivalent IR representation
9 /// and logic for strings in AX.
10 ///
11 
12 #ifndef OPENVDB_AX_CODEGEN_STRING_HAS_BEEN_INCLUDED
13 #define OPENVDB_AX_CODEGEN_STRING_HAS_BEEN_INCLUDED
14 
15 #include <openvdb/version.h>
16 #include <openvdb/Types.h>
17 
18 #include <cstring>
19 #include <cstdlib>
20 #include <cassert>
21 
22 namespace openvdb {
24 namespace OPENVDB_VERSION_NAME {
25 
26 namespace ax {
27 namespace codegen {
28 
29 /// @brief An extremely basic but native representation of a string class with
30 /// SSO support. This exists to provide an interface between the AX C++ API
31 /// and backend IR string logic. It is not designed to fulfill any other use
32 /// and should very rarely be used directly.
33 struct String
34 {
35  static constexpr int64_t SSO_LENGTH = 16; // Should never be less than 2
36  static_assert(SSO_LENGTH >= 2, "SSO should be greater than or equal to 2");
37 
38  String() : String("", 0) {}
39  /// Construct from null terminated character string
40  String(const char* str) : String(str, std::strlen(str)) {}
41  String(const std::string& str) : String(str.c_str(), str.size()) {}
42  String(const char* str, const int64_t size)
43  {
44  assert(str != nullptr);
45  this->ptr = this->SSO; // for the isLocal check in alloc
46  this->reset(str, size);
47  }
48  ~String() { if (!this->isLocal()) std::free(ptr); }
49 
50  String(const String& other) : String(other.ptr, other.len) {}
51 
52  ///////////////////////////////////////////////////////////////////////////
53 
54  const std::string str() const { return std::string(this->ptr, this->len); }
55  const char* c_str() const { return this->ptr; }
56  int64_t size() const { return this->len; }
57  bool isLocal() const { return this->ptr == this->SSO; }
58  void clear() { this->reset("", 0); }
59 
60  // operators
61 
62  inline operator const char*() const { return this->ptr; }
63 
64  const String& operator=(const std::string& str)
65  {
66  this->reset(str.c_str(), str.size());
67  return *this;
68  }
69 
70  const String& operator=(const String& other)
71  {
72  this->reset(other.ptr, other.len);
73  return *this;
74  }
75 
76  bool operator==(const String& other) const
77  {
78  return std::strcmp(this->ptr, other.ptr) == 0;
79  }
80 
81  bool operator!=(const String& other) const
82  {
83  return !this->operator==(other);
84  }
85 
86  String operator+(const String& other) const
87  {
88  String s;
89  s.alloc(this->size() + other.size());
90  std::memcpy(s.ptr, this->c_str(), this->size());
91  std::memcpy(s.ptr + this->size(), other.c_str(), other.size());
92  return s;
93  }
94 
95  ///////////////////////////////////////////////////////////////////////////
96  ///////////////////////////////////////////////////////////////////////////
97 
98  // Internal methods and members. These should never be used directly, but
99  // remain public for C bindings in StringFunction (allowing for behaviour
100  // coupling with the IR implementations).
101 
102  inline void reset(const char* str, const int64_t size)
103  {
104  this->alloc(size);
105  std::memcpy(this->ptr, str, size);
106  }
107 
108  inline void alloc(const size_t size)
109  {
110  if (!this->isLocal()) std::free(this->ptr);
111  if (size > SSO_LENGTH-1) this->ptr = static_cast<char*>(std::malloc(size + 1));
112  else this->ptr = this->SSO;
113  this->ptr[size] = '\0';
114  this->len = size;
115  }
116 
117  char* ptr = nullptr;
118  char SSO[SSO_LENGTH];
119  int64_t len = 0;
120 };
121 
122 } // namespace codegen
123 } // namespace ax
124 
125 template<> inline ax::codegen::String zeroVal<ax::codegen::String>() { return ""; }
126 
127 } // namespace OPENVDB_VERSION_NAME
128 } // namespace openvdb
129 
130 #endif // OPENVDB_AX_CODEGEN_STRING_HAS_BEEN_INCLUDED
131 
String operator+(const String &other) const
Definition: String.h:86
const char * c_str() const
Definition: String.h:55
String(const std::string &str)
Definition: String.h:41
Definition: Coord.h:587
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:477
String(const String &other)
Definition: String.h:50
const String & operator=(const std::string &str)
Definition: String.h:64
String()
Definition: String.h:38
int64_t len
Definition: String.h:119
void reset(const char *str, const int64_t size)
Definition: String.h:102
String(const char *str)
Construct from null terminated character string.
Definition: String.h:40
Definition: Exceptions.h:13
String(const char *str, const int64_t size)
Definition: String.h:42
char * ptr
Definition: String.h:117
const String & operator=(const String &other)
Definition: String.h:70
void alloc(const size_t size)
Definition: String.h:108
bool isLocal() const
Definition: String.h:57
bool operator!=(const String &other) const
Definition: String.h:81
void clear()
Definition: String.h:58
int64_t size() const
Definition: String.h:56
~String()
Definition: String.h:48
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
bool operator==(const String &other) const
Definition: String.h:76
const std::string str() const
Definition: String.h:54
An extremely basic but native representation of a string class with SSO support. This exists to provi...
Definition: String.h:33
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202