Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / src / foam / db / runTimeSelection / runTimeSelectionTables.H
blob2189bda67e3323987614e65099d425cfd8fda1d7
1 /*---------------------------------------------------------------------------*\
2   =========                 |
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 -------------------------------------------------------------------------------
8 License
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/>.
24 Class
25     Foam::runTimeSelectionTables
27 Description
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 \*---------------------------------------------------------------------------*/
38 #include "token.H"
40 #ifndef runTimeSelectionTables_H
41 #define runTimeSelectionTables_H
43 #include "autoPtr.H"
44 #include "HashTable.H"
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 // external use:
50 // ~~~~~~~~~~~~~
51 // declare a run-time selection:
52 #define declareRunTimeSelectionTable\
53 (autoPtr,baseType,argNames,argList,parList)                                   \
54                                                                               \
55     /* Construct from argList function pointer type */                        \
56     typedef autoPtr< baseType > (*argNames##ConstructorPtr)argList;           \
57                                                                               \
58     /* Construct from argList function table type */                          \
59     typedef HashTable< argNames##ConstructorPtr, word, string::hash >         \
60         argNames##ConstructorTable;                                           \
61                                                                               \
62     /* Construct from argList function pointer table pointer */               \
63     static argNames##ConstructorTable* argNames##ConstructorTablePtr_;        \
64                                                                               \
65     /* Class to add constructor from argList to table */                      \
66     template< class baseType##Type >                                          \
67     class add##argNames##ConstructorToTable                                   \
68     {                                                                         \
69     public:                                                                   \
70                                                                               \
71         static autoPtr< baseType > New argList                                \
72         {                                                                     \
73             return autoPtr< baseType >(new baseType##Type parList);           \
74         }                                                                     \
75                                                                               \
76         add##argNames##ConstructorToTable                                     \
77         (                                                                     \
78             const word& lookup = baseType##Type::typeName                     \
79         )                                                                     \
80         {                                                                     \
81             construct##argNames##ConstructorTables();                         \
82             argNames##ConstructorTablePtr_->insert(lookup, New);              \
83         }                                                                     \
84                                                                               \
85         ~add##argNames##ConstructorToTable()                                  \
86         {                                                                     \
87             destroy##argNames##ConstructorTables();                           \
88         }                                                                     \
89     };                                                                        \
90                                                                               \
91     /* Table constructor called from the table add function */                \
92     static void construct##argNames##ConstructorTables();                     \
93                                                                               \
94     /* Table destructor called from the table add function destructor */      \
95     static void destroy##argNames##ConstructorTables()
98 // external use:
99 // ~~~~~~~~~~~~~
100 // declare a run-time selection for derived classes:
101 #define declareRunTimeNewSelectionTable\
102 (autoPtr,baseType,argNames,argList,parList)                                   \
103                                                                               \
104     /* Construct from argList function pointer type */                        \
105     typedef autoPtr< baseType > (*argNames##ConstructorPtr)argList;           \
106                                                                               \
107     /* Construct from argList function table type */                          \
108     typedef HashTable< argNames##ConstructorPtr, word, string::hash >         \
109         argNames##ConstructorTable;                                           \
110                                                                               \
111     /* Construct from argList function pointer table pointer */               \
112     static argNames##ConstructorTable* argNames##ConstructorTablePtr_;        \
113                                                                               \
114     /* Class to add constructor from argList to table */                      \
115     template< class baseType##Type >                                          \
116     class add##argNames##ConstructorToTable                                   \
117     {                                                                         \
118     public:                                                                   \
119                                                                               \
120         static autoPtr< baseType > New##baseType argList                      \
121         {                                                                     \
122             return autoPtr< baseType >(baseType##Type::New parList.ptr());    \
123         }                                                                     \
124                                                                               \
125         add##argNames##ConstructorToTable                                     \
126         (                                                                     \
127             const word& lookup = baseType##Type::typeName                     \
128         )                                                                     \
129         {                                                                     \
130             construct##argNames##ConstructorTables();                         \
131             argNames##ConstructorTablePtr_->insert                            \
132             (                                                                 \
133                 lookup,                                                       \
134                 New##baseType                                                 \
135             );                                                                \
136         }                                                                     \
137                                                                               \
138         ~add##argNames##ConstructorToTable()                                  \
139         {                                                                     \
140             destroy##argNames##ConstructorTables();                           \
141         }                                                                     \
142     };                                                                        \
143                                                                               \
144     /* Table constructor called from the table add function */                \
145     static void construct##argNames##ConstructorTables();                     \
146                                                                               \
147     /* Table destructor called from the table add function destructor */      \
148     static void destroy##argNames##ConstructorTables()
151 // internal use:
152 // constructor aid
153 #define defineRunTimeSelectionTableConstructor\
154 (baseType,argNames)                                                           \
155                                                                               \
156     /* Table constructor called from the table add function */                \
157     void baseType::construct##argNames##ConstructorTables()                   \
158     {                                                                         \
159         static bool constructed = false;                                      \
160                                                                               \
161         if (!constructed)                                                     \
162         {                                                                     \
163             baseType::argNames##ConstructorTablePtr_                          \
164                 = new baseType::argNames##ConstructorTable;                   \
165                                                                               \
166             constructed = true;                                               \
167         }                                                                     \
168     }
171 // internal use:
172 // destructor aid
173 #define defineRunTimeSelectionTableDestructor\
174 (baseType,argNames)                                                           \
175                                                                               \
176     /* Table destructor called from the table add function destructor */      \
177     void baseType::destroy##argNames##ConstructorTables()                     \
178     {                                                                         \
179         if (baseType::argNames##ConstructorTablePtr_)                         \
180         {                                                                     \
181             delete baseType::argNames##ConstructorTablePtr_;                  \
182             baseType::argNames##ConstructorTablePtr_ = NULL;                  \
183         }                                                                     \
184     }
187 // internal use:
188 // create pointer to hash-table of functions
189 #define defineRunTimeSelectionTablePtr\
190 (baseType,argNames)                                                           \
191                                                                               \
192     /* Define the constructor function table */                               \
193     baseType::argNames##ConstructorTable*                                     \
194         baseType::argNames##ConstructorTablePtr_ = NULL
197 // not much in use:
198 #define defineTemplateRunTimeSelectionTablePtr(baseType,argNames)             \
199                                                                               \
200     /* Define the constructor function table */                               \
201     typename baseType::argNames##ConstructorTable*                            \
202         baseType::argNames##ConstructorTablePtr_ = NULL
205 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
208 // external use:
209 // ~~~~~~~~~~~~~
210 // define run-time selection table
211 #define defineRunTimeSelectionTable\
212 (baseType,argNames)                                                           \
213                                                                               \
214     defineRunTimeSelectionTablePtr(baseType,argNames);                        \
215     defineRunTimeSelectionTableConstructor(baseType,argNames);                \
216     defineRunTimeSelectionTableDestructor(baseType,argNames)
219 // external use:
220 // ~~~~~~~~~~~~~
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)                                                           \
225                                                                               \
226     template<>                                                                \
227     defineRunTimeSelectionTablePtr(baseType,argNames);                        \
228     template<>                                                                \
229     defineRunTimeSelectionTableConstructor(baseType,argNames);                \
230     template<>                                                                \
231     defineRunTimeSelectionTableDestructor(baseType,argNames)
234 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
237 // internal use:
238 // constructor aid
239 // use when baseType requires the Targ template argument
240 #define defineTemplatedRunTimeSelectionTableConstructor\
241 (baseType,argNames,Targ)                                                      \
242                                                                               \
243     /* Table constructor called from the table add function */                \
244     void baseType< Targ >::construct##argNames##ConstructorTables()           \
245     {                                                                         \
246         static bool constructed = false;                                      \
247                                                                               \
248         if (!constructed)                                                     \
249         {                                                                     \
250             baseType< Targ >::argNames##ConstructorTablePtr_                  \
251                 = new baseType< Targ >::argNames##ConstructorTable;           \
252                                                                               \
253             constructed = true;                                               \
254         }                                                                     \
255     }
258 // internal use:
259 // destructor aid
260 // use when baseType requires the Targ template argument
261 #define defineTemplatedRunTimeSelectionTableDestructor\
262 (baseType,argNames,Targ)                                                      \
263                                                                               \
264     /* Table destructor called from the table add function destructor */      \
265     void baseType< Targ >::destroy##argNames##ConstructorTables()             \
266     {                                                                         \
267         if (baseType< Targ >::argNames##ConstructorTablePtr_)                 \
268         {                                                                     \
269             delete baseType< Targ >::argNames##ConstructorTablePtr_;          \
270             baseType< Targ >::argNames##ConstructorTablePtr_ = NULL;          \
271         }                                                                     \
272     }
275 // internal use:
276 // create pointer to hash-table of functions
277 // use when baseType requires the Targ template argument
278 #define defineTemplatedRunTimeSelectionTablePtr\
279 (baseType,argNames,Targ)                                                      \
280                                                                               \
281     /* Define the constructor function table */                               \
282     baseType< Targ >::argNames##ConstructorTable*                             \
283         baseType< Targ >::argNames##ConstructorTablePtr_ = NULL
286 // external use:
287 // ~~~~~~~~~~~~~
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)                                                      \
292                                                                               \
293     template<>                                                                \
294     defineTemplatedRunTimeSelectionTablePtr(baseType,argNames,Targ);          \
295     template<>                                                                \
296     defineTemplatedRunTimeSelectionTableConstructor(baseType,argNames,Targ);  \
297     template<>                                                                \
298     defineTemplatedRunTimeSelectionTableDestructor(baseType,argNames,Targ)
301 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
303 #endif
305 // ************************************************************************* //