1 #ifndef COIN_SOSUBENGINE_H
2 #define COIN_SOSUBENGINE_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 #include <cassert>
37 #include <Inventor/SbName.h>
38 #include <Inventor/SoType.h>
39 #include <Inventor/C/tidbits.h>
40 #include <Inventor/engines/SoEngine.h>
41 #include <Inventor/engines/SoOutputData.h>
42 #include <Inventor/fields/SoFieldData.h>
43 
44 // *************************************************************************
45 
46 //
47 // FIXME: document macros. pederb, 20000309
48 //
49 
50 #define PRIVATE_ENGINE_TYPESYSTEM_HEADER( ) \
51 public: \
52   static SoType getClassTypeId(void); \
53   virtual SoType getTypeId(void) const; \
54 private: \
55   static SoType classTypeId
56 
57 #define SO_ENGINE_ABSTRACT_HEADER(_classname_) \
58   PRIVATE_ENGINE_TYPESYSTEM_HEADER(); \
59 protected: \
60   static const SoFieldData ** getInputDataPtr(void); \
61   static const SoEngineOutputData ** getOutputDataPtr(void); \
62 public: \
63   virtual const SoFieldData * getFieldData(void) const; \
64   virtual const SoEngineOutputData * getOutputData(void) const; \
65 private: \
66   static unsigned int classinstances; \
67   static SoFieldData * inputdata; \
68   static const SoFieldData ** parentinputdata; \
69   static SoEngineOutputData * outputdata; \
70   static const SoEngineOutputData ** parentoutputdata; \
71   static void atexit_cleanup(void)
72 
73 #define SO_ENGINE_HEADER(_classname_) \
74     SO_ENGINE_ABSTRACT_HEADER(_classname_); \
75   public: \
76     static void * createInstance(void)
77 
78 // *************************************************************************
79 
80 #define PRIVATE_ENGINE_TYPESYSTEM_SOURCE(_class_) \
81 SoType _class_::getClassTypeId(void) { return _class_::classTypeId; } \
82 SoType _class_::getTypeId(void) const { return _class_::classTypeId; } \
83 SoType _class_::classTypeId STATIC_SOTYPE_INIT
84 
85 #define SO_ENGINE_ABSTRACT_SOURCE(_class_) \
86 PRIVATE_ENGINE_TYPESYSTEM_SOURCE(_class_); \
87  \
88 unsigned int _class_::classinstances = 0; \
89 SoFieldData * _class_::inputdata = NULL; \
90 const SoFieldData ** _class_::parentinputdata = NULL; \
91 SoEngineOutputData * _class_::outputdata = NULL; \
92 const SoEngineOutputData ** _class_::parentoutputdata = NULL; \
93  \
94 const SoFieldData ** \
95 _class_::getInputDataPtr(void) \
96 { \
97   return const_cast<const SoFieldData **>(&_class_::inputdata); \
98 } \
99  \
100 const SoFieldData * \
101 _class_::getFieldData(void) const \
102 { \
103   return _class_::inputdata; \
104 } \
105  \
106 const SoEngineOutputData ** \
107 _class_::getOutputDataPtr(void) \
108 { \
109   return const_cast<const SoEngineOutputData**>(&_class_::outputdata); \
110 } \
111  \
112 const SoEngineOutputData * \
113 _class_::getOutputData(void) const \
114 { \
115   return _class_::outputdata; \
116 } \
117  \
118 void \
119 _class_::atexit_cleanup(void) \
120 { \
121   delete _class_::inputdata; \
122   delete _class_::outputdata; \
123   _class_::inputdata = NULL; \
124   _class_::outputdata = NULL; \
125   _class_::parentinputdata = NULL; \
126   _class_::parentoutputdata = NULL; \
127   assert(_class_::classTypeId != SoType::badType()); \
128   SoType::removeType(_class_::classTypeId.getName()); \
129   _class_::classTypeId STATIC_SOTYPE_INIT; \
130   _class_::classinstances = 0; \
131 }
132 
133 #define SO_ENGINE_SOURCE(_class_) \
134 SO_ENGINE_ABSTRACT_SOURCE(_class_); \
135  \
136 void * \
137 _class_::createInstance(void) \
138 { \
139   return new _class_; \
140 }
141 
142 // *************************************************************************
143 
144 #define SO_ENGINE_IS_FIRST_INSTANCE() \
145    (classinstances == 1)
146 
147 #define SO_ENGINE_CONSTRUCTOR(_class_) \
148   do { \
149     SoBase::staticDataLock(); \
150     _class_::classinstances++; \
151     /* Catch attempts to use an engine class which has not been initialized. */ \
152     assert(_class_::classTypeId != SoType::badType()); \
153     /* Initialize a inputdata container for the class only once. */ \
154     if (!_class_::inputdata) { \
155       _class_::inputdata = \
156         new SoFieldData(_class_::parentinputdata ? \
157                         *_class_::parentinputdata : NULL); \
158       _class_::outputdata = \
159         new SoEngineOutputData(_class_::parentoutputdata ? \
160                                *_class_::parentoutputdata : NULL); \
161     } \
162     /* Extension classes from the application programmers should not be */ \
163     /* considered native. This is important to get the export code to do */ \
164     /* the Right Thing. */ \
165     this->isBuiltIn = FALSE; \
166     SoBase::staticDataUnlock(); \
167   } WHILE_0
168 
169 // *************************************************************************
170 
171 #define PRIVATE_COMMON_ENGINE_INIT_CODE(_class_, _classname_, _createfunc_, _parentclass_) \
172   do { \
173     /* Make sure we only initialize once. */ \
174     assert(_class_::classTypeId == SoType::badType()); \
175     /* Make sure superclass gets initialized before subclass. */ \
176     assert(_parentclass_::getClassTypeId() != SoType::badType()); \
177  \
178     /* Set up entry in the type system. */ \
179     _class_::classTypeId = \
180       SoType::createType(_parentclass_::getClassTypeId(), \
181                          _classname_, \
182                          _createfunc_); \
183  \
184     /* Store parent's data pointers for later use in the constructor. */ \
185     _class_::parentinputdata = _parentclass_::getInputDataPtr(); \
186     _class_::parentoutputdata = _parentclass_::getOutputDataPtr(); \
187     cc_coin_atexit_static_internal \
188       (reinterpret_cast<coin_atexit_f*>(_class_::atexit_cleanup));  \
189   } WHILE_0
190 
191 
192 #define SO_ENGINE_INIT_CLASS(_class_, _parentclass_, _parentname_) \
193   do { \
194     const char * classname = SO__QUOTE(_class_); \
195     PRIVATE_COMMON_ENGINE_INIT_CODE(_class_, classname, &_class_::createInstance, _parentclass_); \
196   } WHILE_0
197 
198 #define SO_ENGINE_INIT_ABSTRACT_CLASS(_class_, _parentclass_, _parentname_) \
199   do { \
200     const char * classname = SO__QUOTE(_class_); \
201     PRIVATE_COMMON_ENGINE_INIT_CODE(_class_, classname, NULL, _parentclass_); \
202   } WHILE_0
203 
204 // *************************************************************************
205 
206 #define SO_ENGINE_ADD_INPUT(_input_, _defaultval_) \
207   do { \
208     this->_input_.setValue _defaultval_;\
209     this->_input_.setContainer(this); \
210     inputdata->addField(this, SO__QUOTE(_input_), &this->_input_);\
211   } WHILE_0
212 
213 #define SO_ENGINE_ADD_OUTPUT(_output_, _type_) \
214   do { \
215     outputdata->addOutput(this, SO__QUOTE(_output_), \
216                           &this->_output_, \
217                           _type_::getClassTypeId()); \
218     this->_output_.setContainer(this); \
219   } WHILE_0
220 
221 // *************************************************************************
222 
223 #define SO_ENGINE_DEFINE_ENUM_VALUE(_enumname_, _enumval_) \
224   do { \
225     inputdata->addEnumValue(SO__QUOTE(_enumname_), \
226                             SO__QUOTE(_enumval_), _enumval_); \
227   } WHILE_0
228 
229 #define SO_ENGINE_OUTPUT(_engineout_, _fieldtype_, _writeop_) \
230   do { \
231     if (_engineout_.isEnabled()) { \
232       /* No fields can be added or removed during this loop, as it */ \
233       /* is a "closed" operation. (The fields are disabled for */ \
234       /* notification while the loop runs). */ \
235       int SO_ENGINE_OUTPUT_numconnections = _engineout_.getNumConnections(); \
236       /* The reason we use the perverted variable names is to */ \
237       /* avoid the possibility of getting _extremely_ hard */ \
238       /* to find bugs when _writeop_ contains the same variable */ \
239       /* names we are using internally in the macro. */ \
240       for (int SO_ENGINE_OUTPUT_i = 0; SO_ENGINE_OUTPUT_i < SO_ENGINE_OUTPUT_numconnections; SO_ENGINE_OUTPUT_i++) { \
241         _fieldtype_ * SO_ENGINE_OUTPUT_field = \
242            static_cast<_fieldtype_*>(_engineout_[SO_ENGINE_OUTPUT_i]); \
243         if (!SO_ENGINE_OUTPUT_field->isReadOnly()) { SO_ENGINE_OUTPUT_field->_writeop_; } \
244       } \
245       /* paranoid assertion */ \
246       assert(_engineout_.getNumConnections() == SO_ENGINE_OUTPUT_numconnections); \
247     } \
248   } WHILE_0
249 
250 // *************************************************************************
251 
252 #define SO_COMPOSE__HEADER(_name_) \
253   SO_ENGINE_HEADER(_name_); \
254   private: \
255     virtual void evaluate(); \
256   protected: \
257     virtual ~_name_();\
258   public: \
259    _name_(); \
260     static void initClass()
261 
262 // *************************************************************************
263 
264 #endif // !COIN_SOSUBENGINE_H
265