Bifrost SDK
Bifrost SDK documentation
GeoPropertyGuard.h
Go to the documentation of this file.
1//-
2//*****************************************************************************
3// Copyright 2025 Autodesk, Inc.
4// All rights reserved.
5//
6// These coded instructions, statements, and computer programs contain
7// unpublished proprietary information written by Autodesk, Inc. and are
8// protected by Federal copyright law. They may not be disclosed to third
9// parties or copied or duplicated in any form, in whole or in part, without
10// the prior written consent of Autodesk, Inc.
11//*****************************************************************************
12//+
13
15
16#ifndef BIFROST_GEOMETRY_GEO_PROPERTY_GUARD_H
17#define BIFROST_GEOMETRY_GEO_PROPERTY_GUARD_H
18
20
21#include <Bifrost/Geometry/GeoProperty.h> // DataInterpolationMode
22#include <Bifrost/Math/Types.h>
24
25#include <Amino/Core/Array.h>
26#include <Amino/Core/Ptr.h>
28
29namespace Bifrost {
30
31template <typename T>
32class PropertyGuard;
33
38template <typename T>
39bool hasProperty(const Bifrost::Object& object, Amino::StringView propertyName);
40
46template <typename T>
47PropertyGuard<T> createPropGuard(Bifrost::Object& object, Amino::StringView propertyName);
48
54template <typename T>
55PropertyGuard<Amino::Ptr<T>> createPtrPropGuard(Bifrost::Object& object,
56 Amino::StringView propertyName);
57
58} // namespace Bifrost
59
60namespace Bifrost {
61namespace Geometry {
62
63template <typename T>
64class DataGeoPropertyGuard;
65
66class RangeGeoPropertyGuard;
67
73template <typename T>
74bool hasDataGeoProperty(const Bifrost::Object& geometry, Amino::StringView property);
75
84template <typename T>
85DataGeoPropertyGuard<T> createDataGeoPropGuard(Bifrost::Object& geometry,
86 Amino::StringView propertyName);
87
94
104 Amino::StringView propertyName);
105
106} // namespace Geometry
107} // namespace Bifrost
108
110// Implementation:
112
113namespace Bifrost {
114
115template <typename T>
116bool hasProperty(const Bifrost::Object& object, Amino::StringView propertyName) {
117 auto any = object.getProperty(propertyName);
118 return any.type() == Amino::Any{T{}}.type();
119}
120
121template <typename T>
123 return hasProperty<T>(object, propertyName)
124 ? PropertyGuard<T>(object, Amino::String(propertyName))
126}
127
128template <typename T>
130 Amino::StringView propertyName) {
131 return createPropGuard<Amino::Ptr<T>>(object, propertyName);
132}
133
134// -----------------------------------------------------------------------------
146template <typename T>
148public:
150 PropertyGuard() = default;
151
154 if (m_object != nullptr) {
155 m_object->setProperty(m_propertyName, std::move(m_property));
156 }
157 }
158
160 PropertyGuard(const PropertyGuard&) = delete;
162
165 : m_object(std::exchange(io.m_object, nullptr))
166 , m_propertyName(std::move(io.m_propertyName))
167 , m_property(std::move(io.m_property))
168 {
169 }
170
173 {
174 if (this != &io) {
175 m_object = std::exchange(io.m_object, nullptr);
176 m_propertyName = std::move(io.m_propertyName);
177 m_property = std::move(io.m_property);
178 }
179 return *this;
180 }
181
183 explicit operator bool() const noexcept { return m_object != nullptr; }
184
186 const T& operator*() const noexcept {
187 assert(m_object != nullptr);
188 return m_property;
189 }
190
192 T& operator*() noexcept {
193 assert(m_object != nullptr);
194 return m_property;
195 }
196
198 const T* operator->() const noexcept {
199 assert(m_object != nullptr);
200 return &m_property;
201 }
202
204 T* operator->() noexcept {
205 assert(m_object != nullptr);
206 return &m_property;
207 }
208
209private:
211
212 PropertyGuard(Bifrost::Object& object, Amino::String propertyName)
213 : m_object(&object), m_propertyName(std::move(propertyName)) {
214 auto any = m_object->extractProperty(m_propertyName);
215 m_property = Amino::any_cast<T>(std::move(any));
216 }
217
218 Bifrost::Object* m_object = nullptr;
219 Amino::String m_propertyName;
220 T m_property;
221};
222
224template <typename T>
226public:
227 PropertyGuard() = default;
228
230 if (m_object != nullptr) {
231 m_object->setProperty(m_propertyName, std::move(m_property));
232 }
233 }
234
235 PropertyGuard(const PropertyGuard&) = delete;
237
239 : m_object(std::exchange(io.m_object, nullptr))
240 , m_propertyName(std::move(io.m_propertyName))
241 , m_property(std::move(io.m_property))
242 {
243 }
244
246 {
247 if (this != &io) {
248 m_object = std::exchange(io.m_object, nullptr);
249 m_propertyName = std::move(io.m_propertyName);
250 m_property = std::move(io.m_property);
251 }
252 return *this;
253 }
254
255 explicit operator bool() const noexcept { return m_object != nullptr; }
256
257 const T& operator*() const noexcept {
258 assert(m_object != nullptr);
259 return *m_property;
260 }
261
262 T& operator*() noexcept {
263 assert(m_object != nullptr);
264 return *m_property;
265 }
266
267 const T* operator->() const noexcept {
268 assert(m_object != nullptr);
269 return m_property.get();
270 }
271
272 T* operator->() noexcept {
273 assert(m_object != nullptr);
274 return m_property.get();
275 }
276
277private:
278 friend PropertyGuard<Amino::Ptr<T>> createPropGuard<Amino::Ptr<T>>(Bifrost::Object&,
280 friend PropertyGuard<Amino::Ptr<T>> createPtrPropGuard<T>(Bifrost::Object&, Amino::StringView);
281
282 PropertyGuard(Bifrost::Object& object, Amino::String propertyName)
283 : m_object(&object), m_propertyName(std::move(propertyName)) {
284 auto any = m_object->extractProperty(m_propertyName);
285 auto constProp = Amino::any_cast<Amino::Ptr<T>>(std::move(any));
286 m_property = constProp.toMutable();
287 }
288
289 Bifrost::Object* m_object = nullptr;
290 Amino::String m_propertyName;
291 Amino::MutablePtr<T> m_property;
292};
293
294} // namespace Bifrost
295
296namespace Bifrost {
297namespace Geometry {
298
299// -----------------------------------------------------------------------------
300template <typename T>
301bool hasDataGeoProperty(const Bifrost::Object& geometry, Amino::StringView property) {
302 // Check if the property exists and matches the Data geo property schema
303 auto propObj = Bifrost::Geometry::getDataGeoProperty(geometry, property);
304 if (!propObj) return false;
305
306 // Check the data type matches
307 auto dataArray = Bifrost::Geometry::getDataGeoPropValues<T>(geometry, property);
308 return dataArray != nullptr;
309}
310
311// -----------------------------------------------------------------------------
337template <typename T>
339{
340public:
343
345 explicit operator bool() const noexcept { return static_cast<bool>(m_property); }
346
349 const Amino::Array<T>& data() const noexcept { return *m_data; }
350
352 Amino::Array<T>& data() noexcept { return *m_data; }
353
356 const Amino::String& target() const noexcept { return *m_target; }
357
359 Amino::String& target() noexcept { return *m_target; }
360
363 const Amino::Array<Amino::String>& dependsOn() const noexcept { return *m_dependsOn; }
364
366 Amino::Array<Amino::String>& dependsOn() noexcept { return *m_dependsOn; }
367
370 const T& defaultValue() const noexcept { return *m_defaultValue; }
371
373 T& defaultValue() noexcept { return *m_defaultValue; }
374
378 return *m_interp;
379 }
380
383 return *m_interp;
384 }
385
386private:
387 friend DataGeoPropertyGuard<T> createDataGeoPropGuard<T>(Bifrost::Object&, Amino::StringView);
388
390 : m_property(createPtrPropGuard<Bifrost::Object>(object, propertyName)),
391 m_data(createPtrPropGuard<Amino::Array<T>>(*m_property, Bifrost::Geometry::sData)),
392 m_target(createPropGuard<Amino::String>(*m_property, Bifrost::Geometry::sTarget)),
393 m_dependsOn(createPtrPropGuard<Amino::Array<Amino::String>>(
394 *m_property, Bifrost::Geometry::sDependsOn)),
395 m_defaultValue(createPropGuard<T>(*m_property, Bifrost::Geometry::sDefault)),
396 m_interp(createPropGuard<Bifrost::Geometry::Common::DataInterpolationMode>(
397 *m_property, Bifrost::Geometry::sInterp)) {}
398
403 PropertyGuard<T> m_defaultValue;
405};
406
407// -----------------------------------------------------------------------------
408template <typename T>
410 Amino::StringView propertyName) {
411 return hasDataGeoProperty<T>(geometry, propertyName)
412 ? DataGeoPropertyGuard<T>(geometry, propertyName)
414}
415
416// -----------------------------------------------------------------------------
440public:
443
445 explicit operator bool() const noexcept { return static_cast<bool>(m_property); }
446
449 const Amino::Array<Bifrost::Geometry::Index>& indices() const noexcept { return *m_indices; }
450
452 Amino::Array<Bifrost::Geometry::Index>& indices() noexcept { return *m_indices; }
453
456 const Amino::String& target() const noexcept { return *m_target; }
457
459 Amino::String& target() noexcept { return *m_target; }
460
463 const Amino::Array<Amino::String>& dependsOn() const noexcept { return *m_dependsOn; }
464
466 Amino::Array<Amino::String>& dependsOn() noexcept { return *m_dependsOn; }
467
468private:
473
475 : m_property(createPtrPropGuard<Bifrost::Object>(object, propertyName)),
476 m_indices(createPtrPropGuard<Amino::Array<Bifrost::Geometry::Index>>(
477 *m_property, Bifrost::Geometry::sIndices)),
478 m_target(createPropGuard<Amino::String>(*m_property, Bifrost::Geometry::sTarget)),
479 m_dependsOn(createPtrPropGuard<Amino::Array<Amino::String>>(
480 *m_property, Bifrost::Geometry::sDependsOn)) {}
481
486};
487
488} // namespace Geometry
489} // namespace Bifrost
490
491#endif // BIFROST_GEOMETRY_GEO_PROPERTY_GUARD_H
A resizable container of contiguous elements.
Smart pointers used to allow custom user classes (opaque classes) to be used within Amino graphs....
String view class (similar to std::string_view)
Definition of macros for symbol visibility.
#define BIFROST_GEOMETRY_DECL
Utility functions for manipulating Geometry Properties that conform to the geometry schema.
Bifrost object interface declaration.
Amino::Ptr< Bifrost::Object > BIFROST_GEOMETRY_DECL getDataGeoProperty(const Bifrost::Object &object, Amino::StringView property)
Get a geo property from an object, with the specified name.
Definition: HostData.h:33
Ptr(MutablePtr< T >) -> Ptr< T >
Deduction guide for Ptr.
Definition: Ptr.h:2080
Definition: FCurve.h:35
PropertyGuard< Amino::Ptr< T > > createPtrPropGuard(Bifrost::Object &object, Amino::StringView propertyName)
Create a PropertyGuard for an Amino::Ptr<T> property.
bool hasProperty(const Bifrost::Object &object, Amino::StringView propertyName)
Check if an object has a property with type T and name propertyName.
PropertyGuard< T > createPropGuard(Bifrost::Object &object, Amino::StringView propertyName)
Create a PropertyGuard for the given object and property name.
bool hasDataGeoProperty(const Bifrost::Object &geometry, Amino::StringView property)
Check if a geometry object has a valid geo property with type T and name property.
DataGeoPropertyGuard< T > createDataGeoPropGuard(Bifrost::Object &geometry, Amino::StringView propertyName)
Create a DataGeoPropertyGuard for the specified geo property.
BIFROST_GEOMETRY_DECL Amino::String const sIndices
BIFROST_GEOMETRY_DECL Amino::String const sInterp
BIFROST_GEOMETRY_DECL bool hasRangeGeoProperty(const Bifrost::Object &geometry, Amino::StringView property)
Check if a geometry object has a valid Range geo property with name property.
BIFROST_GEOMETRY_DECL Amino::String const sData
BIFROST_GEOMETRY_DECL RangeGeoPropertyGuard createRangeGeoPropGuard(Bifrost::Object &geometry, Amino::StringView propertyName)
Create a RangeGeoPropertyGuard for the specified range geo property.
BIFROST_GEOMETRY_DECL Amino::String const sDependsOn
Amino::uint_t Index
BIFROST_GEOMETRY_DECL Amino::String const sTarget
BIFROST_GEOMETRY_DECL Amino::String const sDefault
Generic value class that allows for storage of a value of any type.
Definition: Any.h:180
TypeId type() const noexcept
Returns the TypeId of the value in this Any, or the TypeId of void if this Any does not have a value.
Definition: Any.h:329
Define a Amino array of elements of type T.
Definition: Array.h:105
Transient version of Amino::Ptr<T> which allows mutable access to the pointee.
Definition: Ptr.h:761
The string class used by Amino.
Definition: String.h:46
String view class (similar to std::string_view).
Definition: StringView.h:42
This class is a RAII guard for a Bifrost::Object property.
PropertyGuard()=default
Creates an invalid guard.
PropertyGuard(PropertyGuard &&io) noexcept
Moving a guard is allowed.
T & operator*() noexcept
Returns the value of the property. noexcept
PropertyGuard(const PropertyGuard &)=delete
Copy constructor is deleted.
T * operator->() noexcept
Returns a pointer to the property. noexcept
~PropertyGuard()
Destroyes the guard and sets the property value back into the Object.
PropertyGuard & operator=(PropertyGuard &&io) noexcept
Moving a guard is allowed.
const T & operator*() const noexcept
Returns the value of the property.
PropertyGuard & operator=(const PropertyGuard &)=delete
const T * operator->() const noexcept
Returns a pointer to the property.
This class is a RAII guard for a Data geo property.
const Amino::Array< Amino::String > & dependsOn() const noexcept
Returns the value of the depends_on field in the geo property.
const Amino::Array< T > & data() const noexcept
Returns the data array of the geo property.
Amino::Array< T > & data() noexcept
Returns the data array of the geo property.
Amino::String & target() noexcept
Returns the target of the geo property.
Amino::Array< Amino::String > & dependsOn() noexcept
Returns the value of the depends_on field in the geo property.
const Bifrost::Geometry::Common::DataInterpolationMode & interpolationMode() const noexcept
Returns the interpolation_mode field in the geo property.
T & defaultValue() noexcept
Returns the default value of the geo property.
Bifrost::Geometry::Common::DataInterpolationMode & interpolationMode() noexcept
Returns the interpolation_mode field in the geo property.
const T & defaultValue() const noexcept
Returns the default value of the geo property.
const Amino::String & target() const noexcept
Returns the target of the geo property.
DataGeoPropertyGuard()=default
Constructor.
PropertyGuard & operator=(PropertyGuard &&io) noexcept
PropertyGuard(PropertyGuard &&io) noexcept
PropertyGuard(const PropertyGuard &)=delete
PropertyGuard & operator=(const PropertyGuard &)=delete
This class is a RAII guard for a Range geo property.
const Amino::String & target() const noexcept
Returns the target of the geo property.
Amino::String & target() noexcept
Returns the target of the geo property.
const Amino::Array< Amino::String > & dependsOn() const noexcept
Returns the value of the depends_on field in the geo property.
Amino::Array< Bifrost::Geometry::Index > & indices() noexcept
Returns the index array of the geo property.
Amino::Array< Amino::String > & dependsOn() noexcept
Returns the value of the depends_on field in the geo property.
const Amino::Array< Bifrost::Geometry::Index > & indices() const noexcept
Returns the index array of the geo property.
RangeGeoPropertyGuard()=default
Constructor.
An interface for dictionary-like objects.
Definition: Object.h:59
bool setProperty(Amino::StringView key, T &&value) noexcept
Set a property. Replace or add depending on if the property already exists or not in the object.
virtual Amino::Any extractProperty(Amino::StringView key) noexcept=0
Extract a property. The property is removed and returned.