initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / db / runTimeSelection / runTimeSelectionTables.H
blob8e9b4ea54e8f7bd30bd893f60294acceeab37785
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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
25 Class
26     Foam::runTimeSelectionTables
28 Description
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 \*---------------------------------------------------------------------------*/
39 #include "token.H"
41 #ifndef runTimeSelectionTables_H
42 #define runTimeSelectionTables_H
44 #include "autoPtr.H"
45 #include "HashTable.H"
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 // external use:
51 // ~~~~~~~~~~~~~
52 // declare a run-time selection:
53 #define declareRunTimeSelectionTable\
54 (autoPtr,baseType,argNames,argList,parList)                                   \
55                                                                               \
56     /* Construct from argList function pointer type */                        \
57     typedef autoPtr< baseType > (*argNames##ConstructorPtr)argList;           \
58                                                                               \
59     /* Construct from argList function table type */                          \
60     typedef HashTable< argNames##ConstructorPtr, word, string::hash >         \
61         argNames##ConstructorTable;                                           \
62                                                                               \
63     /* Construct from argList function pointer table pointer */               \
64     static argNames##ConstructorTable* argNames##ConstructorTablePtr_;        \
65                                                                               \
66     /* Class to add constructor from argList to table */                      \
67     template< class baseType##Type >                                          \
68     class add##argNames##ConstructorToTable                                   \
69     {                                                                         \
70     public:                                                                   \
71                                                                               \
72         static autoPtr< baseType > New argList                                \
73         {                                                                     \
74             return autoPtr< baseType >(new baseType##Type parList);           \
75         }                                                                     \
76                                                                               \
77         add##argNames##ConstructorToTable                                     \
78         (                                                                     \
79             const word& lookup = baseType##Type::typeName                     \
80         )                                                                     \
81         {                                                                     \
82             construct##argNames##ConstructorTables();                         \
83             argNames##ConstructorTablePtr_->insert(lookup, New);              \
84         }                                                                     \
85                                                                               \
86         ~add##argNames##ConstructorToTable()                                  \
87         {                                                                     \
88             destroy##argNames##ConstructorTables();                           \
89         }                                                                     \
90     };                                                                        \
91                                                                               \
92     /* Table constructor called from the table add function */                \
93     static void construct##argNames##ConstructorTables();                     \
94                                                                               \
95     /* Table destructor called from the table add function destructor */      \
96     static void destroy##argNames##ConstructorTables()
99 // external use:
100 // ~~~~~~~~~~~~~
101 // declare a run-time selection for derived classes:
102 #define declareRunTimeNewSelectionTable\
103 (autoPtr,baseType,argNames,argList,parList)                                   \
104                                                                               \
105     /* Construct from argList function pointer type */                        \
106     typedef autoPtr< baseType > (*argNames##ConstructorPtr)argList;           \
107                                                                               \
108     /* Construct from argList function table type */                          \
109     typedef HashTable< argNames##ConstructorPtr, word, string::hash >         \
110         argNames##ConstructorTable;                                           \
111                                                                               \
112     /* Construct from argList function pointer table pointer */               \
113     static argNames##ConstructorTable* argNames##ConstructorTablePtr_;        \
114                                                                               \
115     /* Class to add constructor from argList to table */                      \
116     template< class baseType##Type >                                          \
117     class add##argNames##ConstructorToTable                                   \
118     {                                                                         \
119     public:                                                                   \
120                                                                               \
121         static autoPtr< baseType > New##baseType argList                      \
122         {                                                                     \
123             return autoPtr< baseType >(baseType##Type::New parList.ptr());    \
124         }                                                                     \
125                                                                               \
126         add##argNames##ConstructorToTable                                     \
127         (                                                                     \
128             const word& lookup = baseType##Type::typeName                     \
129         )                                                                     \
130         {                                                                     \
131             construct##argNames##ConstructorTables();                         \
132             argNames##ConstructorTablePtr_->insert                            \
133             (                                                                 \
134                 lookup,                                                       \
135                 New##baseType                                                 \
136             );                                                                \
137         }                                                                     \
138                                                                               \
139         ~add##argNames##ConstructorToTable()                                  \
140         {                                                                     \
141             destroy##argNames##ConstructorTables();                           \
142         }                                                                     \
143     };                                                                        \
144                                                                               \
145     /* Table constructor called from the table add function */                \
146     static void construct##argNames##ConstructorTables();                     \
147                                                                               \
148     /* Table destructor called from the table add function destructor */      \
149     static void destroy##argNames##ConstructorTables()
152 // internal use:
153 // constructor aid
154 #define defineRunTimeSelectionTableConstructor\
155 (baseType,argNames)                                                           \
156                                                                               \
157     /* Table constructor called from the table add function */                \
158     void baseType::construct##argNames##ConstructorTables()                   \
159     {                                                                         \
160         static bool constructed = false;                                      \
161                                                                               \
162         if (!constructed)                                                     \
163         {                                                                     \
164             baseType::argNames##ConstructorTablePtr_                          \
165                 = new baseType::argNames##ConstructorTable;                   \
166                                                                               \
167             constructed = true;                                               \
168         }                                                                     \
169     }
172 // internal use:
173 // destructor aid
174 #define defineRunTimeSelectionTableDestructor\
175 (baseType,argNames)                                                           \
176                                                                               \
177     /* Table destructor called from the table add function destructor */      \
178     void baseType::destroy##argNames##ConstructorTables()                     \
179     {                                                                         \
180         if (baseType::argNames##ConstructorTablePtr_)                         \
181         {                                                                     \
182             delete baseType::argNames##ConstructorTablePtr_;                  \
183             baseType::argNames##ConstructorTablePtr_ = NULL;                  \
184         }                                                                     \
185     }
188 // internal use:
189 // create pointer to hash-table of functions
190 #define defineRunTimeSelectionTablePtr\
191 (baseType,argNames)                                                           \
192                                                                               \
193     /* Define the constructor function table */                               \
194     baseType::argNames##ConstructorTable*                                     \
195         baseType::argNames##ConstructorTablePtr_ = NULL
198 // not much in use:
199 #define defineTemplateRunTimeSelectionTablePtr(baseType,argNames)             \
200                                                                               \
201     /* Define the constructor function table */                               \
202     typename baseType::argNames##ConstructorTable*                            \
203         baseType::argNames##ConstructorTablePtr_ = NULL
206 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
209 // external use:
210 // ~~~~~~~~~~~~~
211 // define run-time selection table
212 #define defineRunTimeSelectionTable\
213 (baseType,argNames)                                                           \
214                                                                               \
215     defineRunTimeSelectionTablePtr(baseType,argNames);                        \
216     defineRunTimeSelectionTableConstructor(baseType,argNames);                \
217     defineRunTimeSelectionTableDestructor(baseType,argNames)
220 // external use:
221 // ~~~~~~~~~~~~~
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)                                                           \
226                                                                               \
227     template<>                                                                \
228     defineRunTimeSelectionTablePtr(baseType,argNames);                        \
229     template<>                                                                \
230     defineRunTimeSelectionTableConstructor(baseType,argNames);                \
231     template<>                                                                \
232     defineRunTimeSelectionTableDestructor(baseType,argNames)
235 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
238 // internal use:
239 // constructor aid
240 // use when baseType requires the Targ template argument
241 #define defineTemplatedRunTimeSelectionTableConstructor\
242 (baseType,argNames,Targ)                                                      \
243                                                                               \
244     /* Table constructor called from the table add function */                \
245     void baseType< Targ >::construct##argNames##ConstructorTables()           \
246     {                                                                         \
247         static bool constructed = false;                                      \
248                                                                               \
249         if (!constructed)                                                     \
250         {                                                                     \
251             baseType< Targ >::argNames##ConstructorTablePtr_                  \
252                 = new baseType< Targ >::argNames##ConstructorTable;           \
253                                                                               \
254             constructed = true;                                               \
255         }                                                                     \
256     }
259 // internal use:
260 // destructor aid
261 // use when baseType requires the Targ template argument
262 #define defineTemplatedRunTimeSelectionTableDestructor\
263 (baseType,argNames,Targ)                                                      \
264                                                                               \
265     /* Table destructor called from the table add function destructor */      \
266     void baseType< Targ >::destroy##argNames##ConstructorTables()             \
267     {                                                                         \
268         if (baseType< Targ >::argNames##ConstructorTablePtr_)                 \
269         {                                                                     \
270             delete baseType< Targ >::argNames##ConstructorTablePtr_;          \
271             baseType< Targ >::argNames##ConstructorTablePtr_ = NULL;          \
272         }                                                                     \
273     }
276 // internal use:
277 // create pointer to hash-table of functions
278 // use when baseType requires the Targ template argument
279 #define defineTemplatedRunTimeSelectionTablePtr\
280 (baseType,argNames,Targ)                                                      \
281                                                                               \
282     /* Define the constructor function table */                               \
283     baseType< Targ >::argNames##ConstructorTable*                             \
284         baseType< Targ >::argNames##ConstructorTablePtr_ = NULL
287 // external use:
288 // ~~~~~~~~~~~~~
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)                                                      \
293                                                                               \
294     template<>                                                                \
295     defineTemplatedRunTimeSelectionTablePtr(baseType,argNames,Targ);          \
296     template<>                                                                \
297     defineTemplatedRunTimeSelectionTableConstructor(baseType,argNames,Targ);  \
298     template<>                                                                \
299     defineTemplatedRunTimeSelectionTableDestructor(baseType,argNames,Targ)
302 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
304 #endif
306 // ************************************************************************* //