Bifrost SDK
Bifrost SDK documentation
Object.h
Go to the documentation of this file.
1//-
2//*****************************************************************************
3// Copyright (c) 2024 Autodesk, Inc.
4// All rights reserved.
5//
6// Use of this software is subject to the terms of the Autodesk license
7// agreement provided at the time of installation or download, or which
8// otherwise accompanies this software in either electronic or hard copy form.
9// =============================================================================
10//+
11//
16
17#ifndef BIFROST_OBJECT_H
18#define BIFROST_OBJECT_H
19
20#include "ObjectExport.h"
21#include "ObjectFwd.h"
22
23#include <Amino/Core/Any.h>
24#include <Amino/Core/ArrayFwd.h>
25#include <Amino/Core/Ptr.h>
26#include <Amino/Core/String.h>
28#include <Amino/Cpp/Annotate.h>
29
30//==============================================================================
31// NAMESPACE Bifrost
32//==============================================================================
33
34//------------------------------------------------------------------------------
38#define BIFROST_IGNORE_NAMESPACE AMINO_ANNOTATE("Amino::Namespace ignore")
40#undef BIFROST_IGNORE_NAMESPACE
41
45
59class AMINO_ANNOTATE("Amino::Class metadata=[{type_kind, dictionary}]") OBJECT_DECL Object {
60public:
61 /*----- static member functions -----*/
62
77 static bool isA(Object const& object, Object const& prototype);
78
79 /*----- types -----*/
80
87 struct Property {
88 template <typename T>
89 Property(Amino::String key, T&& value);
90
91 Amino::String m_key;
92 Amino::Any m_value;
93 };
94
95 /*----- member functions -----*/
96
98 Object();
99
101 virtual ~Object();
102
106
109
111 virtual size_t size() const noexcept = 0;
112
115 virtual bool empty() const noexcept = 0;
116
120 virtual bool hasProperty(Amino::StringView key) const noexcept = 0;
121
127 virtual Amino::Any getProperty(Amino::StringView key) const noexcept = 0;
128
132 virtual Amino::Any extractProperty(Amino::StringView key) noexcept = 0;
133
137 virtual bool eraseProperty(Amino::StringView key) noexcept = 0;
138
140 virtual void eraseAllProperties() noexcept = 0;
141
145 virtual Amino::Ptr<Amino::Array<Amino::String>> keys() const noexcept = 0;
146
148
155 template <typename T>
156 bool setProperty(Amino::StringView key, T&& value) noexcept;
157
158 template <typename S, typename T>
159 typename std::enable_if<std::is_same<std::decay_t<S>,Amino::String>::value,bool>::type
160 setProperty(S&& key, T&& value) noexcept;
162
163protected:
171 Object(Object&&) noexcept = default;
172 Object(Object const&) = default;
173 Object& operator=(Object&&) noexcept = default;
174 Object& operator=(Object const&) = default;
176
190 virtual bool setPropertyAny(Amino::StringView key, Amino::Any value) noexcept = 0;
191 virtual bool setPropertyAny(Amino::String const& key, Amino::Any value) noexcept = 0;
193};
194
195//==============================================================================
196// GLOBAL FUNCTIONS
197//==============================================================================
198
202//------------------------------------------------------------------------------
203//
206OBJECT_DECL
207Amino::MutablePtr<Object> createObject();
208
209//------------------------------------------------------------------------------
210//
214OBJECT_DECL
215Amino::MutablePtr<Object> createObject(
216 std::initializer_list<Object::Property> properties);
217
220
221//==============================================================================
222// Implementation details
223//==============================================================================
224
225//------------------------------------------------------------------------------
226//
229namespace Internal {
230template <typename T>
231inline Amino::Any makeAny(T&& v) {
232 return Amino::Any{std::forward<T>(v)};
233}
234inline Amino::Any makeAny(Amino::Any v) { return v; }
235inline Amino::Any makeAny(const char* v) {
236 return Amino::Any{Amino::String(v)};
237}
238inline Amino::Any makeAny(Amino::StringView v) {
239 return Amino::Any{Amino::String(v)};
240}
241template <typename T>
242inline Amino::Any makeAny(Amino::MutablePtr<T> v) {
243 return Amino::Any{Amino::Ptr<T>{std::move(v)}};
244}
245} // namespace Internal
246
247//==============================================================================
248// CLASS Object::Property
249//==============================================================================
250
251template <typename T>
252Object::Property::Property(Amino::String key, T&& value)
253 : m_key(std::move(key)),
254 m_value(Internal::makeAny(std::forward<T>(value))) // LCOV_EXCL_BR_LINE
255{}
256
257//==============================================================================
258// CLASS Object
259//==============================================================================
260
261template <typename T>
262bool Object::setProperty(Amino::StringView key, T&& value) noexcept {
263 // LCOV_EXCL_BR_START
264 if (key.empty()) {
265 return false; // keys not allowed to be empty strings
266 }
267 return setPropertyAny(key, Internal::makeAny(std::forward<T>(value)));
268 // LCOV_EXCL_BR_STOP
269}
270
271template <typename S, typename T>
272typename std::enable_if<std::is_same<std::decay_t<S>, Amino::String>::value, bool>::type
273Object::setProperty(S&& key, T&& value) noexcept {
274 // LCOV_EXCL_BR_START
275 if (key.empty()) {
276 return false; // keys not allowed to be empty strings
277 }
278 return setPropertyAny(key, Internal::makeAny(std::forward<T>(value)));
279 // LCOV_EXCL_BR_STOP
280}
281
283
284} // namespace Bifrost BIFROST_IGNORE_NAMESPACE
285
286#endif // BIFROST_OBJECT_H
A generic value object.
Forward declaration of Amino Array.
Smart pointers used to allow custom user classes (opaque classes) to be used within Amino graphs....
String class.
String view class (similar to std::string_view)
Header Parser Annotation Macro & Parser Documentation.
#define BIFROST_IGNORE_NAMESPACE
Use a define, otherwise clang-format gets confused.
Definition: Object.h:38
Definition of macros for symbol visibility.
Bifrost object interface forward declaration.
Definition: HostData.h:33
Definition: FCurve.h:35