1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
25 Foam::runTimeSelectionTables
28 Macros to enable the easy declaration of run-time selection tables.
30 declareRunTimeSelectionTable is used to create a run-time selection table
31 for a base-class which holds constructor pointers on the table.
33 declareRunTimeNewSelectionTable is used to create a run-time selection
34 table for a derived-class which holds "New" pointers on the table.
36 \*---------------------------------------------------------------------------*/
40 #ifndef runTimeSelectionTables_H
41 #define runTimeSelectionTables_H
44 #include "HashTable.H"
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 // declare a run-time selection:
52 #define declareRunTimeSelectionTable\
53 (autoPtr,baseType,argNames,argList,parList) \
55 /* Construct from argList function pointer type */ \
56 typedef autoPtr< baseType > (*argNames##ConstructorPtr)argList; \
58 /* Construct from argList function table type */ \
59 typedef HashTable< argNames##ConstructorPtr, word, string::hash > \
60 argNames##ConstructorTable; \
62 /* Construct from argList function pointer table pointer */ \
63 static argNames##ConstructorTable* argNames##ConstructorTablePtr_; \
65 /* Class to add constructor from argList to table */ \
66 template< class baseType##Type > \
67 class add##argNames##ConstructorToTable \
71 static autoPtr< baseType > New argList \
73 return autoPtr< baseType >(new baseType##Type parList); \
76 add##argNames##ConstructorToTable \
78 const word& lookup = baseType##Type::typeName \
81 construct##argNames##ConstructorTables(); \
82 argNames##ConstructorTablePtr_->insert(lookup, New); \
85 ~add##argNames##ConstructorToTable() \
87 destroy##argNames##ConstructorTables(); \
91 /* Table constructor called from the table add function */ \
92 static void construct##argNames##ConstructorTables(); \
94 /* Table destructor called from the table add function destructor */ \
95 static void destroy##argNames##ConstructorTables()
100 // declare a run-time selection for derived classes:
101 #define declareRunTimeNewSelectionTable\
102 (autoPtr,baseType,argNames,argList,parList) \
104 /* Construct from argList function pointer type */ \
105 typedef autoPtr< baseType > (*argNames##ConstructorPtr)argList; \
107 /* Construct from argList function table type */ \
108 typedef HashTable< argNames##ConstructorPtr, word, string::hash > \
109 argNames##ConstructorTable; \
111 /* Construct from argList function pointer table pointer */ \
112 static argNames##ConstructorTable* argNames##ConstructorTablePtr_; \
114 /* Class to add constructor from argList to table */ \
115 template< class baseType##Type > \
116 class add##argNames##ConstructorToTable \
120 static autoPtr< baseType > New##baseType argList \
122 return autoPtr< baseType >(baseType##Type::New parList.ptr()); \
125 add##argNames##ConstructorToTable \
127 const word& lookup = baseType##Type::typeName \
130 construct##argNames##ConstructorTables(); \
131 argNames##ConstructorTablePtr_->insert \
138 ~add##argNames##ConstructorToTable() \
140 destroy##argNames##ConstructorTables(); \
144 /* Table constructor called from the table add function */ \
145 static void construct##argNames##ConstructorTables(); \
147 /* Table destructor called from the table add function destructor */ \
148 static void destroy##argNames##ConstructorTables()
153 #define defineRunTimeSelectionTableConstructor\
154 (baseType,argNames) \
156 /* Table constructor called from the table add function */ \
157 void baseType::construct##argNames##ConstructorTables() \
159 static bool constructed = false; \
163 baseType::argNames##ConstructorTablePtr_ \
164 = new baseType::argNames##ConstructorTable; \
166 constructed = true; \
173 #define defineRunTimeSelectionTableDestructor\
174 (baseType,argNames) \
176 /* Table destructor called from the table add function destructor */ \
177 void baseType::destroy##argNames##ConstructorTables() \
179 if (baseType::argNames##ConstructorTablePtr_) \
181 delete baseType::argNames##ConstructorTablePtr_; \
182 baseType::argNames##ConstructorTablePtr_ = NULL; \
188 // create pointer to hash-table of functions
189 #define defineRunTimeSelectionTablePtr\
190 (baseType,argNames) \
192 /* Define the constructor function table */ \
193 baseType::argNames##ConstructorTable* \
194 baseType::argNames##ConstructorTablePtr_ = NULL
198 #define defineTemplateRunTimeSelectionTablePtr(baseType,argNames) \
200 /* Define the constructor function table */ \
201 typename baseType::argNames##ConstructorTable* \
202 baseType::argNames##ConstructorTablePtr_ = NULL
205 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
210 // define run-time selection table
211 #define defineRunTimeSelectionTable\
212 (baseType,argNames) \
214 defineRunTimeSelectionTablePtr(baseType,argNames); \
215 defineRunTimeSelectionTableConstructor(baseType,argNames); \
216 defineRunTimeSelectionTableDestructor(baseType,argNames)
221 // define run-time selection table for template classes
222 // use when baseType doesn't need a template argument (eg, is a typedef)
223 #define defineTemplateRunTimeSelectionTable\
224 (baseType,argNames) \
227 defineRunTimeSelectionTablePtr(baseType,argNames); \
229 defineRunTimeSelectionTableConstructor(baseType,argNames); \
231 defineRunTimeSelectionTableDestructor(baseType,argNames)
234 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
239 // use when baseType requires the Targ template argument
240 #define defineTemplatedRunTimeSelectionTableConstructor\
241 (baseType,argNames,Targ) \
243 /* Table constructor called from the table add function */ \
244 void baseType< Targ >::construct##argNames##ConstructorTables() \
246 static bool constructed = false; \
250 baseType< Targ >::argNames##ConstructorTablePtr_ \
251 = new baseType< Targ >::argNames##ConstructorTable; \
253 constructed = true; \
260 // use when baseType requires the Targ template argument
261 #define defineTemplatedRunTimeSelectionTableDestructor\
262 (baseType,argNames,Targ) \
264 /* Table destructor called from the table add function destructor */ \
265 void baseType< Targ >::destroy##argNames##ConstructorTables() \
267 if (baseType< Targ >::argNames##ConstructorTablePtr_) \
269 delete baseType< Targ >::argNames##ConstructorTablePtr_; \
270 baseType< Targ >::argNames##ConstructorTablePtr_ = NULL; \
276 // create pointer to hash-table of functions
277 // use when baseType requires the Targ template argument
278 #define defineTemplatedRunTimeSelectionTablePtr\
279 (baseType,argNames,Targ) \
281 /* Define the constructor function table */ \
282 baseType< Targ >::argNames##ConstructorTable* \
283 baseType< Targ >::argNames##ConstructorTablePtr_ = NULL
288 // define run-time selection table for template classes
289 // use when baseType requires the Targ template argument
290 #define defineTemplatedRunTimeSelectionTable\
291 (baseType,argNames,Targ) \
294 defineTemplatedRunTimeSelectionTablePtr(baseType,argNames,Targ); \
296 defineTemplatedRunTimeSelectionTableConstructor(baseType,argNames,Targ); \
298 defineTemplatedRunTimeSelectionTableDestructor(baseType,argNames,Targ)
301 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
305 // ************************************************************************* //