Ocean
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 <vector>
15 
16 namespace Ocean
17 {
18 
19 namespace SceneDescription
20 {
21 
22 // Forward declaration.
23 class DynamicNode;
24 
25 /**
26  * Definition of a smart object reference for X scene description nodes.
27  * @see DynamicNode, Node.
28  * @ingroup scenedescription
29  */
31 
32 /**
33  * This class is the base class for all nodes able to handle fields loaded during runtime.
34  * @ingroup scenedescription
35  */
36 class OCEAN_SCENEDESCRIPTION_EXPORT DynamicNode : virtual public Node
37 {
38  protected:
39 
40  /**
41  * Definition of a vector holding fields.
42  */
43  typedef std::vector<Field*> DynamicFields;
44 
45  /**
46  * Definition of a map mapping field names to field indices.
47  */
48  typedef std::unordered_map<std::string, unsigned int> DynamicFieldIndices;
49 
50  public:
51 
52  /**
53  * Adds a new field during runtime.
54  * @param name The name of the node
55  * @param field Field to add
56  * @return True, if no field has be added with the same name before
57  */
58  bool addField(const std::string& name, const Field& field);
59 
60  /**
61  * Returns the number of dynamic fields.
62  * @return Number of dynamic fields
63  */
64  inline unsigned int dynamicFields() const;
65 
66  /**
67  * Returns whether this node has a special standard or dynamic field.
68  * @see Node::hasAnyField().
69  */
70  bool hasAnyField(const std::string& fieldName) const override;
71 
72  /**
73  * Returns whether this node holds a specific dynamic field.
74  * @param name The name of the dynamic field to check
75  * @return True, if so
76  */
77  bool hasDynamicField(const std::string& name) const;
78 
79  /**
80  * Returns the name of a dynamic field by a given index.
81  * @param index Index of the dynamic field to return
82  */
83  const std::string& dynamicFieldName(const unsigned int index) const;
84 
85  /**
86  * Returns the field base of a specified standard or dynamic field.
87  * @see Node::anyField().
88  */
89  const Field& anyField(const std::string& fieldName) const override;
90 
91  /**
92  * Returns the field base of a specified standard or dynamic field.
93  * @see Node::anyField().
94  */
95  Field& anyField(const std::string& fieldName) override;
96 
97  /**
98  * Returns the field base of a specified dynamic field.
99  * @param fieldName Name of the dynamic field to return
100  * @return Field base of the specified dynamic field
101  */
102  const Field& dynamicField(const std::string& fieldName) const;
103 
104  /**
105  * Returns the field base of a specified dynamic field.
106  * Beware: Changing a field value using this function will not produce any field changing event functions!
107  * @param fieldName Name of the dynamic field to return
108  * @return Field base of the specified dynamic field
109  */
110  Field& dynamicField(const std::string& fieldName);
111 
112  /**
113  * Returns a specified dynamic field.
114  * @param fieldName Name of the dynamic field to return
115  */
116  template <typename T> const T& dynamicField(const std::string& fieldName) const;
117 
118  /**
119  * Returns a specified dynamic field.
120  * Beware: Changing a field value using this function will not produce any field changing event functions!
121  * @param fieldName Name of the dynamic field to return
122  */
123  template <typename T> T& dynamicField(const std::string& fieldName);
124 
125  /**
126  * Removes a field added during runtime.
127  * @param name The name of the field to remove
128  * @return True, if the field could be remove
129  */
130  bool removeField(const std::string& name);
131 
132  /**
133  * Returns whether this node can hold dynamic generated field.
134  * @see Node::isDynamic().
135  */
136  bool isDynamic() const override;
137 
138  protected:
139 
140  /**
141  * Creates a new dynamic node object.
142  */
144 
145  /**
146  * Destructs a dynamic node object.
147  */
148  ~DynamicNode() override;
149 
150  protected:
151 
152  /// Vector holding the dynamic fields.
154 
155  /// Map mapping names of dynamic fields to indices.
157 };
158 
159 inline unsigned int DynamicNode::dynamicFields() const
160 {
161  return (unsigned int)(dynamicFields_.size());
162 }
163 
164 template <typename T> const T& DynamicNode::dynamicField(const std::string& fieldName) const
165 {
166  const DynamicFieldIndices::const_iterator i = dynamicFieldIndices_.find(fieldName);
167  if (i == dynamicFieldIndices_.end())
168  {
169  throw OceanException("Invalid dynamic field name.");
170  }
171 
172  ocean_assert(i->second < dynamicFields_.size());
173  return Field::cast<const T&>(*dynamicFields_[i->second]);
174 }
175 
176 template <typename T> T& DynamicNode::dynamicField(const std::string& fieldName)
177 {
178  const DynamicFieldIndices::const_iterator i = dynamicFieldIndices_.find(fieldName);
179  if (i == dynamicFieldIndices_.end())
180  {
181  throw OceanException("Invalid dynamic field name.");
182  }
183 
184  ocean_assert(i->second < dynamicFields_.size());
185  return Field::cast<T&>(*dynamicFields_[i->second]);
186 }
187 
188 }
189 
190 }
191 
192 #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:37
DynamicFieldIndices dynamicFieldIndices_
Map mapping names of dynamic fields to indices.
Definition: DynamicNode.h:156
bool addField(const std::string &name, const Field &field)
Adds a new field during runtime.
std::unordered_map< std::string, unsigned int > DynamicFieldIndices
Definition of a map mapping field names to field indices.
Definition: DynamicNode.h:48
bool hasAnyField(const std::string &fieldName) const override
Returns whether this node has a special standard or dynamic field.
const std::string & dynamicFieldName(const unsigned int index) const
Returns the name of a dynamic field by a given index.
unsigned int dynamicFields() const
Returns the number of dynamic fields.
Definition: DynamicNode.h:159
const Field & anyField(const std::string &fieldName) const override
Returns the field base of a specified standard or dynamic field.
DynamicNode()
Creates a new dynamic node object.
const Field & dynamicField(const std::string &fieldName) const
Returns the field base of a specified dynamic field.
std::vector< Field * > DynamicFields
Definition of a vector holding fields.
Definition: DynamicNode.h:43
bool removeField(const std::string &name)
Removes a field added during runtime.
Field & dynamicField(const std::string &fieldName)
Returns the field base of a specified dynamic field.
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:153
bool hasDynamicField(const std::string &name) const
Returns whether this node holds a specific dynamic field.
Field & anyField(const std::string &fieldName) override
Returns the field base of a specified standard or dynamic field.
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
SmartObjectRef< DynamicNode, Node > DynamicNodeRef
Definition of a smart object reference for X scene description nodes.
Definition: DynamicNode.h:23
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15