Coin: SoSubNode.h Source File

| 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( ) \
68public: \
69 static SoType getClassTypeId(void); \
70 virtual SoType getTypeId(void) const; \
71private: \
72 static SoType classTypeId
73
74// FIXME: document. 20000103 mortene.
75#define SO_NODE_ABSTRACT_HEADER(_class_) \
76 PRIVATE_NODE_TYPESYSTEM_HEADER(); \
77protected: \
78 static const SoFieldData ** getFieldDataPtr(void); \
79 virtual const SoFieldData * getFieldData(void) const; \
80private: \
81 static void atexit_cleanup(void); \
82 static const SoFieldData ** parentFieldData; \
83 static SoFieldData * fieldData; \
84 /* Counts number of instances of subclasses as well 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_); \
92private: \
93 static void * createInstance(void)
94
95// *************************************************************************
96
97#define PRIVATE_NODE_TYPESYSTEM_SOURCE(_class_) \
98 \
99
102 \
103SoType _class_::getClassTypeId(void) { return _class_::classTypeId; } \
104 \
105
109 \
110SoType _class_::getTypeId(void) const { return _class_::classTypeId; } \
111SoType _class_::classTypeId STATIC_SOTYPE_INIT
112
113
114// FIXME: document. 20000103 mortene.
115#define SO_NODE_ABSTRACT_SOURCE(_class_) \
116PRIVATE_NODE_TYPESYSTEM_SOURCE(_class_); \
117 \
118unsigned int _class_::classinstances = 0; \
119const SoFieldData ** _class_::parentFieldData = NULL; \
120SoFieldData * _class_::fieldData = NULL; \
121 \
122
127 \
128const SoFieldData ** \
129_class_::getFieldDataPtr(void) \
130{ \
131 return const_cast<const SoFieldData **>(&_class_::fieldData); \
132} \
133 \
134const SoFieldData * \
135_class_::getFieldData(void) const \
136{ \
137 return _class_::fieldData; \
138} \
139 \
140void \
141_class_::atexit_cleanup(void) \
142{ \
143 delete _class_::fieldData; \
144 _class_::fieldData = NULL; \
145 _class_::parentFieldData = NULL; \
146 SoType::removeType(_class_::classTypeId.getName()); \
147 _class_::classTypeId STATIC_SOTYPE_INIT; \
148 _class_::classinstances = 0; \
149}
150
151// FIXME: document. 20000103 mortene.
152#define SO_NODE_SOURCE(_class_) \
153SO_NODE_ABSTRACT_SOURCE(_class_) \
154 \
155
157 \
158void * \
159_class_::createInstance(void) \
160{ \
161 return new _class_; \
162}
163
164// *************************************************************************
165
166// FIXME: document. 20000103 mortene.
167#define SO_NODE_IS_FIRST_INSTANCE() \
168 (classinstances == 1)
169
170// FIXME: document. 20000103 mortene.
171#define SO_NODE_CONSTRUCTOR_NOLOCK(_class_) \
172 do { \
173 _class_::classinstances++; \
174 /* Catch attempts to use a node class which has not been initialized. */ \
175 assert(_class_::classTypeId != SoType::badType() && "you forgot init()!"); \
176 /* Initialize a fielddata container for the class only once. */ \
177 if (!_class_::fieldData) { \
178 /* FIXME: this is a "static" memory leak. 20030131 mortene. */ \
179 /* Internal Coin nodes are handled properly though. pederb, 20041122 */ \
180 _class_::fieldData = \
181 new SoFieldData(_class_::parentFieldData ? \
182 *_class_::parentFieldData : NULL); \
183 } \
184 /* Extension classes from the application programmers should not be \
185 considered native. This is important to get the export code to do \
186 the Right Thing. */ \
187 this->isBuiltIn = FALSE; \
188 } WHILE_0
189
190// FIXME: document. 20000103 mortene.
191#define SO_NODE_CONSTRUCTOR(_class_) \
192 do { \
193 SoBase::staticDataLock(); \
194 SO_NODE_CONSTRUCTOR_NOLOCK(_class_); \
195 SoBase::staticDataUnlock(); \
196 } WHILE_0
197
198// *************************************************************************
199
200// FIXME: create-type with get-next and inc-next must be an atomic step in an MT
201// environment 20020216 larsa
202// FIXME: you can not use the _parentclass_ argument directly as a class specifier
203// in initClass because such usage causes dynamic loading to fail when trying to
204// invoke the initClass method in the case where _parentclass_ also has to be
205// dynamically loaded. that is the reason for this roundabout way of getting the
206// parent class type. 20020216 larsa
207
208#define PRIVATE_COMMON_INIT_CODE(_class_, _classname_, _createfunc_, _parentclass_) \
209 do { \
210 /* Make sure we only initialize once. */ \
211 assert(_class_::classTypeId == SoType::badType() && "don't init() twice!"); \
212 /* Make sure superclass gets initialized before subclass. */ \
213 assert(strcmp(SO__QUOTE(_parentclass_), "inherited")); \
214 SoType parentType(SoType::fromName(SO__QUOTE(_parentclass_))); \
215 assert(parentType != SoType::badType() && "you forgot init() on parentclass!"); \
216 \
217 /* Set up entry in the type system. */ \
218 _class_::classTypeId = \
219 SoType::createType(parentType, \
220 _classname_, \
221 _createfunc_, \
222 (uint16_t)SoNode::getNextActionMethodIndex()); \
223 SoNode::incNextActionMethodIndex(); \
224 \
225 /* Store parent's fielddata pointer for later use in the constructor. */ \
226 _class_::parentFieldData = _parentclass_::getFieldDataPtr(); \
227\
228 /* Make sure also external nodes are cleaned up */ \
229 cc_coin_atexit_static_internal(_class_::atexit_cleanup); \
230 } WHILE_0
231
232
233// FIXME: document. 20000103 mortene.
234#define SO_NODE_INIT_CLASS(_class_, _parentclass_, _parentname_) \
235 do { \
236 const char * classname = SO__QUOTE(_class_); \
237 PRIVATE_COMMON_INIT_CODE(_class_, classname, &_class_::createInstance, _parentclass_); \
238 } WHILE_0
239
240// FIXME: document. 20000103 mortene.
241#define SO_NODE_INIT_ABSTRACT_CLASS(_class_, _parentclass_, _parentname_) \
242 do { \
243 const char * classname = SO__QUOTE(_class_); \
244 PRIVATE_COMMON_INIT_CODE(_class_, classname, NULL, _parentclass_); \
245 } WHILE_0
246
247
248// *************************************************************************
249
250// FIXME: document. 20000103 mortene.
251#define SO_NODE_ADD_FIELD(_field_, _defaultval_) \
252 do { \
253 this->_field_.setValue _defaultval_;\
254 this->_field_.setContainer(this); \
255 fieldData->addField(this, SO__QUOTE(_field_), &this->_field_); \
256 } WHILE_0
257
258// New for Coin-3
259#define SO_NODE_ADD_EMPTY_MFIELD(_field_) \
260 do { \
261 this->_field_.setContainer(this); \
262 fieldData->addField(this, SO__QUOTE(_field_), &this->_field_);\
263 } WHILE_0
264
265
266// FIXME: document. 20000103 mortene.
267#define SO_NODE_DEFINE_ENUM_VALUE(_enumname_, _enumval_) \
268 do { \
269 fieldData->addEnumValue(SO__QUOTE(_enumname_), \
270 SO__QUOTE(_enumval_), _enumval_); \
271 } WHILE_0
272
273// *************************************************************************
274
275#endif // !COIN_SOSUBNODE_H