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 type_ = attribute->getType();
56 // get attribute data pointer
57 // TODO: check types match
58 // TODO: add the other types
59
60 // int
61 if constexpr (std::is_same<bt::intT, T>::value)
62 {
63 data_=attribute->intStore_;
64 }
65
66 // float
67 else if constexpr (std::is_same<bt::floatT, T>::value)
68 {
69 data_=attribute->floatStore_;
70 }
71
72 // vector 3
73 else if constexpr (std::is_same<enzo::bt::Vector3, T>::value)
74 {
75 data_=attribute->vector3Store_;
76 }
77 else if constexpr (std::is_same<enzo::bt::boolT, T>::value)
78 {
79 data_=attribute->boolStore_;
80 }
81 else
82 {
83 throw std::runtime_error("Type " + std::to_string(static_cast<int>(type_)) + " was not properly accounted for in AttributeHandle constructor");
84 }
85 }
86
93 void addValue(T value)
94 {
95 // TODO:make this private (primitive friend classes only)
96 data_->push_back(value);
97 }
98
99
108 void reserve(std::size_t newCap)
109 {
110 data_->reserve(newCap);
111 }
112
113 // TODO: replace with iterator
122 std::vector<T> getAllValues() const
123 {
124 return {data_->begin(), data_->end()};
125 }
126
130 size_t getSize() const
131 {
132 return data_->size();
133 }
134
141 T getValue(size_t offset) const
142 {
143 return (*data_)[offset];
144 }
145
151 void setValue(size_t offset, const T& value)
152 {
153 (*data_)[offset] = value;
154 }
155
159 std::string getName() const
160 {
161 return name_;
162 }
163
164
165
166private:
167 // private attributes are attributes that are hidden from the user
168 // for internal use
169 bool private_=false;
170 // hidden attributes are user accessible attributes that the user may
171 // or may want to use
172 bool hidden_=false;
173 // allows the user to read the attributeHandle but not modify it
174 bool readOnly_=false;
175
176 std::string name_="";
177
178 std::shared_ptr<StoreContainer<T>> data_;
179
180 // int typeID_;
181
182};
183
184using AttributeHandleInt = AttributeHandle<bt::intT>;
185using AttributeHandleFloat = AttributeHandle<bt::floatT>;
186using AttributeHandleVector3 = AttributeHandle<enzo::bt::Vector3>;
187using AttributeHandleBool = AttributeHandle<enzo::bt::boolT>;
188
189template <typename T>
195{
196public:
197 ga::AttributeType type_;
198
200 AttributeHandleRO(std::shared_ptr<const Attribute> attribute)
201 {
202 type_ = attribute->getType();
203 // get attribute data pointer
204 // TODO: check types match
205 // TODO: add the other types
206
207 // int
208 if constexpr (std::is_same<bt::intT, T>::value)
209 {
210 data_=attribute->intStore_;
211 }
212
213 // float
214 else if constexpr (std::is_same<bt::floatT, T>::value)
215 {
216 data_=attribute->floatStore_;
217 }
218
219 // vector 3
220 else if constexpr (std::is_same<enzo::bt::Vector3, T>::value)
221 {
222 data_=attribute->vector3Store_;
223 }
224 else if constexpr (std::is_same<enzo::bt::boolT, T>::value)
225 {
226 data_=attribute->boolStore_;
227 }
228 else
229 {
230 throw std::runtime_error("Type " + std::to_string(static_cast<int>(type_)) + " was not properly accounted for in AttributeHandle constructor");
231 }
232
233 }
234
236 std::vector<T> getAllValues() const
237 {
238 return {data_->begin(), data_->end()};
239 }
240
242 size_t getSize() const
243 {
244 return data_->size();
245 }
246
248 T getValue(size_t pos) const
249 {
250 // TODO:protect against invalid positions
251 // TODO: cast types
252 return (*data_)[pos];
253 }
254
256 std::string getName() const
257 {
258 return name_;
259 }
260
261
262
263private:
264 // private attributes are attributes that are hidden from the user
265 // for internal use
266 bool private_=false;
267 // hidden attributes are user accessible attributes that the user may
268 // or may want to use
269 bool hidden_=false;
270 // allows the user to read the attributeHandle but not modify it
271 bool readOnly_=false;
272
273 std::string name_="";
274
275 std::shared_ptr<StoreContainer<T>> data_;
276
277 // int typeID_;
278
279};
280
281using AttributeHandleInt = AttributeHandle<bt::intT>;
282using AttributeHandleFloat = AttributeHandle<bt::floatT>;
283using AttributeHandleVector3 = AttributeHandle<enzo::bt::Vector3>;
284using AttributeHandleBool = AttributeHandle<enzo::bt::boolT>;
285
286
287}
Basic attribute, parameter, and node types for Enzo.
AttributeType
Data types available to store attribute values in.
Definition Types.h:33
Read only accessor for enzo::ga::Attribute.
Definition AttributeHandle.h:195
AttributeHandleRO(std::shared_ptr< const Attribute > attribute)
Construct a new typed handle linked to a target attribute.
Definition AttributeHandle.h:200
std::string getName() const
Returs the attribute name as a string.
Definition AttributeHandle.h:256
size_t getSize() const
Gets the number of element stored in the attribute.
Definition AttributeHandle.h:242
T getValue(size_t pos) const
Gets the value at a given offset.
Definition AttributeHandle.h:248
std::vector< T > getAllValues() const
Gets a vector containing all the values stored in this attribute.
Definition AttributeHandle.h:236
void setValue(size_t offset, const T &value)
Sets the value at a given offset.
Definition AttributeHandle.h:151
std::string getName() const
Returs the attribute name as a string.
Definition AttributeHandle.h:159
std::vector< T > getAllValues() const
Gets a vector containing all the values stored in this attribute.
Definition AttributeHandle.h:122
void addValue(T value)
Adds an element to the end of the attribute.
Definition AttributeHandle.h:93
size_t getSize() const
Gets the number of element stored in the attribute.
Definition AttributeHandle.h:130
T getValue(size_t offset) const
Gets the value at a given offset.
Definition AttributeHandle.h:141
AttributeHandle(std::shared_ptr< Attribute > attribute)
Construct a new typed handle linked to a target attribute.
Definition AttributeHandle.h:53
void reserve(std::size_t newCap)
Reserves more space in the attribute to add new elements.
Definition AttributeHandle.h:108