OpenVDB 9.0.0
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
22namespace openvdb {
24namespace OPENVDB_VERSION_NAME {
25
26namespace ax {
27namespace 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.
33struct 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 String(const char* str) : String(str, std::strlen(str)) {}
40 // initialize from a fixed size char array
41 // @note the last element in the array is NOT the null terminator.
42 // this is automatically added. i.e. char data[3] = {'f','o','o'}
43 // is valid.
44 template<std::size_t S>
45 String(char (&str)[S]) : String(str, S) {}
46 String(const std::string& str) : String(str.c_str(), str.size()) {}
47 String(const String& other) : String(other.ptr, other.len) {}
48 ~String() { if (!this->isLocal()) std::free(ptr); }
49
50 ///////////////////////////////////////////////////////////////////////////
51
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); }
57
58 // operators
59
60 inline operator const char*() const { return this->ptr; }
61
62 const String& operator=(const std::string& str)
63 {
64 this->reset(str.c_str(), str.size());
65 return *this;
66 }
67
68 const String& operator=(const String& other)
69 {
70 this->reset(other.ptr, other.len);
71 return *this;
72 }
73
74 bool operator==(const String& other) const
75 {
76 return std::strcmp(this->ptr, other.ptr) == 0;
77 }
78
79 bool operator!=(const String& other) const
80 {
81 return !this->operator==(other);
82 }
83
84 String operator+(const String& other) const
85 {
86 String s;
87 s.alloc(this->size() + other.size());
88 std::memcpy(s.ptr, this->c_str(), this->size());
89 std::memcpy(s.ptr + this->size(), other.c_str(), other.size());
90 return s;
91 }
92
93 ///////////////////////////////////////////////////////////////////////////
94 ///////////////////////////////////////////////////////////////////////////
95
96 // Internal methods and members. These should never be used directly, but
97 // remain public for C bindings in StringFunction (allowing for behaviour
98 // coupling with the IR implementations).
99
100 String(const char* str, const int64_t size)
101 {
102 assert(str != nullptr);
103 this->ptr = this->SSO; // for the isLocal check in alloc
104 this->reset(str, size);
105 }
106
107 inline void reset(const char* str, const int64_t size)
108 {
109 this->alloc(size);
110 std::memcpy(this->ptr, str, size);
111 }
112
113 inline void alloc(const size_t size)
114 {
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';
119 this->len = size;
120 }
121
122 char* ptr = nullptr;
123 char SSO[SSO_LENGTH];
124 int64_t len = 0;
125};
126
127} // namespace codegen
128} // namespace ax
129
130template<> inline ax::codegen::String zeroVal<ax::codegen::String>() { return ""; }
131
132} // namespace OPENVDB_VERSION_NAME
133} // namespace openvdb
134
135#endif // OPENVDB_AX_CODEGEN_STRING_HAS_BEEN_INCLUDED
136
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:477
Definition: Exceptions.h:13
Definition: Coord.h:586
An extremely basic but native representation of a string class with SSO support. This exists to provi...
Definition: String.h:34
const char * c_str() const
Definition: String.h:53
bool operator==(const String &other) const
Definition: String.h:74
String(const String &other)
Definition: String.h:47
const String & operator=(const String &other)
Definition: String.h:68
void reset(const char *str, const int64_t size)
Definition: String.h:107
bool operator!=(const String &other) const
Definition: String.h:79
String operator+(const String &other) const
Definition: String.h:84
void alloc(const size_t size)
Definition: String.h:113
String()
Definition: String.h:38
char * ptr
Definition: String.h:122
const std::string str() const
Definition: String.h:52
~String()
Definition: String.h:48
int64_t size() const
Definition: String.h:54
String(char(&str)[S])
Definition: String.h:45
const String & operator=(const std::string &str)
Definition: String.h:62
String(const std::string &str)
Definition: String.h:46
void clear()
Definition: String.h:56
bool isLocal() const
Definition: String.h:55
String(const char *str)
Definition: String.h:39
String(const char *str, const int64_t size)
Definition: String.h:100
int64_t len
Definition: String.h:124
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202