1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM 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 2 of the License, or (at your
14 option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 Foam::runTimeSelectionTables
29 Macros to enable the easy declaration of run-time selection tables.
31 declareRunTimeSelectionTable is used to create a run-time selection table
32 for a base-class which holds constructor pointers on the table.
34 declareRunTimeNewSelectionTable is used to create a run-time selection
35 table for a derived-class which holds "New" pointers on the table.
37 \*---------------------------------------------------------------------------*/
41 #ifndef runTimeSelectionTables_H
42 #define runTimeSelectionTables_H
45 #include "HashTable.H"
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52 // declare a run-time selection:
53 #define declareRunTimeSelectionTable\
54 (autoPtr,baseType,argNames,argList,parList) \
56 /* Construct from argList function pointer type */ \
57 typedef autoPtr< baseType > (*argNames##ConstructorPtr)argList; \
59 /* Construct from argList function table type */ \
60 typedef HashTable< argNames##ConstructorPtr, word, string::hash > \
61 argNames##ConstructorTable; \
63 /* Construct from argList function pointer table pointer */ \
64 static argNames##ConstructorTable* argNames##ConstructorTablePtr_; \
66 /* Class to add constructor from argList to table */ \
67 template< class baseType##Type > \
68 class add##argNames##ConstructorToTable \
72 static autoPtr< baseType > New argList \
74 return autoPtr< baseType >(new baseType##Type parList); \
77 add##argNames##ConstructorToTable \
79 const word& lookup = baseType##Type::typeName \
82 construct##argNames##ConstructorTables(); \
83 argNames##ConstructorTablePtr_->insert(lookup, New); \
86 ~add##argNames##ConstructorToTable() \
88 destroy##argNames##ConstructorTables(); \
92 /* Table constructor called from the table add function */ \
93 static void construct##argNames##ConstructorTables(); \
95 /* Table destructor called from the table add function destructor */ \
96 static void destroy##argNames##ConstructorTables()
101 // declare a run-time selection for derived classes:
102 #define declareRunTimeNewSelectionTable\
103 (autoPtr,baseType,argNames,argList,parList) \
105 /* Construct from argList function pointer type */ \
106 typedef autoPtr< baseType > (*argNames##ConstructorPtr)argList; \
108 /* Construct from argList function table type */ \
109 typedef HashTable< argNames##ConstructorPtr, word, string::hash > \
110 argNames##ConstructorTable; \
112 /* Construct from argList function pointer table pointer */ \
113 static argNames##ConstructorTable* argNames##ConstructorTablePtr_; \
115 /* Class to add constructor from argList to table */ \
116 template< class baseType##Type > \
117 class add##argNames##ConstructorToTable \
121 static autoPtr< baseType > New##baseType argList \
123 return autoPtr< baseType >(baseType##Type::New parList.ptr()); \
126 add##argNames##ConstructorToTable \
128 const word& lookup = baseType##Type::typeName \
131 construct##argNames##ConstructorTables(); \
132 argNames##ConstructorTablePtr_->insert \
139 ~add##argNames##ConstructorToTable() \
141 destroy##argNames##ConstructorTables(); \
145 /* Table constructor called from the table add function */ \
146 static void construct##argNames##ConstructorTables(); \
148 /* Table destructor called from the table add function destructor */ \
149 static void destroy##argNames##ConstructorTables()
154 #define defineRunTimeSelectionTableConstructor\
155 (baseType,argNames) \
157 /* Table constructor called from the table add function */ \
158 void baseType::construct##argNames##ConstructorTables() \
160 static bool constructed = false; \
164 baseType::argNames##ConstructorTablePtr_ \
165 = new baseType::argNames##ConstructorTable; \
167 constructed = true; \
174 #define defineRunTimeSelectionTableDestructor\
175 (baseType,argNames) \
177 /* Table destructor called from the table add function destructor */ \
178 void baseType::destroy##argNames##ConstructorTables() \
180 if (baseType::argNames##ConstructorTablePtr_) \
182 delete baseType::argNames##ConstructorTablePtr_; \
183 baseType::argNames##ConstructorTablePtr_ = NULL; \
189 // create pointer to hash-table of functions
190 #define defineRunTimeSelectionTablePtr\
191 (baseType,argNames) \
193 /* Define the constructor function table */ \
194 baseType::argNames##ConstructorTable* \
195 baseType::argNames##ConstructorTablePtr_ = NULL
199 #define defineTemplateRunTimeSelectionTablePtr(baseType,argNames) \
201 /* Define the constructor function table */ \
202 typename baseType::argNames##ConstructorTable* \
203 baseType::argNames##ConstructorTablePtr_ = NULL
206 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
211 // define run-time selection table
212 #define defineRunTimeSelectionTable\
213 (baseType,argNames) \
215 defineRunTimeSelectionTablePtr(baseType,argNames); \
216 defineRunTimeSelectionTableConstructor(baseType,argNames); \
217 defineRunTimeSelectionTableDestructor(baseType,argNames)
222 // define run-time selection table for template classes
223 // use when baseType doesn't need a template argument (eg, is a typedef)
224 #define defineTemplateRunTimeSelectionTable\
225 (baseType,argNames) \
228 defineRunTimeSelectionTablePtr(baseType,argNames); \
230 defineRunTimeSelectionTableConstructor(baseType,argNames); \
232 defineRunTimeSelectionTableDestructor(baseType,argNames)
235 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
240 // use when baseType requires the Targ template argument
241 #define defineTemplatedRunTimeSelectionTableConstructor\
242 (baseType,argNames,Targ) \
244 /* Table constructor called from the table add function */ \
245 void baseType< Targ >::construct##argNames##ConstructorTables() \
247 static bool constructed = false; \
251 baseType< Targ >::argNames##ConstructorTablePtr_ \
252 = new baseType< Targ >::argNames##ConstructorTable; \
254 constructed = true; \
261 // use when baseType requires the Targ template argument
262 #define defineTemplatedRunTimeSelectionTableDestructor\
263 (baseType,argNames,Targ) \
265 /* Table destructor called from the table add function destructor */ \
266 void baseType< Targ >::destroy##argNames##ConstructorTables() \
268 if (baseType< Targ >::argNames##ConstructorTablePtr_) \
270 delete baseType< Targ >::argNames##ConstructorTablePtr_; \
271 baseType< Targ >::argNames##ConstructorTablePtr_ = NULL; \
277 // create pointer to hash-table of functions
278 // use when baseType requires the Targ template argument
279 #define defineTemplatedRunTimeSelectionTablePtr\
280 (baseType,argNames,Targ) \
282 /* Define the constructor function table */ \
283 baseType< Targ >::argNames##ConstructorTable* \
284 baseType< Targ >::argNames##ConstructorTablePtr_ = NULL
289 // define run-time selection table for template classes
290 // use when baseType requires the Targ template argument
291 #define defineTemplatedRunTimeSelectionTable\
292 (baseType,argNames,Targ) \
295 defineTemplatedRunTimeSelectionTablePtr(baseType,argNames,Targ); \
297 defineTemplatedRunTimeSelectionTableConstructor(baseType,argNames,Targ); \
299 defineTemplatedRunTimeSelectionTableDestructor(baseType,argNames,Targ)
302 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
306 // ************************************************************************* //