Program Listing for omni/graph/core/ogn/string.h
↰ Return to documentation for omni/graph/core/ogn/string.h
// Copyright (c) 2021-2024, NVIDIA CORPORATION. All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto. Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION is strictly prohibited.
//
#pragma once
// =================================================================================================================
// This file contains simple interface classes which wrap data in the OGN database for easier use.
//
// ogn::const_string Read-only access to a string in fabric
// ogn::string Writable access to a string in fabric, with resizing and reallocation wrapped in
// string-like functions
//
// WARNING: These interfaces are subject to change without warning and are only meant to be used by generated code.
// If you call them directly you may have to modify your code when they change.
// =================================================================================================================
#include <gsl/string_span>
#include <omni/graph/core/iComputeGraph.h>
#include <omni/graph/core/ogn/Types.h>
#include <algorithm>
#include <string>
#include <stdexcept>
namespace omni {
namespace graph {
namespace core {
namespace ogn {
template <typename CharType, typename HandleType>
class base_string : public base_array<CharType, HandleType>
{
public:
size_t length() const
{
return this->size();
}
operator std::string() const
{
auto const& spanObj = this->span();
if (spanObj.data())
{
return std::string{ spanObj.data(), spanObj.size() };
}
return std::string{};
}
template <typename IterableStringType>
bool operator==(const IterableStringType& rhs) const
{
return this->data() ? std::equal(this->begin(), this->end(), rhs.begin(), rhs.end()) : (rhs.begin() == rhs.end());
}
template <typename IterableStringType>
bool operator!=(const IterableStringType& rhs) const
{
return this->data() ? !std::equal(this->begin(), this->end(), rhs.begin(), rhs.end()) :
(rhs.begin() != rhs.end());
}
template <typename IterableStringType>
bool operator<(const IterableStringType& rhs) const
{
return std::lexicographical_compare(this->begin(), this->end(), rhs.begin(), rhs.end());
}
template <typename IterableStringType>
bool operator>(const IterableStringType& rhs) const
{
return std::lexicographical_compare(rhs.begin(), rhs.end(), this->begin(), this->end());
}
template <typename IterableStringType>
bool operator<=(const IterableStringType& rhs) const
{
return operator<(rhs) || operator==(rhs);
}
template <typename IterableStringType>
bool operator>=(const IterableStringType& rhs) const
{
return operator>(rhs) || operator==(rhs);
}
};
class const_string : public base_string<const char, ConstAttributeDataHandle>
{
// from regular attributes
template <typename, eAttributeType, eMemoryType, PtrToPtrKind>
friend struct ArrayAttribute;
// from runtime attributes
template <typename, bool, eMemoryType, PtrToPtrKind>
friend struct ArrayData;
public:
const_string() = default;
};
class string : public base_string<char, AttributeDataHandle>
{
public:
using parent_t = base_string<char, AttributeDataHandle>;
// Make non-templated functions available to pass 1 of template resolution
// http://www.gotw.ca/gotw/087.htm
using base_string::data;
using base_string::size;
using base_string::reset;
string() = default;
void resize(size_t newCount)
{
CUDA_SAFE_ASSERT(isValid());
const IAttributeData& iData = *(this->context()->iAttributeData);
iData.setElementCount(*this->context(), this->handle(), newCount);
this->setDirty();
}
char* data()
{
return const_cast<char*>(this->span().data());
}
char& operator[](size_t index)
{
return const_cast<char&>(this->operator[](index));
}
char& at(size_t index)
{
return const_cast<char&>(this->at(index));
}
string& assign(char const* dataToCopy, size_t charCount)
{
CUDA_SAFE_ASSERT(isValid());
// Resize this string first so that it has the proper space to receive the new data
resize(charCount);
if (charCount == 0)
{
// No work to do when the new string is empty
return *this;
}
std::memcpy(data(), dataToCopy, charCount);
return *this;
}
template <typename StringType>
string& operator=(StringType& rhs)
{
return assign(rhs.data(), rhs.size());
}
string& operator=(const char* rhs)
{
return assign(rhs, rhs ? std::strlen(rhs) : 0);
}
void clear()
{
resize(0);
}
};
} // namespace ogn
} // namespace core
} // namespace graph
} // namespace omni