2#include "Engine/Attribute/Attribute.h"
4#include "tbb/concurrent_vector.h"
42template <
typename T>
class AttributeHandle
45 attr::AttributeType type_;
55 if (attribute ==
nullptr)
56 throw std::runtime_error(
"Cannot pass empty pointer to AttributeHandle constructor");
57 type_ = attribute->getType();
58 name_ = attribute->getName();
59 data_ = std::get<std::shared_ptr<StoreContainer<T>>>(attribute->store_);
71 data_->push_back(value);
109 void resize(std::size_t newSize) { data_->resize(newSize); }
120 std::vector<T>
getAllValues()
const {
return {data_->begin(), data_->end()}; }
125 size_t getSize()
const {
return data_->size(); }
134 T
getValue(
size_t offset)
const {
return (*data_)[offset]; }
144 requires(!std::is_same_v<T, boolT>)
146 return (*data_)[offset];
153 std::span<const T>
getSpan()
const {
return {data_->data(), data_->size()}; }
161 void setValue(
size_t offset,
const T& value) { (*data_)[offset] = value; }
171 std::shared_ptr<StoreContainer<T>> data_;
176using AttributeHandleInt = AttributeHandle<intT>;
177using AttributeHandleFloat = AttributeHandle<floatT>;
178using AttributeHandleVector3 = AttributeHandle<enzo::Vector3>;
179using AttributeHandleBool = AttributeHandle<enzo::boolT>;
180using AttributeHandleMatrix4 = AttributeHandle<enzo::Matrix4>;
195 type_ = attribute->getType();
196 name_ = attribute->getName();
197 data_ = std::get<std::shared_ptr<StoreContainer<T>>>(attribute->store_);
201 std::vector<T>
getAllValues()
const {
return {data_->begin(), data_->end()}; }
204 size_t getSize()
const {
return data_->size(); }
212 if (offset >= data_->size())
213 throw std::out_of_range(
214 "Cannot get offset: " + std::to_string(offset) +
215 " from size: " + std::to_string(data_->size()) +
" for attribute: " + name_
217 return (*data_)[offset];
222 requires(!std::is_same_v<T, boolT>)
224 return (*data_)[offset];
233 std::shared_ptr<StoreContainer<T>> data_;
238using AttributeHandleInt = AttributeHandle<intT>;
239using AttributeHandleFloat = AttributeHandle<floatT>;
240using AttributeHandleVector3 = AttributeHandle<enzo::Vector3>;
241using AttributeHandleBool = AttributeHandle<enzo::boolT>;
242using AttributeHandleMatrix4 = AttributeHandle<enzo::Matrix4>;
Basic attribute, parameter, and node types for Enzo.
AttributeType
Data types available to store attribute values in.
Definition Types.h:37
Read only accessor for enzo::attr::Attribute.
Definition AttributeHandle.h:188
AttributeHandleRO(std::shared_ptr< const Attribute > attribute)
Construct a new typed handle linked to a target attribute.
Definition AttributeHandle.h:193
T getValue(size_t offset) const
Gets the value at a given offset.
Definition AttributeHandle.h:207
const T & operator[](size_t offset) const
Zero copy element access. Prefer in hot loops over getValue.
Definition AttributeHandle.h:221
size_t getSize() const
Gets the number of element stored in the attribute.
Definition AttributeHandle.h:204
std::string getName() const
Returs the attribute name as a string.
Definition AttributeHandle.h:228
std::vector< T > getAllValues() const
Gets a vector containing all the values stored in this attribute.
Definition AttributeHandle.h:201
void addValue(T value)
Adds an element to the end of the attribute.
Definition AttributeHandle.h:68
void resize(std::size_t newSize)
Resizes more space in the attribute to add new elements.
Definition AttributeHandle.h:109
std::span< const T > getSpan() const
Contiguous read only view over all stored values.
Definition AttributeHandle.h:153
std::vector< T > getAllValues() const
Gets a vector containing all the values stored in this attribute.
Definition AttributeHandle.h:120
T getValue(size_t offset) const
Gets the value at a given offset.
Definition AttributeHandle.h:134
void setValue(size_t offset, const T &value)
Sets the value at a given offset.
Definition AttributeHandle.h:161
AttributeHandle(std::shared_ptr< Attribute > attribute)
Construct a new typed handle linked to a target attribute.
Definition AttributeHandle.h:53
size_t getSize() const
Gets the number of element stored in the attribute.
Definition AttributeHandle.h:125
std::string getName() const
Returs the attribute name as a string.
Definition AttributeHandle.h:166
const T & operator[](size_t offset) const
Zero copy element access. Prefer in hot loops over getValue.
Definition AttributeHandle.h:143