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 // private attributes are attributes that are hidden from the user
170 // for internal use
171 bool private_ = false;
172 // hidden attributes are user accessible attributes that the user may
173 // or may want to use
174 bool hidden_ = false;
175 // allows the user to read the attributeHandle but not modify it
176 bool readOnly_ = false;
177
178 std::string name_;
179
180 std::shared_ptr<StoreContainer<T>> data_;
181
182 // int typeID_;
183};
184
185using AttributeHandleInt = AttributeHandle<intT>;
186using AttributeHandleFloat = AttributeHandle<floatT>;
187using AttributeHandleVector3 = AttributeHandle<enzo::Vector3>;
188using AttributeHandleBool = AttributeHandle<enzo::boolT>;
189using AttributeHandleMatrix4 = AttributeHandle<enzo::Matrix4>;
190
191template <typename T>
197{
198 public:
200
202 AttributeHandleRO(std::shared_ptr<const Attribute> attribute)
203 {
204 type_ = attribute->getType();
205 name_ = attribute->getName();
206 data_ = std::get<std::shared_ptr<StoreContainer<T>>>(attribute->store_);
207 }
208
210 std::vector<T> getAllValues() const { return {data_->begin(), data_->end()}; }
211
213 size_t getSize() const { return data_->size(); }
214
216 T getValue(size_t offset) const
217 {
218 // TODO:protect against invalid positions
219 // TODO: cast types
220 // TODO: consider removing range check for faster reads
221 if (offset >= data_->size())
222 throw std::out_of_range(
223 "Cannot get offset: " + std::to_string(offset) +
224 " from size: " + std::to_string(data_->size()) + " for attribute: " + name_
225 );
226 return (*data_)[offset];
227 }
228
230 const T& operator[](size_t offset) const
231 requires(!std::is_same_v<T, boolT>)
232 {
233 return (*data_)[offset];
234 }
235
237 std::string getName() const { return name_; }
238
239 private:
240 // private attributes are attributes that are hidden from the user
241 // for internal use
242 bool private_ = false;
243 // hidden attributes are user accessible attributes that the user may
244 // or may want to use
245 bool hidden_ = false;
246 // allows the user to read the attributeHandle but not modify it
247 bool readOnly_ = false;
248
249 std::string name_;
250
251 std::shared_ptr<StoreContainer<T>> data_;
252
253 // int typeID_;
254};
255
256using AttributeHandleInt = AttributeHandle<intT>;
257using AttributeHandleFloat = AttributeHandle<floatT>;
258using AttributeHandleVector3 = AttributeHandle<enzo::Vector3>;
259using AttributeHandleBool = AttributeHandle<enzo::boolT>;
260using AttributeHandleMatrix4 = AttributeHandle<enzo::Matrix4>;
261
262} // namespace enzo::attr
Basic attribute, parameter, and node types for Enzo.
AttributeType
Data types available to store attribute values in.
Definition Types.h:32
Read only accessor for enzo::attr::Attribute.
Definition AttributeHandle.h:197
AttributeHandleRO(std::shared_ptr< const Attribute > attribute)
Construct a new typed handle linked to a target attribute.
Definition AttributeHandle.h:202
T getValue(size_t offset) const
Gets the value at a given offset.
Definition AttributeHandle.h:216
const T & operator[](size_t offset) const
Zero copy element access. Prefer in hot loops over getValue.
Definition AttributeHandle.h:230
size_t getSize() const
Gets the number of element stored in the attribute.
Definition AttributeHandle.h:213
std::string getName() const
Returs the attribute name as a string.
Definition AttributeHandle.h:237
std::vector< T > getAllValues() const
Gets a vector containing all the values stored in this attribute.
Definition AttributeHandle.h:210
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