1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
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
13 the Free Software Foundation, either version 3 of the License, or
14 (at your 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, see <http://www.gnu.org/licenses/>.
25 Macros to ease declaration of run-time selection tables.
27 declareRunTimeSelectionTable is used to create a run-time selection table
28 for a base-class which holds constructor pointers on the table.
30 declareRunTimeNewSelectionTable is used to create a run-time selection
31 table for a derived-class which holds "New" pointers on the table.
33 \*---------------------------------------------------------------------------*/
37 #ifndef runTimeSelectionTables_H
38 #define runTimeSelectionTables_H
41 #include "HashTable.H"
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 // declare a run-time selection:
49 #define declareRunTimeSelectionTable\
50 (autoPtr,baseType,argNames,argList,parList) \
52 /* Construct from argList function pointer type */ \
53 typedef autoPtr< baseType > (*argNames##ConstructorPtr)argList; \
55 /* Construct from argList function table type */ \
56 typedef HashTable< argNames##ConstructorPtr, word, string::hash > \
57 argNames##ConstructorTable; \
59 /* Construct from argList function pointer table pointer */ \
60 static argNames##ConstructorTable* argNames##ConstructorTablePtr_; \
62 /* Table constructor called from the table add function */ \
63 static void construct##argNames##ConstructorTables(); \
65 /* Table destructor called from the table add function destructor */ \
66 static void destroy##argNames##ConstructorTables(); \
68 /* Class to add constructor from argList to table */ \
69 template< class baseType##Type > \
70 class add##argNames##ConstructorToTable \
74 static autoPtr< baseType > New argList \
76 return autoPtr< baseType >(new baseType##Type parList); \
79 add##argNames##ConstructorToTable \
81 const word& lookup = baseType##Type::typeName \
84 construct##argNames##ConstructorTables(); \
85 argNames##ConstructorTablePtr_->insert(lookup, New); \
88 ~add##argNames##ConstructorToTable() \
90 destroy##argNames##ConstructorTables(); \
94 /* Class to add constructor from argList to table */ \
95 /* Remove only the entry (not the table) upon destruction */ \
96 template< class baseType##Type > \
97 class addRemovable##argNames##ConstructorToTable \
99 /* retain lookup name for later removal */ \
100 const word& lookup_; \
104 static autoPtr< baseType > New argList \
106 return autoPtr< baseType >(new baseType##Type parList); \
109 addRemovable##argNames##ConstructorToTable \
111 const word& lookup = baseType##Type::typeName \
116 construct##argNames##ConstructorTables(); \
117 argNames##ConstructorTablePtr_->set(lookup, New); \
120 ~addRemovable##argNames##ConstructorToTable() \
122 if (argNames##ConstructorTablePtr_) \
124 argNames##ConstructorTablePtr_->erase(lookup_); \
133 // declare a run-time selection for derived classes:
134 #define declareRunTimeNewSelectionTable\
135 (autoPtr,baseType,argNames,argList,parList) \
137 /* Construct from argList function pointer type */ \
138 typedef autoPtr< baseType > (*argNames##ConstructorPtr)argList; \
140 /* Construct from argList function table type */ \
141 typedef HashTable< argNames##ConstructorPtr, word, string::hash > \
142 argNames##ConstructorTable; \
144 /* Construct from argList function pointer table pointer */ \
145 static argNames##ConstructorTable* argNames##ConstructorTablePtr_; \
147 /* Table constructor called from the table add function */ \
148 static void construct##argNames##ConstructorTables(); \
150 /* Table destructor called from the table add function destructor */ \
151 static void destroy##argNames##ConstructorTables(); \
153 /* Class to add constructor from argList to table */ \
154 template< class baseType##Type > \
155 class add##argNames##ConstructorToTable \
159 static autoPtr< baseType > New##baseType argList \
161 return autoPtr< baseType >(baseType##Type::New parList.ptr()); \
164 add##argNames##ConstructorToTable \
166 const word& lookup = baseType##Type::typeName \
169 construct##argNames##ConstructorTables(); \
170 argNames##ConstructorTablePtr_->insert \
177 ~add##argNames##ConstructorToTable() \
179 destroy##argNames##ConstructorTables(); \
183 /* Class to add constructor from argList to table */ \
184 template< class baseType##Type > \
185 class addRemovable##argNames##ConstructorToTable \
187 /* retain lookup name for later removal */ \
188 const word& lookup_; \
192 static autoPtr< baseType > New##baseType argList \
194 return autoPtr< baseType >(baseType##Type::New parList.ptr()); \
197 addRemovable##argNames##ConstructorToTable \
199 const word& lookup = baseType##Type::typeName \
204 construct##argNames##ConstructorTables(); \
205 argNames##ConstructorTablePtr_->set \
212 ~addRemovable##argNames##ConstructorToTable() \
214 if (argNames##ConstructorTablePtr_) \
216 argNames##ConstructorTablePtr_->erase(lookup_); \
224 #define defineRunTimeSelectionTableConstructor\
225 (baseType,argNames) \
227 /* Table constructor called from the table add function */ \
228 void baseType::construct##argNames##ConstructorTables() \
230 static bool constructed = false; \
233 constructed = true; \
234 baseType::argNames##ConstructorTablePtr_ \
235 = new baseType::argNames##ConstructorTable; \
242 #define defineRunTimeSelectionTableDestructor\
243 (baseType,argNames) \
245 /* Table destructor called from the table add function destructor */ \
246 void baseType::destroy##argNames##ConstructorTables() \
248 if (baseType::argNames##ConstructorTablePtr_) \
250 delete baseType::argNames##ConstructorTablePtr_; \
251 baseType::argNames##ConstructorTablePtr_ = NULL; \
257 // create pointer to hash-table of functions
258 #define defineRunTimeSelectionTablePtr\
259 (baseType,argNames) \
261 /* Define the constructor function table */ \
262 baseType::argNames##ConstructorTable* \
263 baseType::argNames##ConstructorTablePtr_ = NULL
267 #define defineTemplateRunTimeSelectionTablePtr(baseType,argNames) \
269 /* Define the constructor function table */ \
270 typename baseType::argNames##ConstructorTable* \
271 baseType::argNames##ConstructorTablePtr_ = NULL
274 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
279 // define run-time selection table
280 #define defineRunTimeSelectionTable\
281 (baseType,argNames) \
283 defineRunTimeSelectionTablePtr(baseType,argNames); \
284 defineRunTimeSelectionTableConstructor(baseType,argNames); \
285 defineRunTimeSelectionTableDestructor(baseType,argNames)
290 // define run-time selection table for template classes
291 // use when baseType doesn't need a template argument (eg, is a typedef)
292 #define defineTemplateRunTimeSelectionTable\
293 (baseType,argNames) \
296 defineRunTimeSelectionTablePtr(baseType,argNames); \
298 defineRunTimeSelectionTableConstructor(baseType,argNames); \
300 defineRunTimeSelectionTableDestructor(baseType,argNames)
303 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
308 // use when baseType requires the Targ template argument
309 #define defineTemplatedRunTimeSelectionTableConstructor\
310 (baseType,argNames,Targ) \
312 /* Table constructor called from the table add function */ \
313 void baseType< Targ >::construct##argNames##ConstructorTables() \
315 static bool constructed = false; \
318 constructed = true; \
319 baseType< Targ >::argNames##ConstructorTablePtr_ \
320 = new baseType< Targ >::argNames##ConstructorTable; \
327 // use when baseType requires the Targ template argument
328 #define defineTemplatedRunTimeSelectionTableDestructor\
329 (baseType,argNames,Targ) \
331 /* Table destructor called from the table add function destructor */ \
332 void baseType< Targ >::destroy##argNames##ConstructorTables() \
334 if (baseType< Targ >::argNames##ConstructorTablePtr_) \
336 delete baseType< Targ >::argNames##ConstructorTablePtr_; \
337 baseType< Targ >::argNames##ConstructorTablePtr_ = NULL; \
343 // create pointer to hash-table of functions
344 // use when baseType requires the Targ template argument
345 #define defineTemplatedRunTimeSelectionTablePtr\
346 (baseType,argNames,Targ) \
348 /* Define the constructor function table */ \
349 baseType< Targ >::argNames##ConstructorTable* \
350 baseType< Targ >::argNames##ConstructorTablePtr_ = NULL
355 // define run-time selection table for template classes
356 // use when baseType requires the Targ template argument
357 #define defineTemplatedRunTimeSelectionTable\
358 (baseType,argNames,Targ) \
361 defineTemplatedRunTimeSelectionTablePtr(baseType,argNames,Targ); \
363 defineTemplatedRunTimeSelectionTableConstructor(baseType,argNames,Targ); \
365 defineTemplatedRunTimeSelectionTableDestructor(baseType,argNames,Targ)
368 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
372 // ************************************************************************* //