Enzo
Loading...
Searching...
No Matches
AttributeHandle.h
1#pragma once
2#include <memory>
3#include <stdexcept>
4#include <string>
5#include <optional>
6#include <vector>
7#include "Engine/Operator/Attribute.h"
8#include "Engine/Types.h"
9#include "tbb/concurrent_vector.h"
10
11#include <iostream>
12
13
14namespace enzo::ga{
15
41template <typename T>
42class AttributeHandle
43{
44public:
45 ga::AttributeType type_;
46
53 AttributeHandle(std::shared_ptr<Attribute> attribute)
54 {
55 if(attribute == nullptr) throw std::runtime_error("Cannot pass empty pointer to AttributeHandle constructor");
56 type_ = attribute->getType();
57 name_ = attribute->getName();
58 // get attribute data pointer
59 // TODO: check types match
60 // TODO: add the other types
61
62 // int
63 if constexpr (std::is_same<bt::intT, T>::value)
64 {
65 data_=attribute->intStore_;
66 }
67
68 // float
69 else if constexpr (std::is_same<bt::floatT, T>::value)
70 {
71 data_=attribute->floatStore_;
72 }
73
74 // vector 3
75 else if constexpr (std::is_same<enzo::bt::Vector3, T>::value)
76 {
77 data_=attribute->vector3Store_;
78 }
79 else if constexpr (std::is_same<enzo::bt::boolT, T>::value)
80 {
81 data_=attribute->boolStore_;
82 }
83 else if constexpr (std::is_same<enzo::bt::Matrix4, T>::value)
84 {
85 data_=attribute->matrix4Store_;
86 }
87 else
88 {
89 throw std::runtime_error("Type " + std::to_string(static_cast<int>(type_)) + " was not properly accounted for in AttributeHandle constructor");
90 }
91 }
92
99 void addValue(T value)
100 {
101 // TODO:make this private (primitive friend classes only)
102 data_->push_back(value);
103 }
104
105
106 // /**
107 // * @brief Reserves more space in the attribute to add new elements
108 // *
109 // * This is important when adding many elements to the attribute as automatic resizing is expensive.
110 // *
111 // * @param newCap The new maximum number of elements the attribute can hold before needing to automatically allocate more.
112 // *
113 // */
114 // void reserve(std::size_t newCap)
115 // {
116 // data_->reserve(newCap);
117 // }
118
136 void resize(std::size_t newSize)
137 {
138 data_->resize(newSize);
139 }
140
141
142 // TODO: replace with iterator
151 std::vector<T> getAllValues() const
152 {
153 return {data_->begin(), data_->end()};
154 }
155
159 size_t getSize() const
160 {
161 return data_->size();
162 }
163
170 T getValue(size_t offset) const
171 {
172 if(offset >= data_->size()) throw std::out_of_range("Cannot get offset: " + std::to_string(offset) + " from size: " + std::to_string(data_->size()) + " for attribute: " + name_);
173 return (*data_)[offset];
174 }
175
181 void setValue(size_t offset, const T& value)
182 {
183 (*data_)[offset] = value;
184 }
185
189 std::string getName() const
190 {
191 return name_;
192 }
193
194
195
196private:
197 // private attributes are attributes that are hidden from the user
198 // for internal use
199 bool private_=false;
200 // hidden attributes are user accessible attributes that the user may
201 // or may want to use
202 bool hidden_=false;
203 // allows the user to read the attributeHandle but not modify it
204 bool readOnly_=false;
205
206 std::string name_;
207
208 std::shared_ptr<StoreContainer<T>> data_;
209
210 // int typeID_;
211
212};
213
214using AttributeHandleInt = AttributeHandle<bt::intT>;
215using AttributeHandleFloat = AttributeHandle<bt::floatT>;
216using AttributeHandleVector3 = AttributeHandle<enzo::bt::Vector3>;
217using AttributeHandleBool = AttributeHandle<enzo::bt::boolT>;
218using AttributeHandleMatrix4 = AttributeHandle<enzo::bt::Matrix4>;
219
220template <typename T>
226{
227public:
228 ga::AttributeType type_;
229
231 AttributeHandleRO(std::shared_ptr<const Attribute> attribute)
232 {
233 type_ = attribute->getType();
234 name_ = attribute->getName();
235 // get attribute data pointer
236 // TODO: check types match
237 // TODO: add the other types
238
239 // int
240 if constexpr (std::is_same<bt::intT, T>::value)
241 {
242 data_=attribute->intStore_;
243 }
244
245 // float
246 else if constexpr (std::is_same<bt::floatT, T>::value)
247 {
248 data_=attribute->floatStore_;
249 }
250
251 // vector 3
252 else if constexpr (std::is_same<enzo::bt::Vector3, T>::value)
253 {
254 data_=attribute->vector3Store_;
255 }
256 else if constexpr (std::is_same<enzo::bt::boolT, T>::value)
257 {
258 data_=attribute->boolStore_;
259 }
260 else if constexpr (std::is_same<enzo::bt::Matrix4, T>::value)
261 {
262 data_=attribute->matrix4Store_;
263 }
264 else
265 {
266 throw std::runtime_error("Type " + std::to_string(static_cast<int>(type_)) + " was not properly accounted for in AttributeHandle constructor");
267 }
268
269 }
270
272 std::vector<T> getAllValues() const
273 {
274 return {data_->begin(), data_->end()};
275 }
276
278 size_t getSize() const
279 {
280 return data_->size();
281 }
282
284 T getValue(size_t offset) const
285 {
286 // TODO:protect against invalid positions
287 // TODO: cast types
288 // TODO: consider removing range check for faster reads
289 if(offset >= data_->size()) throw std::out_of_range("Cannot get offset: " + std::to_string(offset) + " from size: " + std::to_string(data_->size()) + " for attribute: " + name_);
290 return (*data_)[offset];
291 }
292
294 std::string getName() const
295 {
296 return name_;
297 }
298
299
300
301private:
302 // private attributes are attributes that are hidden from the user
303 // for internal use
304 bool private_=false;
305 // hidden attributes are user accessible attributes that the user may
306 // or may want to use
307 bool hidden_=false;
308 // allows the user to read the attributeHandle but not modify it
309 bool readOnly_=false;
310
311 std::string name_;
312
313 std::shared_ptr<StoreContainer<T>> data_;
314
315 // int typeID_;
316
317};
318
319using AttributeHandleInt = AttributeHandle<bt::intT>;
320using AttributeHandleFloat = AttributeHandle<bt::floatT>;
321using AttributeHandleVector3 = AttributeHandle<enzo::bt::Vector3>;
322using AttributeHandleBool = AttributeHandle<enzo::bt::boolT>;
323using AttributeHandleMatrix4 = AttributeHandle<enzo::bt::Matrix4>;
324
325
326}
Basic attribute, parameter, and node types for Enzo.
AttributeType
Data types available to store attribute values in.
Definition Types.h:25
Read only accessor for enzo::ga::Attribute.
Definition AttributeHandle.h:226
AttributeHandleRO(std::shared_ptr< const Attribute > attribute)
Construct a new typed handle linked to a target attribute.
Definition AttributeHandle.h:231
std::string getName() const
Returs the attribute name as a string.
Definition AttributeHandle.h:294
size_t getSize() const
Gets the number of element stored in the attribute.
Definition AttributeHandle.h:278
T getValue(size_t offset) const
Gets the value at a given offset.
Definition AttributeHandle.h:284
std::vector< T > getAllValues() const
Gets a vector containing all the values stored in this attribute.
Definition AttributeHandle.h:272
void setValue(size_t offset, const T &value)
Sets the value at a given offset.
Definition AttributeHandle.h:181
std::string getName() const
Returs the attribute name as a string.
Definition AttributeHandle.h:189
std::vector< T > getAllValues() const
Gets a vector containing all the values stored in this attribute.
Definition AttributeHandle.h:151
void addValue(T value)
Adds an element to the end of the attribute.
Definition AttributeHandle.h:99
size_t getSize() const
Gets the number of element stored in the attribute.
Definition AttributeHandle.h:159
T getValue(size_t offset) const
Gets the value at a given offset.
Definition AttributeHandle.h:170
void resize(std::size_t newSize)
Reserves more space in the attribute to add new elements.
Definition AttributeHandle.h:136
AttributeHandle(std::shared_ptr< Attribute > attribute)
Construct a new typed handle linked to a target attribute.
Definition AttributeHandle.h:53