runTimeSelectionTables.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
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 Description
25  Macros to ease declaration of run-time selection tables.
26 
27  declareRunTimeSelectionTable is used to create a run-time selection table
28  for a base-class which holds constructor pointers on the table.
29 
30  declareRunTimeNewSelectionTable is used to create a run-time selection
31  table for a derived-class which holds "New" pointers on the table.
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #include "token.H"
36 
37 #ifndef runTimeSelectionTables_H
38 #define runTimeSelectionTables_H
39 
40 #include "autoPtr.H"
41 #include "HashTable.H"
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 //- Declare a run-time selection
46 #define declareRunTimeSelectionTable(autoPtr,baseType,argNames,argList,parList)\
47  \
48  /* Construct from argList function pointer type */ \
49  typedef autoPtr<baseType> (*argNames##ConstructorPtr)argList; \
50  \
51  /* Construct from argList function table type */ \
52  typedef HashTable<argNames##ConstructorPtr, word, string::hash> \
53  argNames##ConstructorTable; \
54  \
55  /* Construct from argList function pointer table pointer */ \
56  static argNames##ConstructorTable* argNames##ConstructorTablePtr_; \
57  \
58  /* Table constructor called from the table add function */ \
59  static void construct##argNames##ConstructorTables(); \
60  \
61  /* Table destructor called from the table add function destructor */ \
62  static void destroy##argNames##ConstructorTables(); \
63  \
64  /* Class to add constructor from argList to table */ \
65  template<class baseType##Type> \
66  class add##argNames##ConstructorToTable \
67  { \
68  public: \
69  \
70  static autoPtr<baseType> New argList \
71  { \
72  return autoPtr<baseType>(new baseType##Type parList); \
73  } \
74  \
75  add##argNames##ConstructorToTable \
76  ( \
77  const word& lookup = baseType##Type::typeName \
78  ) \
79  { \
80  construct##argNames##ConstructorTables(); \
81  if (!argNames##ConstructorTablePtr_->insert(lookup, New)) \
82  { \
83  std::cerr<< "Duplicate entry " << lookup \
84  << " in runtime selection table " << #baseType \
85  << std::endl; \
86  error::safePrintStack(std::cerr); \
87  } \
88  } \
89  \
90  ~add##argNames##ConstructorToTable() \
91  { \
92  destroy##argNames##ConstructorTables(); \
93  } \
94  }; \
95  \
96  /* Class to add constructor from argList to table */ \
97  /* Remove only the entry (not the table) upon destruction */ \
98  template<class baseType##Type> \
99  class addRemovable##argNames##ConstructorToTable \
100  { \
101  /* retain lookup name for later removal */ \
102  const word& lookup_; \
103  \
104  public: \
105  \
106  static autoPtr<baseType> New argList \
107  { \
108  return autoPtr<baseType>(new baseType##Type parList); \
109  } \
110  \
111  addRemovable##argNames##ConstructorToTable \
112  ( \
113  const word& lookup = baseType##Type::typeName \
114  ) \
115  : \
116  lookup_(lookup) \
117  { \
118  construct##argNames##ConstructorTables(); \
119  argNames##ConstructorTablePtr_->set(lookup, New); \
120  } \
121  \
122  ~addRemovable##argNames##ConstructorToTable() \
123  { \
124  if (argNames##ConstructorTablePtr_) \
125  { \
126  argNames##ConstructorTablePtr_->erase(lookup_); \
127  } \
128  } \
129  };
130 
131 
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  if \
171  ( \
172  !argNames##ConstructorTablePtr_->insert \
173  ( \
174  lookup, \
175  New##baseType \
176  ) \
177  ) \
178  { \
179  std::cerr<< "Duplicate entry " << lookup \
180  << " in runtime selection table " << #baseType \
181  << std::endl; \
182  error::safePrintStack(std::cerr); \
183  } \
184  } \
185  \
186  ~add##argNames##ConstructorToTable() \
187  { \
188  destroy##argNames##ConstructorTables(); \
189  } \
190  }; \
191  \
192  /* Class to add constructor from argList to table */ \
193  template<class baseType##Type> \
194  class addRemovable##argNames##ConstructorToTable \
195  { \
196  /* retain lookup name for later removal */ \
197  const word& lookup_; \
198  \
199  public: \
200  \
201  static autoPtr<baseType> New##baseType argList \
202  { \
203  return autoPtr<baseType>(baseType##Type::New parList.ptr()); \
204  } \
205  \
206  addRemovable##argNames##ConstructorToTable \
207  ( \
208  const word& lookup = baseType##Type::typeName \
209  ) \
210  : \
211  lookup_(lookup) \
212  { \
213  construct##argNames##ConstructorTables(); \
214  argNames##ConstructorTablePtr_->set \
215  ( \
216  lookup, \
217  New##baseType \
218  ); \
219  } \
220  \
221  ~addRemovable##argNames##ConstructorToTable() \
222  { \
223  if (argNames##ConstructorTablePtr_) \
224  { \
225  argNames##ConstructorTablePtr_->erase(lookup_); \
226  } \
227  } \
228  };
229 
230 
231 // Constructor aid
232 #define defineRunTimeSelectionTableConstructor(baseType,argNames) \
233  \
234  /* Table constructor called from the table add function */ \
235  void baseType::construct##argNames##ConstructorTables() \
236  { \
237  static bool constructed = false; \
238  if (!constructed) \
239  { \
240  constructed = true; \
241  baseType::argNames##ConstructorTablePtr_ \
242  = new baseType::argNames##ConstructorTable; \
243  } \
244  }
245 
246 
247 // Destructor aid
248 #define defineRunTimeSelectionTableDestructor(baseType,argNames) \
249  \
250  /* Table destructor called from the table add function destructor */ \
251  void baseType::destroy##argNames##ConstructorTables() \
252  { \
253  if (baseType::argNames##ConstructorTablePtr_) \
254  { \
255  delete baseType::argNames##ConstructorTablePtr_; \
256  baseType::argNames##ConstructorTablePtr_ = nullptr; \
257  } \
258  }
259 
260 
261 // Create pointer to hash-table of functions
262 #define defineRunTimeSelectionTablePtr(baseType,argNames) \
263  \
264  /* Define the constructor function table */ \
265  baseType::argNames##ConstructorTable* \
266  baseType::argNames##ConstructorTablePtr_ = nullptr
267 
268 
269 
270 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
271 
272 //- Define run-time selection table
273 #define defineRunTimeSelectionTable(baseType,argNames) \
274  \
275  defineRunTimeSelectionTablePtr(baseType,argNames); \
276  defineRunTimeSelectionTableConstructor(baseType,argNames); \
277  defineRunTimeSelectionTableDestructor(baseType,argNames)
278 
279 
280 //- Define run-time selection table for template classes
281 // use when baseType doesn't need a template argument (eg, is a typedef)
282 #define defineTemplateRunTimeSelectionTable(baseType,argNames) \
283  \
284  template<> \
285  defineRunTimeSelectionTablePtr(baseType,argNames); \
286  template<> \
287  defineRunTimeSelectionTableConstructor(baseType,argNames); \
288  template<> \
289  defineRunTimeSelectionTableDestructor(baseType,argNames)
290 
291 
292 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
293 
294 // Constructor aid: use when baseType requires the Targ template argument
295 #define defineTemplatedRunTimeSelectionTableConstructor(baseType,argNames,Targ)\
296  \
297  /* Table constructor called from the table add function */ \
298  void baseType<Targ>::construct##argNames##ConstructorTables() \
299  { \
300  static bool constructed = false; \
301  if (!constructed) \
302  { \
303  constructed = true; \
304  baseType<Targ>::argNames##ConstructorTablePtr_ \
305  = new baseType<Targ>::argNames##ConstructorTable; \
306  } \
307  }
308 
309 
310 // Destructor aid: use when baseType requires the Targ template argument
311 #define defineTemplatedRunTimeSelectionTableDestructor(baseType,argNames,Targ) \
312  \
313  /* Table destructor called from the table add function destructor */ \
314  void baseType<Targ>::destroy##argNames##ConstructorTables() \
315  { \
316  if (baseType<Targ>::argNames##ConstructorTablePtr_) \
317  { \
318  delete baseType<Targ>::argNames##ConstructorTablePtr_; \
319  baseType<Targ>::argNames##ConstructorTablePtr_ = nullptr; \
320  } \
321  }
322 
323 
324 //- Create pointer to hash-table of functions
325 // use when baseType requires the Targ template argument
326 #define defineTemplatedRunTimeSelectionTablePtr(baseType,argNames,Targ) \
327  \
328  /* Define the constructor function table */ \
329  baseType<Targ>::argNames##ConstructorTable* \
330  baseType<Targ>::argNames##ConstructorTablePtr_ = nullptr
331 
332 
333 //- Define run-time selection table for template classes
334 // use when baseType requires the Targ template argument
335 #define defineTemplatedRunTimeSelectionTable(baseType,argNames,Targ) \
336  \
337  template<> \
338  defineTemplatedRunTimeSelectionTablePtr(baseType,argNames,Targ); \
339  template<> \
340  defineTemplatedRunTimeSelectionTableConstructor(baseType,argNames,Targ); \
341  template<> \
342  defineTemplatedRunTimeSelectionTableDestructor(baseType,argNames,Targ)
343 
344 
345 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
346 
347 #endif
348 
349 // ************************************************************************* //