Ocean
scenedescription/Node.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_NODE_H
9 #define META_OCEAN_SCENEDESCRIPTION_NODE_H
10 
13 
14 #include "ocean/rendering/Engine.h"
15 #include "ocean/rendering/Node.h"
16 
17 #include <map>
18 
19 namespace Ocean
20 {
21 
22 namespace SceneDescription
23 {
24 
25 // Forward declaration.
26 class Field;
27 
28 // Forward declaration.
29 class Node;
30 
31 /**
32  * Definition of a scene description node reference with an internal reference counter.
33  * @see Node.
34  * @ingroup scenedescription
35  */
37 
38 /**
39  * Definition of a vector holding scene description node references.
40  * @ingroup scenedescription
41  */
42 typedef std::vector<NodeRef> NodeRefs;
43 
44 /**
45  * This class is the base class for all scene description nodes.
46  * @ingroup scenedescription
47  */
48 class OCEAN_SCENEDESCRIPTION_EXPORT Node
49 {
50  friend class Ocean::ObjectRef<Node>;
51 
52  public:
53 
54  /**
55  * Definition of different field access types.
56  */
58  {
59  /// No access possible, which can be a static field.
60  ACCESS_NONE = 0,
61  /// Read only field.
62  ACCESS_GET = 1,
63  /// Write only field.
64  ACCESS_SET = 2,
65  /// Read and write field.
66  ACCESS_GET_SET = ACCESS_GET | ACCESS_SET,
67  /// Field which will produce an explicit update event if it receives a new value.
68  ACCESS_EXPLICIT_NOTIFICATION = 4
69  };
70 
71  protected:
72 
73  /**
74  * This class implements a node specification object.
75  */
76  class OCEAN_SCENEDESCRIPTION_EXPORT NodeSpecification
77  {
78  friend class Node;
79 
80  private:
81 
82  /**
83  * Definition of a field specification pair.
84  * The first parameter holds the field address offset.
85  * The second parameter holds the field access type.
86  */
87  typedef std::pair<size_t, FieldAccessType> FieldPair;
88 
89  /**
90  * Definition of a map mapping field names to field address offsets.
91  */
92  typedef std::map<std::string, FieldPair> FieldSpecificationMap;
93 
94  public:
95 
96  /**
97  * Creates a new node specification object.
98  * @param type Name of the node type.
99  */
100  NodeSpecification(const std::string& type);
101 
102  /**
103  * Returns the type of this node.
104  * @return Node type
105  */
106  inline const std::string& type() const;
107 
108  /**
109  * Returns the number registered fields.
110  * @return Field numbers
111  */
112  inline unsigned int size() const;
113 
114  /**
115  * Returns whether the node specification holds a specific field.
116  * @param fieldName Name of the field to check
117  * @return True, if so
118  */
119  bool hasField(const std::string& fieldName) const;
120 
121  /**
122  * Returns the name of the specific field.
123  * @param index Index of the field to return the name for
124  * @return Field name
125  * @exception Is thrown if the index is out of range
126  */
127  const std::string& fieldName(const unsigned int index) const;
128 
129  /**
130  * Returns the access type of a specified field.
131  * @param fieldName Name of the field to check
132  * @return Field access type
133  * @exception Is thrown if the field does not exist
134  */
135  FieldAccessType fieldAccessType(const std::string& fieldName) const;
136 
137  /**
138  * Returns a specific field of a specific node instance.
139  * @param objectAddress Address of the specific node instance holding the field to return
140  * @param fieldName Name of the field to be returned
141  * @return Node field
142  * @exception Is thrown if the field does not exist
143  */
144  const Field& field(const size_t objectAddress, const std::string& fieldName) const;
145 
146  /**
147  * Returns a specific field of a specific node instance.
148  * @param objectAddress Address of the specific node instance holding the field to return
149  * @param fieldName Name of the field to be returned
150  * @return Node field
151  * @exception Is thrown if the field does not exist
152  */
153  Field& field(const size_t objectAddress, const std::string& fieldName);
154 
155  protected:
156 
157  /**
158  * Registers a new field to a specific node type.
159  * @param objectAddress Address of an node which is used to determine the node type only
160  * @param fieldName Name of the field to register
161  * @param field Field to be register
162  * @param accessType Field access type of the field to register
163  */
164  void registerField(const size_t objectAddress, const std::string& fieldName, const Field& field, const FieldAccessType accessType);
165 
166  private:
167 
168  /// Type of the node specified by this specification object.
169  std::string type_;
170 
171  /// Map mapping field name to address offsets.
173  };
174 
175  /**
176  * Definition of a map mapping field names to fields
177  */
178  typedef std::map<std::string, Field*> FieldMap;
179 
180  public:
181 
182  /**
183  * Returns the scene description type of this node.
184  * @return Scene description type
185  */
186  virtual DescriptionType descriptionType() const = 0;
187 
188  /**
189  * Returns the unique node id of this node.
190  * @return Unique node id
191  */
192  inline NodeId id() const;
193 
194  /**
195  * Returns the name of this node.
196  * @return Node name
197  */
198  inline const std::string& name() const;
199 
200  /**
201  * Returns the type of this node.
202  * @return Node type
203  */
204  inline const std::string& type() const;
205 
206  /**
207  * Returns the field base of a specified (standard) field.
208  * @param fieldName Name of the field to return
209  * @return Field base of the specified field
210  * @exception Is thrown if the field does not exist
211  */
212  const Field& field(const std::string& fieldName) const;
213 
214  /**
215  * Returns the field base of a specified (standard) field.
216  * Beware: Changing a field value using this function will not produce any field changing event functions!
217  * @param fieldName Name of the field to return
218  * @return Field base of the specified field
219  * @exception Is thrown if the field does not exist
220  */
221  Field& field(const std::string& fieldName);
222 
223  /**
224  * Returns the field base of a specified standard or dynamic field.
225  * @param fieldName Name of the (standard or dynamic) field to return
226  * @return Field base of the specified field
227  * @exception Is thrown if the field does not exist
228  */
229  virtual const Field& anyField(const std::string& fieldName) const;
230 
231  /**
232  * Returns the field base of a specified standard or dynamic field.
233  * Beware: Changing a field value using this function will not produce any field changing event functions!
234  * @param fieldName Name of the (standard or dynamic) field to return
235  * @return Field base of the specified field
236  * @exception Is thrown if the field does not exist
237  */
238  virtual Field& anyField(const std::string& fieldName);
239 
240  /**
241  * Returns a specified (standard) field.
242  * @param fieldName Name of the field to return
243  * @return Specified field
244  * @exception Is thrown if the field does not exist
245  */
246  template <typename T> const T& field(const std::string& fieldName) const;
247 
248  /**
249  * Returns a specified (standard) field.
250  * Beware: Changing a field value using this function will not produce any field changing event functions!
251  * @param fieldName Name of the field to return
252  * @return Specified field
253  * @exception Is thrown if the field does not exist
254  */
255  template <typename T> T& field(const std::string& fieldName);
256 
257  /**
258  * Returns a specified standard or dynamic field.
259  * @param fieldName Name of the field to return
260  * @return Specified field
261  * @exception Is thrown if the field does not exist
262  */
263  template <typename T> const T& anyField(const std::string& fieldName) const;
264 
265  /**
266  * Returns a specified standard or dynamic field.
267  * Beware: Changing a field value using this function will not produce any field changing event functions!
268  * @param fieldName Name of the field to return
269  * @return Specified field
270  * @exception Is thrown if the field does not exist
271  */
272  template <typename T> T& anyField(const std::string& fieldName);
273 
274  /**
275  * Returns the access type of a specified field.
276  * @param fieldName Name of the field to check
277  * @return Field access type
278  * @exception Is thrown if the field does not exist
279  */
280  FieldAccessType fieldAccessType(const std::string& fieldName) const;
281 
282  /**
283  * Sets the name of this node.
284  * @param name The name of this node to set
285  */
286  virtual void setName(const std::string& name);
287 
288  /**
289  * Returns whether this node has a special (standard) field.
290  * @param fieldName Name of the field
291  * @return True, if so
292  */
293  bool hasField(const std::string& fieldName) const;
294 
295  /**
296  * Returns whether this node has a special standard or dynamic field.
297  * @param fieldName Name of the field
298  * @return True, if so
299  */
300  virtual bool hasAnyField(const std::string& fieldName) const;
301 
302  /**
303  * Tries to translate an alias field name to the original field name.
304  * @param fieldName Name of the alias field to return the original field name for
305  * @return Original field name if existent, otherwise the given aliasFieldName again
306  */
307  virtual std::string originalFieldName(const std::string& fieldName) const;
308 
309  /**
310  * Returns the type of a special field.
311  * @param fieldName Name of the field
312  * @return Scalar of the specified field type
313  * @exception Is thrown if the field does not exist
314  */
315  Field::Type fieldType(const std::string& fieldName) const;
316 
317  /**
318  * Return the dimension of a special field.
319  * @param fieldName Name of the field
320  * @return Dimension of the specified field
321  * @exception Is thrown if the field does not exist
322  */
323  unsigned int fieldDimension(const std::string& fieldName) const;
324 
325  /**
326  * Returns whether this node can hold dynamic generated field.
327  * @return True, if so
328  */
329  virtual bool isDynamic() const;
330 
331  protected:
332 
333  /**
334  * Creates a new node.
335  */
336  Node();
337 
338  /**
339  * Disabled copy constructor.
340  * @param node Object which would be copied
341  */
342  Node(const Node& node) = delete;
343 
344  /**
345  * Destructs a node.
346  */
347  virtual ~Node();
348 
349  /**
350  * Returns the address of the most derived object.
351  * Each derived (not abstract) class must reimplement this function to guarantee a valid field mapping.
352  * @return Address of this object
353  */
354  virtual size_t objectAddress() const;
355 
356  /**
357  * Registers a new field to a specified node type.
358  * @param specification Node specification receiving the new field type
359  * @param fieldName Name of the field to register
360  * @param field Field pattern to register
361  * @param accessType Field access type of the field to register
362  */
363  void registerField(NodeSpecification& specification, const std::string& fieldName, const Field& field, const FieldAccessType accessType = ACCESS_GET_SET);
364 
365  /**
366  * Disabled copy operator.
367  * @param node Object which would be copied
368  * @return Reference to this object
369  */
370  Node& operator=(const Node& node) = delete;
371 
372  /**
373  * Returns the lock for the node id counter.
374  * @return Lock for the node id counter
375  */
377 
378  protected:
379 
380  /// Unique node id.
382 
383  /// Node name
384  std::string name_;
385 
386  /**
387  * Pointer to the node specification, guaranteed to exist as long as the node exist.
388  * This pointer has to be set in derivated classes.
389  * Because it's not a pointer only the object is not disposed.
390  */
391  NodeSpecification* specification_ = nullptr;
392 
393  /// Unique node id counter.
395 };
396 
397 inline const std::string& Node::NodeSpecification::type() const
398 {
399  return type_;
400 }
401 
402 inline unsigned int Node::NodeSpecification::size() const
403 {
404  return (unsigned int)(fields_.size());
405 }
406 
407 inline NodeId Node::id() const
408 {
409  return nodeId_;
410 }
411 
412 inline const std::string& Node::name() const
413 {
414  return name_;
415 }
416 
417 inline const std::string& Node::type() const
418 {
419  ocean_assert(specification_);
420  return specification_->type();
421 }
422 
423 template <typename T>
424 const T& Node::field(const std::string& fieldName) const
425 {
426  const Field& unspecificField(field(fieldName));
427 
428  ocean_assert(unspecificField.type() == T::fieldType);
429  ocean_assert(unspecificField.dimension() == T::fieldDimension);
430 
431  return dynamic_cast<const T&>(unspecificField);
432 }
433 
434 template <typename T>
435 T& Node::field(const std::string& fieldName)
436 {
437  Field& unspecificField(field(fieldName));
438 
439  ocean_assert(unspecificField.type() == T::fieldType);
440  ocean_assert(unspecificField.dimension() == T::fieldDimension);
441 
442  return dynamic_cast<T&>(unspecificField);
443 }
444 
445 template <typename T>
446 const T& Node::anyField(const std::string& fieldName) const
447 {
448  const Field& unspecificField(anyField(fieldName));
449 
450  ocean_assert(unspecificField.type() == T::fieldType);
451  ocean_assert(unspecificField.dimension() == T::fieldDimension);
452 
453  return dynamic_cast<const T&>(unspecificField);
454 }
455 
456 template <typename T>
457 T& Node::anyField(const std::string& fieldName)
458 {
459  Field& unspecificField(anyField(fieldName));
460 
461  ocean_assert(unspecificField.type() == T::fieldType);
462  ocean_assert(unspecificField.dimension() == T::fieldDimension);
463 
464  return dynamic_cast<T&>(unspecificField);
465 }
466 
467 }
468 
469 }
470 
471 #endif // META_OCEAN_SCENEDESCRIPTION_NODE_H
This class implements a recursive lock object.
Definition: Lock.h:31
This template class implements a object reference with an internal reference counter.
Definition: base/ObjectRef.h:58
This class is the base class for all scene description fields.
Definition: Field.h:36
Type
Definition of scalar field types.
Definition: Field.h:43
virtual Type type() const =0
Returns the type of this field.
virtual unsigned int dimension() const =0
Returns the dimension of this field.
This class implements a node specification object.
Definition: scenedescription/Node.h:77
FieldAccessType fieldAccessType(const std::string &fieldName) const
Returns the access type of a specified field.
const std::string & type() const
Returns the type of this node.
Definition: scenedescription/Node.h:397
std::string type_
Type of the node specified by this specification object.
Definition: scenedescription/Node.h:169
Field & field(const size_t objectAddress, const std::string &fieldName)
Returns a specific field of a specific node instance.
const std::string & fieldName(const unsigned int index) const
Returns the name of the specific field.
NodeSpecification(const std::string &type)
Creates a new node specification object.
const Field & field(const size_t objectAddress, const std::string &fieldName) const
Returns a specific field of a specific node instance.
void registerField(const size_t objectAddress, const std::string &fieldName, const Field &field, const FieldAccessType accessType)
Registers a new field to a specific node type.
bool hasField(const std::string &fieldName) const
Returns whether the node specification holds a specific field.
std::map< std::string, FieldPair > FieldSpecificationMap
Definition of a map mapping field names to field address offsets.
Definition: scenedescription/Node.h:92
unsigned int size() const
Returns the number registered fields.
Definition: scenedescription/Node.h:402
std::pair< size_t, FieldAccessType > FieldPair
Definition of a field specification pair.
Definition: scenedescription/Node.h:87
FieldSpecificationMap fields_
Map mapping field name to address offsets.
Definition: scenedescription/Node.h:172
This class is the base class for all scene description nodes.
Definition: scenedescription/Node.h:49
virtual bool isDynamic() const
Returns whether this node can hold dynamic generated field.
Node(const Node &node)=delete
Disabled copy constructor.
virtual const Field & anyField(const std::string &fieldName) const
Returns the field base of a specified standard or dynamic field.
const std::string & name() const
Returns the name of this node.
Definition: scenedescription/Node.h:412
NodeId id() const
Returns the unique node id of this node.
Definition: scenedescription/Node.h:407
virtual size_t objectAddress() const
Returns the address of the most derived object.
Field::Type fieldType(const std::string &fieldName) const
Returns the type of a special field.
static Lock & nodeIdCounterLock()
Returns the lock for the node id counter.
void registerField(NodeSpecification &specification, const std::string &fieldName, const Field &field, const FieldAccessType accessType=ACCESS_GET_SET)
Registers a new field to a specified node type.
Node()
Creates a new node.
FieldAccessType fieldAccessType(const std::string &fieldName) const
Returns the access type of a specified field.
virtual Field & anyField(const std::string &fieldName)
Returns the field base of a specified standard or dynamic field.
const std::string & type() const
Returns the type of this node.
Definition: scenedescription/Node.h:417
virtual DescriptionType descriptionType() const =0
Returns the scene description type of this node.
Node & operator=(const Node &node)=delete
Disabled copy operator.
NodeId nodeId_
Unique node id.
Definition: scenedescription/Node.h:381
std::string name_
Node name.
Definition: scenedescription/Node.h:384
virtual ~Node()
Destructs a node.
const Field & field(const std::string &fieldName) const
Returns the field base of a specified (standard) field.
std::map< std::string, Field * > FieldMap
Definition of a map mapping field names to fields.
Definition: scenedescription/Node.h:178
FieldAccessType
Definition of different field access types.
Definition: scenedescription/Node.h:58
static NodeId nodeIdCounter_
Unique node id counter.
Definition: scenedescription/Node.h:394
NodeSpecification * specification_
Pointer to the node specification, guaranteed to exist as long as the node exist.
Definition: scenedescription/Node.h:391
virtual void setName(const std::string &name)
Sets the name of this node.
virtual bool hasAnyField(const std::string &fieldName) const
Returns whether this node has a special standard or dynamic field.
virtual std::string originalFieldName(const std::string &fieldName) const
Tries to translate an alias field name to the original field name.
unsigned int fieldDimension(const std::string &fieldName) const
Return the dimension of a special field.
bool hasField(const std::string &fieldName) const
Returns whether this node has a special (standard) field.
Field & field(const std::string &fieldName)
Returns the field base of a specified (standard) field.
Ocean::ObjectRef< Node > NodeRef
Definition of a scene description node reference with an internal reference counter.
Definition: scenedescription/Node.h:29
DescriptionType
Definition of different scene description types.
Definition: SceneDescription.h:64
std::vector< NodeRef > NodeRefs
Definition of a vector holding scene description node references.
Definition: scenedescription/Node.h:42
size_t NodeId
Definition of a object id.
Definition: SceneDescription.h:41
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15