Coin Logo Coin3D is Free Software,
published under the BSD 3-clause license.
https://coin3d.github.io
https://www.kongsberg.com/en/kogt/
SoSubNode.h
1 #ifndef COIN_SOSUBNODE_H
2 #define COIN_SOSUBNODE_H
3 
4 /**************************************************************************\
5  * Copyright (c) Kongsberg Oil & Gas Technologies AS
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * Neither the name of the copyright holder nor the names of its
20  * contributors may be used to endorse or promote products derived from
21  * this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 \**************************************************************************/
35 
36 /*
37  The most used macros and their arguments:
38 
39  header files:
40  SO_NODE_HEADER(classname)
41  SO_NODE_ABSTRACT_HEADER(classname)
42 
43  source files:
44  SO_NODE_SOURCE(classname)
45  SO_NODE_ABSTRACT_SOURCE(classname)
46  in constructor:
47  SO_NODE_CONSTRUCTOR(classname)
48  SO_NODE_DEFINE_ENUM_VALUE(enumname, enumvalue)
49  SO_NODE_ADD_FIELD(fieldname, (defaultvalue))
50  in initClass():
51  SO_NODE_INIT_CLASS(classname, parentclassname, parentclassname)
52  SO_NODE_INIT_ABSTRACT_CLASS(classname, parentclassname, parentclassname)
53 */
54 
55 // *************************************************************************
56 
57 #include <cstring> /* strcmp used in assert() */
58 #include <Inventor/SbBasic.h>
59 #include <Inventor/SbName.h>
60 #include <Inventor/SoType.h>
61 #include <Inventor/fields/SoFieldData.h>
62 #include <Inventor/nodes/SoNode.h>
63 #include <Inventor/C/tidbits.h>
64 
65 // *************************************************************************
66 
67 #define PRIVATE_NODE_TYPESYSTEM_HEADER( ) \
68 public: \
69  static SoType getClassTypeId(void); \
70  virtual SoType getTypeId(void) const; \
71 private: \
72  static SoType classTypeId
73 
74 // FIXME: document. 20000103 mortene.
75 #define SO_NODE_ABSTRACT_HEADER(_class_) \
76  PRIVATE_NODE_TYPESYSTEM_HEADER(); \
77 protected: \
78  static const SoFieldData ** getFieldDataPtr(void); \
79  virtual const SoFieldData * getFieldData(void) const; \
80 private: \
81  static void atexit_cleanup(void); \
82  static const SoFieldData ** parentFieldData; \
83  static SoFieldData * fieldData; \
84  /* Counts number of instances of subclasses aswell as "direct" */ \
85  /* instances from non-abstract classes. */ \
86  static unsigned int classinstances
87 
88 
89 // FIXME: document. 20000103 mortene.
90 #define SO_NODE_HEADER(_class_) \
91  SO_NODE_ABSTRACT_HEADER(_class_); \
92 private: \
93  static void * createInstance(void)
94 
95 // *************************************************************************
96 
97 #define PRIVATE_NODE_TYPESYSTEM_SOURCE(_class_) \
98 SoType _class_::getClassTypeId(void) { return _class_::classTypeId; } \
99 SoType _class_::getTypeId(void) const { return _class_::classTypeId; } \
100 SoType _class_::classTypeId STATIC_SOTYPE_INIT
101 
102 
103 // FIXME: document. 20000103 mortene.
104 #define SO_NODE_ABSTRACT_SOURCE(_class_) \
105 PRIVATE_NODE_TYPESYSTEM_SOURCE(_class_); \
106  \
107 unsigned int _class_::classinstances = 0; \
108 const SoFieldData ** _class_::parentFieldData = NULL; \
109 SoFieldData * _class_::fieldData = NULL; \
110  \
111 const SoFieldData ** \
112 _class_::getFieldDataPtr(void) \
113 { \
114  return const_cast<const SoFieldData **>(&_class_::fieldData); \
115 } \
116  \
117 const SoFieldData * \
118 _class_::getFieldData(void) const \
119 { \
120  return _class_::fieldData; \
121 } \
122  \
123 void \
124 _class_::atexit_cleanup(void) \
125 { \
126  delete _class_::fieldData; \
127  _class_::fieldData = NULL; \
128  _class_::parentFieldData = NULL; \
129  SoType::removeType(_class_::classTypeId.getName()); \
130  _class_::classTypeId STATIC_SOTYPE_INIT; \
131  _class_::classinstances = 0; \
132 }
133 
134 // FIXME: document. 20000103 mortene.
135 #define SO_NODE_SOURCE(_class_) \
136 SO_NODE_ABSTRACT_SOURCE(_class_) \
137  \
138  \
141 void * \
142 _class_::createInstance(void) \
143 { \
144  return new _class_; \
145 }
146 
147 // *************************************************************************
148 
149 // FIXME: document. 20000103 mortene.
150 #define SO_NODE_IS_FIRST_INSTANCE() \
151  (classinstances == 1)
152 
153 // FIXME: document. 20000103 mortene.
154 #define SO_NODE_CONSTRUCTOR_NOLOCK(_class_) \
155  do { \
156  _class_::classinstances++; \
157  /* Catch attempts to use a node class which has not been initialized. */ \
158  assert(_class_::classTypeId != SoType::badType() && "you forgot init()!"); \
159  /* Initialize a fielddata container for the class only once. */ \
160  if (!_class_::fieldData) { \
161  /* FIXME: this is a "static" memory leak. 20030131 mortene. */ \
162  /* Internal Coin nodes are handled properly though. pederb, 20041122 */ \
163  _class_::fieldData = \
164  new SoFieldData(_class_::parentFieldData ? \
165  *_class_::parentFieldData : NULL); \
166  } \
167  /* Extension classes from the application programmers should not be \
168  considered native. This is important to get the export code to do \
169  the Right Thing. */ \
170  this->isBuiltIn = FALSE; \
171  } WHILE_0
172 
173 // FIXME: document. 20000103 mortene.
174 #define SO_NODE_CONSTRUCTOR(_class_) \
175  do { \
176  SoBase::staticDataLock(); \
177  SO_NODE_CONSTRUCTOR_NOLOCK(_class_); \
178  SoBase::staticDataUnlock(); \
179  } WHILE_0
180 
181 // *************************************************************************
182 
183 // FIXME: create-type with get-next and inc-next must be an atomic step in an MT
184 // environment 20020216 larsa
185 // FIXME: you can not use the _parentclass_ argument directly as a class specifier
186 // in initClass because such usage causes dynamic loading to fail when trying to
187 // invoke the initClass method in the case where _parentclass_ also has to be
188 // dynamically loaded. that is the reason for this roundabout way of getting the
189 // parent class type. 20020216 larsa
190 
191 #define PRIVATE_COMMON_INIT_CODE(_class_, _classname_, _createfunc_, _parentclass_) \
192  do { \
193  /* Make sure we only initialize once. */ \
194  assert(_class_::classTypeId == SoType::badType() && "don't init() twice!"); \
195  /* Make sure superclass gets initialized before subclass. */ \
196  assert(strcmp(SO__QUOTE(_parentclass_), "inherited")); \
197  SoType parentType(SoType::fromName(SO__QUOTE(_parentclass_))); \
198  assert(parentType != SoType::badType() && "you forgot init() on parentclass!"); \
199  \
200  /* Set up entry in the type system. */ \
201  _class_::classTypeId = \
202  SoType::createType(parentType, \
203  _classname_, \
204  _createfunc_, \
205  (uint16_t)SoNode::getNextActionMethodIndex()); \
206  SoNode::incNextActionMethodIndex(); \
207  \
208  /* Store parent's fielddata pointer for later use in the constructor. */ \
209  _class_::parentFieldData = _parentclass_::getFieldDataPtr(); \
210 \
211  /* Make sure also external nodes are cleaned up */ \
212  cc_coin_atexit_static_internal(_class_::atexit_cleanup); \
213  } WHILE_0
214 
215 
216 // FIXME: document. 20000103 mortene.
217 #define SO_NODE_INIT_CLASS(_class_, _parentclass_, _parentname_) \
218  do { \
219  const char * classname = SO__QUOTE(_class_); \
220  PRIVATE_COMMON_INIT_CODE(_class_, classname, &_class_::createInstance, _parentclass_); \
221  } WHILE_0
222 
223 // FIXME: document. 20000103 mortene.
224 #define SO_NODE_INIT_ABSTRACT_CLASS(_class_, _parentclass_, _parentname_) \
225  do { \
226  const char * classname = SO__QUOTE(_class_); \
227  PRIVATE_COMMON_INIT_CODE(_class_, classname, NULL, _parentclass_); \
228  } WHILE_0
229 
230 
231 // *************************************************************************
232 
233 // FIXME: document. 20000103 mortene.
234 #define SO_NODE_ADD_FIELD(_field_, _defaultval_) \
235  do { \
236  this->_field_.setValue _defaultval_;\
237  this->_field_.setContainer(this); \
238  fieldData->addField(this, SO__QUOTE(_field_), &this->_field_); \
239  } WHILE_0
240 
241 // New for Coin-3
242 #define SO_NODE_ADD_EMPTY_MFIELD(_field_) \
243  do { \
244  this->_field_.setContainer(this); \
245  fieldData->addField(this, SO__QUOTE(_field_), &this->_field_);\
246  } WHILE_0
247 
248 
249 // FIXME: document. 20000103 mortene.
250 #define SO_NODE_DEFINE_ENUM_VALUE(_enumname_, _enumval_) \
251  do { \
252  fieldData->addEnumValue(SO__QUOTE(_enumname_), \
253  SO__QUOTE(_enumval_), _enumval_); \
254  } WHILE_0
255 
256 // *************************************************************************
257 
258 #endif // !COIN_SOSUBNODE_H