BUG: UListIO: byteSize overflowing on really big faceLists
[OpenFOAM-2.0.x.git] / src / OpenFOAM / db / runTimeSelection / construction / runTimeSelectionTables.H
blob45766611377599251076162687d18d586fb08e7e
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
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
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
19     for more details.
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/>.
24 Description
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 \*---------------------------------------------------------------------------*/
35 #include "token.H"
37 #ifndef runTimeSelectionTables_H
38 #define runTimeSelectionTables_H
40 #include "autoPtr.H"
41 #include "HashTable.H"
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 // external use:
47 // ~~~~~~~~~~~~~
48 // declare a run-time selection:
49 #define declareRunTimeSelectionTable\
50 (autoPtr,baseType,argNames,argList,parList)                                   \
51                                                                               \
52     /* Construct from argList function pointer type */                        \
53     typedef autoPtr< baseType > (*argNames##ConstructorPtr)argList;           \
54                                                                               \
55     /* Construct from argList function table type */                          \
56     typedef HashTable< argNames##ConstructorPtr, word, string::hash >         \
57         argNames##ConstructorTable;                                           \
58                                                                               \
59     /* Construct from argList function pointer table pointer */               \
60     static argNames##ConstructorTable* argNames##ConstructorTablePtr_;        \
61                                                                               \
62     /* Table constructor called from the table add function */                \
63     static void construct##argNames##ConstructorTables();                     \
64                                                                               \
65     /* Table destructor called from the table add function destructor */      \
66     static void destroy##argNames##ConstructorTables();                       \
67                                                                               \
68     /* Class to add constructor from argList to table */                      \
69     template< class baseType##Type >                                          \
70     class add##argNames##ConstructorToTable                                   \
71     {                                                                         \
72     public:                                                                   \
73                                                                               \
74         static autoPtr< baseType > New argList                                \
75         {                                                                     \
76             return autoPtr< baseType >(new baseType##Type parList);           \
77         }                                                                     \
78                                                                               \
79         add##argNames##ConstructorToTable                                     \
80         (                                                                     \
81             const word& lookup = baseType##Type::typeName                     \
82         )                                                                     \
83         {                                                                     \
84             construct##argNames##ConstructorTables();                         \
85             argNames##ConstructorTablePtr_->insert(lookup, New);              \
86         }                                                                     \
87                                                                               \
88         ~add##argNames##ConstructorToTable()                                  \
89         {                                                                     \
90             destroy##argNames##ConstructorTables();                           \
91         }                                                                     \
92     };                                                                        \
93                                                                               \
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                          \
98     {                                                                         \
99         /* retain lookup name for later removal */                            \
100         const word& lookup_;                                                  \
101                                                                               \
102     public:                                                                   \
103                                                                               \
104         static autoPtr< baseType > New argList                                \
105         {                                                                     \
106             return autoPtr< baseType >(new baseType##Type parList);           \
107         }                                                                     \
108                                                                               \
109         addRemovable##argNames##ConstructorToTable                            \
110         (                                                                     \
111             const word& lookup = baseType##Type::typeName                     \
112         )                                                                     \
113         :                                                                     \
114             lookup_(lookup)                                                   \
115         {                                                                     \
116             construct##argNames##ConstructorTables();                         \
117             argNames##ConstructorTablePtr_->set(lookup, New);                 \
118         }                                                                     \
119                                                                               \
120         ~addRemovable##argNames##ConstructorToTable()                         \
121         {                                                                     \
122             if (argNames##ConstructorTablePtr_)                               \
123             {                                                                 \
124                 argNames##ConstructorTablePtr_->erase(lookup_);               \
125             }                                                                 \
126         }                                                                     \
127     };
131 // external use:
132 // ~~~~~~~~~~~~~
133 // declare a run-time selection for derived classes:
134 #define declareRunTimeNewSelectionTable\
135 (autoPtr,baseType,argNames,argList,parList)                                   \
136                                                                               \
137     /* Construct from argList function pointer type */                        \
138     typedef autoPtr< baseType > (*argNames##ConstructorPtr)argList;           \
139                                                                               \
140     /* Construct from argList function table type */                          \
141     typedef HashTable< argNames##ConstructorPtr, word, string::hash >         \
142         argNames##ConstructorTable;                                           \
143                                                                               \
144     /* Construct from argList function pointer table pointer */               \
145     static argNames##ConstructorTable* argNames##ConstructorTablePtr_;        \
146                                                                               \
147     /* Table constructor called from the table add function */                \
148     static void construct##argNames##ConstructorTables();                     \
149                                                                               \
150     /* Table destructor called from the table add function destructor */      \
151     static void destroy##argNames##ConstructorTables();                       \
152                                                                               \
153     /* Class to add constructor from argList to table */                      \
154     template< class baseType##Type >                                          \
155     class add##argNames##ConstructorToTable                                   \
156     {                                                                         \
157     public:                                                                   \
158                                                                               \
159         static autoPtr< baseType > New##baseType argList                      \
160         {                                                                     \
161             return autoPtr< baseType >(baseType##Type::New parList.ptr());    \
162         }                                                                     \
163                                                                               \
164         add##argNames##ConstructorToTable                                     \
165         (                                                                     \
166             const word& lookup = baseType##Type::typeName                     \
167         )                                                                     \
168         {                                                                     \
169             construct##argNames##ConstructorTables();                         \
170             argNames##ConstructorTablePtr_->insert                            \
171             (                                                                 \
172                 lookup,                                                       \
173                 New##baseType                                                 \
174             );                                                                \
175         }                                                                     \
176                                                                               \
177         ~add##argNames##ConstructorToTable()                                  \
178         {                                                                     \
179             destroy##argNames##ConstructorTables();                           \
180         }                                                                     \
181     };                                                                        \
182                                                                               \
183     /* Class to add constructor from argList to table */                      \
184     template< class baseType##Type >                                          \
185     class addRemovable##argNames##ConstructorToTable                          \
186     {                                                                         \
187         /* retain lookup name for later removal */                            \
188         const word& lookup_;                                                  \
189                                                                               \
190     public:                                                                   \
191                                                                               \
192         static autoPtr< baseType > New##baseType argList                      \
193         {                                                                     \
194             return autoPtr< baseType >(baseType##Type::New parList.ptr());    \
195         }                                                                     \
196                                                                               \
197         addRemovable##argNames##ConstructorToTable                            \
198         (                                                                     \
199             const word& lookup = baseType##Type::typeName                     \
200         )                                                                     \
201         :                                                                     \
202             lookup_(lookup)                                                   \
203         {                                                                     \
204             construct##argNames##ConstructorTables();                         \
205             argNames##ConstructorTablePtr_->set                               \
206             (                                                                 \
207                 lookup,                                                       \
208                 New##baseType                                                 \
209             );                                                                \
210         }                                                                     \
211                                                                               \
212         ~addRemovable##argNames##ConstructorToTable()                         \
213         {                                                                     \
214             if (argNames##ConstructorTablePtr_)                               \
215             {                                                                 \
216                 argNames##ConstructorTablePtr_->erase(lookup_);               \
217             }                                                                 \
218         }                                                                     \
219     };
222 // internal use:
223 // constructor aid
224 #define defineRunTimeSelectionTableConstructor\
225 (baseType,argNames)                                                           \
226                                                                               \
227     /* Table constructor called from the table add function */                \
228     void baseType::construct##argNames##ConstructorTables()                   \
229     {                                                                         \
230         static bool constructed = false;                                      \
231         if (!constructed)                                                     \
232         {                                                                     \
233             constructed = true;                                               \
234             baseType::argNames##ConstructorTablePtr_                          \
235                 = new baseType::argNames##ConstructorTable;                   \
236         }                                                                     \
237     }
240 // internal use:
241 // destructor aid
242 #define defineRunTimeSelectionTableDestructor\
243 (baseType,argNames)                                                           \
244                                                                               \
245     /* Table destructor called from the table add function destructor */      \
246     void baseType::destroy##argNames##ConstructorTables()                     \
247     {                                                                         \
248         if (baseType::argNames##ConstructorTablePtr_)                         \
249         {                                                                     \
250             delete baseType::argNames##ConstructorTablePtr_;                  \
251             baseType::argNames##ConstructorTablePtr_ = NULL;                  \
252         }                                                                     \
253     }
256 // internal use:
257 // create pointer to hash-table of functions
258 #define defineRunTimeSelectionTablePtr\
259 (baseType,argNames)                                                           \
260                                                                               \
261     /* Define the constructor function table */                               \
262     baseType::argNames##ConstructorTable*                                     \
263         baseType::argNames##ConstructorTablePtr_ = NULL
266 // not much in use:
267 #define defineTemplateRunTimeSelectionTablePtr(baseType,argNames)             \
268                                                                               \
269     /* Define the constructor function table */                               \
270     typename baseType::argNames##ConstructorTable*                            \
271         baseType::argNames##ConstructorTablePtr_ = NULL
274 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
277 // external use:
278 // ~~~~~~~~~~~~~
279 // define run-time selection table
280 #define defineRunTimeSelectionTable\
281 (baseType,argNames)                                                           \
282                                                                               \
283     defineRunTimeSelectionTablePtr(baseType,argNames);                        \
284     defineRunTimeSelectionTableConstructor(baseType,argNames);                \
285     defineRunTimeSelectionTableDestructor(baseType,argNames)
288 // external use:
289 // ~~~~~~~~~~~~~
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)                                                           \
294                                                                               \
295     template<>                                                                \
296     defineRunTimeSelectionTablePtr(baseType,argNames);                        \
297     template<>                                                                \
298     defineRunTimeSelectionTableConstructor(baseType,argNames);                \
299     template<>                                                                \
300     defineRunTimeSelectionTableDestructor(baseType,argNames)
303 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
306 // internal use:
307 // constructor aid
308 // use when baseType requires the Targ template argument
309 #define defineTemplatedRunTimeSelectionTableConstructor\
310 (baseType,argNames,Targ)                                                      \
311                                                                               \
312     /* Table constructor called from the table add function */                \
313     void baseType< Targ >::construct##argNames##ConstructorTables()           \
314     {                                                                         \
315         static bool constructed = false;                                      \
316         if (!constructed)                                                     \
317         {                                                                     \
318             constructed = true;                                               \
319             baseType< Targ >::argNames##ConstructorTablePtr_                  \
320                 = new baseType< Targ >::argNames##ConstructorTable;           \
321         }                                                                     \
322     }
325 // internal use:
326 // destructor aid
327 // use when baseType requires the Targ template argument
328 #define defineTemplatedRunTimeSelectionTableDestructor\
329 (baseType,argNames,Targ)                                                      \
330                                                                               \
331     /* Table destructor called from the table add function destructor */      \
332     void baseType< Targ >::destroy##argNames##ConstructorTables()             \
333     {                                                                         \
334         if (baseType< Targ >::argNames##ConstructorTablePtr_)                 \
335         {                                                                     \
336             delete baseType< Targ >::argNames##ConstructorTablePtr_;          \
337             baseType< Targ >::argNames##ConstructorTablePtr_ = NULL;          \
338         }                                                                     \
339     }
342 // internal use:
343 // create pointer to hash-table of functions
344 // use when baseType requires the Targ template argument
345 #define defineTemplatedRunTimeSelectionTablePtr\
346 (baseType,argNames,Targ)                                                      \
347                                                                               \
348     /* Define the constructor function table */                               \
349     baseType< Targ >::argNames##ConstructorTable*                             \
350         baseType< Targ >::argNames##ConstructorTablePtr_ = NULL
353 // external use:
354 // ~~~~~~~~~~~~~
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)                                                      \
359                                                                               \
360     template<>                                                                \
361     defineTemplatedRunTimeSelectionTablePtr(baseType,argNames,Targ);          \
362     template<>                                                                \
363     defineTemplatedRunTimeSelectionTableConstructor(baseType,argNames,Targ);  \
364     template<>                                                                \
365     defineTemplatedRunTimeSelectionTableDestructor(baseType,argNames,Targ)
368 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
370 #endif
372 // ************************************************************************* //