FreeFOAM The Cross-Platform CFD Toolkit
runTimeSelectionTables.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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.
20 
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/>.
23 
24 Class
25  Foam::runTimeSelectionTables
26 
27 Description
28  Macros to enable the easy declaration of run-time selection tables.
29 
30  declareRunTimeSelectionTable is used to create a run-time selection table
31  for a base-class which holds constructor pointers on the table.
32 
33  declareRunTimeNewSelectionTable is used to create a run-time selection
34  table for a derived-class which holds "New" pointers on the table.
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #include <OpenFOAM/token.H>
39 
40 #ifndef runTimeSelectionTables_H
41 #define runTimeSelectionTables_H
42 
43 #include <OpenFOAM/autoPtr.H>
44 #include <OpenFOAM/HashTable.H>
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 
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()
96 
97 
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()
149 
150 
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  }
169 
170 
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  }
185 
186 
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
195 
196 
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
203 
204 
205 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
206 
207 
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)
217 
218 
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)
232 
233 
234 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
235 
236 
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  }
256 
257 
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  }
273 
274 
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
284 
285 
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)
299 
300 
301 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
302 
303 #endif
304 
305 // ************************ vim: set sw=4 sts=4 et: ************************ //