OpenVDB: openvdb_ax/codegen/String.h Source File

OpenVDB 12.1.0

Loading...
Searching...
No Matches
String.h
Go to the documentation of this file.
1// Copyright Contributors to the OpenVDB Project
2// SPDX-License-Identifier: Apache-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#include <openvdb/util/Assert.h>
18
19#include <cstring>
20#include <cstdlib>
21
22namespace openvdb {
23 OPENVDB_USE_VERSION_NAMESPACE
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.
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 OPENVDB_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
Assert.h
OPENVDB_ASSERT
#define OPENVDB_ASSERT(X)
Definition Assert.h:41
openvdb::v12_1::zeroVal
constexpr T zeroVal()
Return the value of type T that corresponds to zero.
Definition Math.h:70
openvdb
Definition Exceptions.h:13
std
Definition Coord.h:590
Types.h
openvdb::v12_1::ax::codegen::String
An extremely basic but native representation of a string class with SSO support. This exists to provi...
Definition String.h:34
openvdb::v12_1::ax::codegen::String::SSO_LENGTH
static constexpr int64_t SSO_LENGTH
Definition String.h:35
openvdb::v12_1::ax::codegen::String::c_str
const char * c_str() const
Definition String.h:55
openvdb::v12_1::ax::codegen::String::operator==
bool operator==(const String &other) const
Definition String.h:76
openvdb::v12_1::ax::codegen::String::String
String(const String &other)
Definition String.h:50
openvdb::v12_1::ax::codegen::String::operator=
const String & operator=(const String &other)
Definition String.h:70
openvdb::v12_1::ax::codegen::String::reset
void reset(const char *str, const int64_t size)
Definition String.h:102
openvdb::v12_1::ax::codegen::String::operator!=
bool operator!=(const String &other) const
Definition String.h:81
openvdb::v12_1::ax::codegen::String::operator+
String operator+(const String &other) const
Definition String.h:86
openvdb::v12_1::ax::codegen::String::alloc
void alloc(const size_t size)
Definition String.h:108
openvdb::v12_1::ax::codegen::String::SSO
char SSO[SSO_LENGTH]
Definition String.h:118
openvdb::v12_1::ax::codegen::String::String
String()
Definition String.h:38
openvdb::v12_1::ax::codegen::String::ptr
char * ptr
Definition String.h:117
openvdb::v12_1::ax::codegen::String::str
const std::string str() const
Definition String.h:54
openvdb::v12_1::ax::codegen::String::~String
~String()
Definition String.h:48
openvdb::v12_1::ax::codegen::String::size
int64_t size() const
Definition String.h:56
openvdb::v12_1::ax::codegen::String::operator=
const String & operator=(const std::string &str)
Definition String.h:64
openvdb::v12_1::ax::codegen::String::String
String(const std::string &str)
Definition String.h:41
openvdb::v12_1::ax::codegen::String::clear
void clear()
Definition String.h:58
openvdb::v12_1::ax::codegen::String::isLocal
bool isLocal() const
Definition String.h:57
openvdb::v12_1::ax::codegen::String::String
String(const char *str)
Construct from null terminated character string.
Definition String.h:40
openvdb::v12_1::ax::codegen::String::String
String(const char *str, const int64_t size)
Definition String.h:42
openvdb::v12_1::ax::codegen::String::len
int64_t len
Definition String.h:119
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:218