Enzo
Loading...
Searching...
No Matches
AttributeHandle.h
1#pragma once
2#include "Engine/Attribute/Attribute.h"
3#include "Engine/Core/Types.h"
4#include "tbb/concurrent_vector.h"
5#include <memory>
6#include <optional>
7#include <span>
8#include <stdexcept>
9#include <string>
10#include <type_traits>
11#include <vector>
12
13#include <iostream>
14
15namespace enzo::attr {
16
42template <typename T> class AttributeHandle
43{
44 public:
45 attr::AttributeType type_;
46
53 AttributeHandle(std::shared_ptr<Attribute> attribute)
54 {
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_);
60 }
61
68 void addValue(T value)
69 {
70 // TODO:make this private (primitive friend classes only)
71 data_->push_back(value);
72 }
73
74 // /**
75 // * @brief Reserves more space in the attribute to add new elements
76 // *
77 // * This is important when adding many elements to the attribute as automatic resizing is
78 // expensive.
79 // *
80 // * @param newCap The new maximum number of elements the attribute can hold before needing to
81 // automatically allocate more.
82 // *
83 // */
84 // void reserve(std::size_t newCap)
85 // {
86 // data_->reserve(newCap);
87 // }
88
109 void resize(std::size_t newSize) { data_->resize(newSize); }
110
111 // TODO: replace with iterator
120 std::vector<T> getAllValues() const { return {data_->begin(), data_->end()}; }
121
125 size_t getSize() const { return data_->size(); }
126
134 T getValue(size_t offset) const { return (*data_)[offset]; }
135
143 const T& operator[](size_t offset) const
144 requires(!std::is_same_v<T, boolT>)
145 {
146 return (*data_)[offset];
147 }
148
153 std::span<const T> getSpan() const { return {data_->data(), data_->size()}; }
154
161 void setValue(size_t offset, const T& value) { (*data_)[offset] = value; }
162
166 std::string getName() const { return name_; }
167
168 private:
169 std::string name_;
170
171 std::shared_ptr<StoreContainer<T>> data_;
172
173 // int typeID_;
174};
175
176using AttributeHandleInt = AttributeHandle<intT>;
177using AttributeHandleFloat = AttributeHandle<floatT>;
178using AttributeHandleVector3 = AttributeHandle<enzo::Vector3>;
179using AttributeHandleBool = AttributeHandle<enzo::boolT>;
180using AttributeHandleMatrix4 = AttributeHandle<enzo::Matrix4>;
181
182template <typename T>
188{
189 public:
191
193 AttributeHandleRO(std::shared_ptr<const Attribute> attribute)
194 {
195 type_ = attribute->getType();
196 name_ = attribute->getName();
197 data_ = std::get<std::shared_ptr<StoreContainer<T>>>(attribute->store_);
198 }
199
201 std::vector<T> getAllValues() const { return {data_->begin(), data_->end()}; }
202
204 size_t getSize() const { return data_->size(); }
205
207 T getValue(size_t offset) const
208 {
209 // TODO:protect against invalid positions
210 // TODO: cast types
211 // TODO: consider removing range check for faster reads
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_
216 );
217 return (*data_)[offset];
218 }
219
221 const T& operator[](size_t offset) const
222 requires(!std::is_same_v<T, boolT>)
223 {
224 return (*data_)[offset];
225 }
226
228 std::string getName() const { return name_; }
229
230 private:
231 std::string name_;
232
233 std::shared_ptr<StoreContainer<T>> data_;
234
235 // int typeID_;
236};
237
238using AttributeHandleInt = AttributeHandle<intT>;
239using AttributeHandleFloat = AttributeHandle<floatT>;
240using AttributeHandleVector3 = AttributeHandle<enzo::Vector3>;
241using AttributeHandleBool = AttributeHandle<enzo::boolT>;
242using AttributeHandleMatrix4 = AttributeHandle<enzo::Matrix4>;
243
244} // namespace enzo::attr
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