Ocean
Loading...
Searching...
No Matches
DynamicNode.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_SCENEDESCRIPTION_DYNAMIC_NODE_H
9#define META_OCEAN_SCENEDESCRIPTION_DYNAMIC_NODE_H
10
13
14#include <memory>
15#include <vector>
16
17namespace Ocean
18{
19
20namespace SceneDescription
21{
22
23// Forward declaration.
24class DynamicNode;
25
26/**
27 * Definition of a smart object reference for X scene description nodes.
28 * @see DynamicNode, Node.
29 * @ingroup scenedescription
30 */
32
33/**
34 * This class is the base class for all nodes able to handle fields loaded during runtime.
35 * A dynamic field is identified by its name. The index of a dynamic field is not an identity but an enumeration handle
36 * with range [0, dynamicFields()), valid only until the next addField() or removeField() call.
37 * @ingroup scenedescription
38 */
39class OCEAN_SCENEDESCRIPTION_EXPORT DynamicNode : virtual public Node
40{
41 protected:
42
43 /**
44 * Definition of a vector holding fields.
45 */
46 using DynamicFields = std::vector<std::unique_ptr<Field>>;
47
48 /**
49 * Definition of a map mapping field names to field indices.
50 */
51 using DynamicFieldIndices = std::unordered_map<std::string, unsigned int>;
52
53 public:
54
55 /**
56 * Adds a new field during runtime.
57 * The field is appended, so it receives the highest index and the indices of all existing fields stay valid.
58 * @param name The name of the field, must be valid
59 * @param field Field to add, a copy is stored
60 * @return True, if no field has be added with the same name before
61 */
62 bool addField(const std::string& name, const Field& field);
63
64 /**
65 * Returns the number of dynamic fields.
66 * Together with dynamicFieldName() this allows to enumerate all dynamic fields of this node.
67 * @return Number of dynamic fields
68 */
69 inline unsigned int dynamicFields() const;
70
71 /**
72 * Returns whether this node has a special standard or dynamic field.
73 * @see Node::hasAnyField().
74 */
75 bool hasAnyField(const std::string& fieldName) const override;
76
77 /**
78 * Returns whether this node holds a specific dynamic field.
79 * @param name The name of the dynamic field to check
80 * @return True, if so
81 */
82 bool hasDynamicField(const std::string& name) const;
83
84 /**
85 * Returns the name of a dynamic field by a given index.
86 * The index reflects the order in which the fields have been added, it is not stable across a removeField() call.
87 * @param index Index of the dynamic field to return, with range [0, dynamicFields())
88 * @return The name of the dynamic field
89 * @exception OceanException Is thrown if the index does not exist
90 */
91 const std::string& dynamicFieldName(const unsigned int index) const;
92
93 /**
94 * Returns the field base of a specified standard or dynamic field.
95 * @see Node::anyField().
96 */
97 const Field& anyField(const std::string& fieldName) const override;
98
99 /**
100 * Returns the field base of a specified standard or dynamic field.
101 * @see Node::anyField().
102 */
103 Field& anyField(const std::string& fieldName) override;
104
105 /**
106 * Returns the field base of a specified dynamic field.
107 * @param fieldName Name of the dynamic field to return
108 * @return Field base of the specified dynamic field
109 */
110 const Field& dynamicField(const std::string& fieldName) const;
111
112 /**
113 * Returns the field base of a specified dynamic field.
114 * Beware: Changing a field value using this function will not produce any field changing event functions!
115 * @param fieldName Name of the dynamic field to return
116 * @return Field base of the specified dynamic field
117 */
118 Field& dynamicField(const std::string& fieldName);
119
120 /**
121 * Returns a specified dynamic field.
122 * @param fieldName Name of the dynamic field to return
123 */
124 template <typename T> const T& dynamicField(const std::string& fieldName) const;
125
126 /**
127 * Returns a specified dynamic field.
128 * Beware: Changing a field value using this function will not produce any field changing event functions!
129 * @param fieldName Name of the dynamic field to return
130 */
131 template <typename T> T& dynamicField(const std::string& fieldName);
132
133 /**
134 * Removes a field added during runtime.
135 * The remaining fields keep their relative order but are re-indexed, so every index above the removed one is invalidated.
136 * @param name The name of the field to remove, must be valid
137 * @return True, if the field could be remove
138 */
139 bool removeField(const std::string& name);
140
141 /**
142 * Returns whether this node can hold dynamic generated field.
143 * @see Node::isDynamic().
144 */
145 bool isDynamic() const override;
146
147 protected:
148
149 /**
150 * Creates a new dynamic node object.
151 */
153
154 /**
155 * Destructs a dynamic node object.
156 */
157 ~DynamicNode() override;
158
159 protected:
160
161 /// Vector holding the dynamic fields.
163
164 /// Map mapping names of dynamic fields to indices.
166};
167
168inline unsigned int DynamicNode::dynamicFields() const
169{
170 return (unsigned int)(dynamicFields_.size());
171}
172
173template <typename T> const T& DynamicNode::dynamicField(const std::string& fieldName) const
174{
175 const DynamicFieldIndices::const_iterator i = dynamicFieldIndices_.find(fieldName);
176 if (i == dynamicFieldIndices_.end())
177 {
178 throw OceanException("Invalid dynamic field name.");
179 }
180
181 ocean_assert(i->second < dynamicFields_.size());
182 return Field::cast<const T&>(*dynamicFields_[i->second]);
183}
184
185template <typename T> T& DynamicNode::dynamicField(const std::string& fieldName)
186{
187 const DynamicFieldIndices::const_iterator i = dynamicFieldIndices_.find(fieldName);
188 if (i == dynamicFieldIndices_.end())
189 {
190 throw OceanException("Invalid dynamic field name.");
191 }
192
193 ocean_assert(i->second < dynamicFields_.size());
194 return Field::cast<T&>(*dynamicFields_[i->second]);
195}
196
197}
198
199}
200
201#endif // META_OCEAN_SCENEDESCRIPTION_DYNAMIC_NODE_H
Definition of a base exception for the entire Ocean framework.
Definition Exception.h:29
This class is the base class for all nodes able to handle fields loaded during runtime.
Definition DynamicNode.h:40
const std::string & dynamicFieldName(const unsigned int index) const
Returns the name of a dynamic field by a given index.
const Field & dynamicField(const std::string &fieldName) const
Returns the field base of a specified dynamic field.
DynamicFieldIndices dynamicFieldIndices_
Map mapping names of dynamic fields to indices.
Definition DynamicNode.h:165
bool addField(const std::string &name, const Field &field)
Adds a new field during runtime.
bool hasAnyField(const std::string &fieldName) const override
Returns whether this node has a special standard or dynamic field.
unsigned int dynamicFields() const
Returns the number of dynamic fields.
Definition DynamicNode.h:168
const Field & anyField(const std::string &fieldName) const override
Returns the field base of a specified standard or dynamic field.
Field & dynamicField(const std::string &fieldName)
Returns the field base of a specified dynamic field.
DynamicNode()
Creates a new dynamic node object.
std::unordered_map< std::string, unsigned int > DynamicFieldIndices
Definition of a map mapping field names to field indices.
Definition DynamicNode.h:51
bool removeField(const std::string &name)
Removes a field added during runtime.
bool isDynamic() const override
Returns whether this node can hold dynamic generated field.
~DynamicNode() override
Destructs a dynamic node object.
DynamicFields dynamicFields_
Vector holding the dynamic fields.
Definition DynamicNode.h:162
Field & anyField(const std::string &fieldName) override
Returns the field base of a specified standard or dynamic field.
bool hasDynamicField(const std::string &name) const
Returns whether this node holds a specific dynamic field.
std::vector< std::unique_ptr< Field > > DynamicFields
Definition of a vector holding fields.
Definition DynamicNode.h:46
This class is the base class for all scene description fields.
Definition Field.h:36
This class is the base class for all scene description nodes.
Definition scenedescription/Node.h:49
This template class implements a smart object reference which is a specialization of an ObjectRef obj...
Definition SmartObjectRef.h:90
The namespace covering the entire Ocean framework.
Definition Accessor.h:15