Loading...
Searching...
No Matches
String.h
Go to the documentation of this file.
// Copyright Contributors to the OpenVDB Project
// SPDX-License-Identifier: MPL-2.0
/// @file codegen/String.h
///
/// @authors Nick Avramoussis
///
/// @brief Provides the class definition for the equivalent IR representation
/// and logic for strings in AX.
///
#ifndef OPENVDB_AX_CODEGEN_STRING_HAS_BEEN_INCLUDED
#define OPENVDB_AX_CODEGEN_STRING_HAS_BEEN_INCLUDED
#include <openvdb/version.h>
#include <openvdb/Types.h>
#include <cstring>
#include <cstdlib>
#include <cassert>
namespace openvdb {
OPENVDB_USE_VERSION_NAMESPACE
namespace OPENVDB_VERSION_NAME {
namespace ax {
namespace codegen {
/// @brief An extremely basic but native representation of a string class with
/// SSO support. This exists to provide an interface between the AX C++ API
/// and backend IR string logic. It is not designed to fulfill any other use
/// and should very rarely be used directly.
struct String
{
static constexpr int64_t SSO_LENGTH = 16; // Should never be less than 2
static_assert(SSO_LENGTH >= 2, "SSO should be greater than or equal to 2");
String() : String("", 0) {}
/// Construct from null terminated character string
String(const char* str) : String(str, std::strlen(str)) {}
String(const std::string& str) : String(str.c_str(), str.size()) {}
String(const char* str, const int64_t size)
{
assert(str != nullptr);
this->ptr = this->SSO; // for the isLocal check in alloc
this->reset(str, size);
}
~String() { if (!this->isLocal()) std::free(ptr); }
String(const String& other) : String(other.ptr, other.len) {}
///////////////////////////////////////////////////////////////////////////
const std::string str() const { return std::string(this->ptr, this->len); }
const char* c_str() const { return this->ptr; }
int64_t size() const { return this->len; }
bool isLocal() const { return this->ptr == this->SSO; }
void clear() { this->reset("", 0); }
// operators
inline operator const char*() const { return this->ptr; }
const String& operator=(const std::string& str)
{
this->reset(str.c_str(), str.size());
return *this;
}
const String& operator=(const String& other)
{
this->reset(other.ptr, other.len);
return *this;
}
bool operator==(const String& other) const
{
return std::strcmp(this->ptr, other.ptr) == 0;
}
bool operator!=(const String& other) const
{
return !this->operator==(other);
}
String operator+(const String& other) const
{
String s;
s.alloc(this->size() + other.size());
std::memcpy(s.ptr, this->c_str(), this->size());
std::memcpy(s.ptr + this->size(), other.c_str(), other.size());
return s;
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// Internal methods and members. These should never be used directly, but
// remain public for C bindings in StringFunction (allowing for behaviour
// coupling with the IR implementations).
inline void reset(const char* str, const int64_t size)
{
this->alloc(size);
std::memcpy(this->ptr, str, size);
}
inline void alloc(const size_t size)
{
if (!this->isLocal()) std::free(this->ptr);
if (size > SSO_LENGTH-1) this->ptr = static_cast<char*>(std::malloc(size + 1));
else this->ptr = this->SSO;
this->ptr[size] = '\0';
this->len = size;
}
char* ptr = nullptr;
char SSO[SSO_LENGTH];
int64_t len = 0;
};
} // namespace codegen
} // namespace ax
template<> inline ax::codegen::String zeroVal<ax::codegen::String>() { return ""; }
} // namespace OPENVDB_VERSION_NAME
} // namespace openvdb
#endif // OPENVDB_AX_CODEGEN_STRING_HAS_BEEN_INCLUDED
openvdb::v10_0::zeroVal
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:587
Types.h
openvdb::v10_0::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::v10_0::ax::codegen::String::SSO_LENGTH
static constexpr int64_t SSO_LENGTH
Definition String.h:35
openvdb::v10_0::ax::codegen::String::c_str
const char * c_str() const
Definition String.h:55
openvdb::v10_0::ax::codegen::String::operator==
bool operator==(const String &other) const
Definition String.h:76
openvdb::v10_0::ax::codegen::String::String
String(const String &other)
Definition String.h:50
openvdb::v10_0::ax::codegen::String::operator=
const String & operator=(const String &other)
Definition String.h:70
openvdb::v10_0::ax::codegen::String::reset
void reset(const char *str, const int64_t size)
Definition String.h:102
openvdb::v10_0::ax::codegen::String::operator!=
bool operator!=(const String &other) const
Definition String.h:81
openvdb::v10_0::ax::codegen::String::operator+
String operator+(const String &other) const
Definition String.h:86
openvdb::v10_0::ax::codegen::String::alloc
void alloc(const size_t size)
Definition String.h:108
openvdb::v10_0::ax::codegen::String::SSO
char SSO[SSO_LENGTH]
Definition String.h:118
openvdb::v10_0::ax::codegen::String::String
String()
Definition String.h:38
openvdb::v10_0::ax::codegen::String::ptr
char * ptr
Definition String.h:117
openvdb::v10_0::ax::codegen::String::str
const std::string str() const
Definition String.h:54
openvdb::v10_0::ax::codegen::String::~String
~String()
Definition String.h:48
openvdb::v10_0::ax::codegen::String::size
int64_t size() const
Definition String.h:56
openvdb::v10_0::ax::codegen::String::operator=
const String & operator=(const std::string &str)
Definition String.h:64
openvdb::v10_0::ax::codegen::String::String
String(const std::string &str)
Definition String.h:41
openvdb::v10_0::ax::codegen::String::clear
void clear()
Definition String.h:58
openvdb::v10_0::ax::codegen::String::isLocal
bool isLocal() const
Definition String.h:57
openvdb::v10_0::ax::codegen::String::String
String(const char *str)
Construct from null terminated character string.
Definition String.h:40
openvdb::v10_0::ax::codegen::String::String
String(const char *str, const int64_t size)
Definition String.h:42
openvdb::v10_0::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:212