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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Description
28  Macros to ease 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 "token.H"
39 
40 #ifndef runTimeSelectionTables_H
41 #define runTimeSelectionTables_H
42 
43 #include "autoPtr.H"
44 #include "HashTable.H"
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 //- Declare a run-time selection
49 #define declareRunTimeSelectionTable(autoPtr,baseType,argNames,argList,parList)\
50  \
51  /* Construct from argList function pointer type */ \
52  typedef autoPtr<baseType> (*argNames##ConstructorPtr)argList; \
53  \
54  /* Construct from argList function table type */ \
55  typedef HashTable<argNames##ConstructorPtr, word, string::hash> \
56  argNames##ConstructorTable; \
57  \
58  /* Construct from argList function pointer table pointer */ \
59  static argNames##ConstructorTable* argNames##ConstructorTablePtr_; \
60  \
61  /* Table constructor called from the table add function */ \
62  static void construct##argNames##ConstructorTables(); \
63  \
64  /* Table destructor called from the table add function destructor */ \
65  static void destroy##argNames##ConstructorTables(); \
66  \
67  /* Class to add constructor from argList to table */ \
68  template<class baseType##Type> \
69  class add##argNames##ConstructorToTable \
70  { \
71  public: \
72  \
73  static autoPtr<baseType> New argList \
74  { \
75  return autoPtr<baseType>(new baseType##Type parList); \
76  } \
77  \
78  explicit add##argNames##ConstructorToTable \
79  ( \
80  const word& lookup = baseType##Type::typeName \
81  ) \
82  { \
83  construct##argNames##ConstructorTables(); \
84  if (!argNames##ConstructorTablePtr_->insert(lookup, New)) \
85  { \
86  std::cerr<< "Duplicate entry " << lookup \
87  << " in runtime selection table " << #baseType \
88  << std::endl; \
89  error::safePrintStack(std::cerr); \
90  } \
91  } \
92  \
93  ~add##argNames##ConstructorToTable() \
94  { \
95  destroy##argNames##ConstructorTables(); \
96  } \
97  \
98  add##argNames##ConstructorToTable \
99  (const add##argNames##ConstructorToTable&) = delete; \
100  \
101  void operator= \
102  (const add##argNames##ConstructorToTable&) = delete; \
103  }; \
104  \
105  /* Class to add constructor from argList to table */ \
106  /* Remove only the entry (not the table) upon destruction */ \
107  template<class baseType##Type> \
108  class addRemovable##argNames##ConstructorToTable \
109  { \
110  public: \
111  \
112  const word name; /* Lookup name for later removal */ \
113  \
114  static autoPtr<baseType> New argList \
115  { \
116  return autoPtr<baseType>(new baseType##Type parList); \
117  } \
118  \
119  explicit addRemovable##argNames##ConstructorToTable \
120  ( \
121  const word& lookup = baseType##Type::typeName \
122  ) \
123  : \
124  name(lookup) \
125  { \
126  construct##argNames##ConstructorTables(); \
127  argNames##ConstructorTablePtr_->set(lookup, New); \
128  } \
129  \
130  ~addRemovable##argNames##ConstructorToTable() \
131  { \
132  if (argNames##ConstructorTablePtr_) \
133  { \
134  argNames##ConstructorTablePtr_->erase(name); \
135  } \
136  } \
137  \
138  addRemovable##argNames##ConstructorToTable \
139  (const addRemovable##argNames##ConstructorToTable&) = delete; \
140  \
141  void operator= \
142  (const addRemovable##argNames##ConstructorToTable&) = delete; \
143  };
144 
145 
146 
147 //- Declare a run-time selection for derived classes
148 #define declareRunTimeNewSelectionTable( \
149  autoPtr,baseType,argNames,argList,parList) \
150  \
151  /* Construct from argList function pointer type */ \
152  typedef autoPtr<baseType> (*argNames##ConstructorPtr)argList; \
153  \
154  /* Construct from argList function table type */ \
155  typedef HashTable<argNames##ConstructorPtr, word, string::hash> \
156  argNames##ConstructorTable; \
157  \
158  /* Construct from argList function pointer table pointer */ \
159  static argNames##ConstructorTable* argNames##ConstructorTablePtr_; \
160  \
161  /* Table constructor called from the table add function */ \
162  static void construct##argNames##ConstructorTables(); \
163  \
164  /* Table destructor called from the table add function destructor */ \
165  static void destroy##argNames##ConstructorTables(); \
166  \
167  /* Class to add constructor from argList to table */ \
168  template<class baseType##Type> \
169  class add##argNames##ConstructorToTable \
170  { \
171  public: \
172  \
173  static autoPtr<baseType> New##baseType argList \
174  { \
175  return autoPtr<baseType>(baseType##Type::New parList.ptr()); \
176  } \
177  \
178  explicit add##argNames##ConstructorToTable \
179  ( \
180  const word& lookup = baseType##Type::typeName \
181  ) \
182  { \
183  construct##argNames##ConstructorTables(); \
184  if \
185  ( \
186  !argNames##ConstructorTablePtr_->insert \
187  ( \
188  lookup, \
189  New##baseType \
190  ) \
191  ) \
192  { \
193  std::cerr<< "Duplicate entry " << lookup \
194  << " in runtime selection table " << #baseType \
195  << std::endl; \
196  error::safePrintStack(std::cerr); \
197  } \
198  } \
199  \
200  ~add##argNames##ConstructorToTable() \
201  { \
202  destroy##argNames##ConstructorTables(); \
203  } \
204  \
205  add##argNames##ConstructorToTable \
206  (const add##argNames##ConstructorToTable&) = delete; \
207  \
208  void operator= \
209  (const add##argNames##ConstructorToTable&) = delete; \
210  }; \
211  \
212  /* Class to add constructor from argList to table */ \
213  template<class baseType##Type> \
214  class addRemovable##argNames##ConstructorToTable \
215  { \
216  public: \
217  \
218  const word name; /* Lookup name for later removal */ \
219  \
220  static autoPtr<baseType> New##baseType argList \
221  { \
222  return autoPtr<baseType>(baseType##Type::New parList.ptr()); \
223  } \
224  \
225  explicit addRemovable##argNames##ConstructorToTable \
226  ( \
227  const word& lookup = baseType##Type::typeName \
228  ) \
229  : \
230  name(lookup) \
231  { \
232  construct##argNames##ConstructorTables(); \
233  argNames##ConstructorTablePtr_->set \
234  ( \
235  lookup, \
236  New##baseType \
237  ); \
238  } \
239  \
240  ~addRemovable##argNames##ConstructorToTable() \
241  { \
242  if (argNames##ConstructorTablePtr_) \
243  { \
244  argNames##ConstructorTablePtr_->erase(name); \
245  } \
246  } \
247  \
248  addRemovable##argNames##ConstructorToTable \
249  (const addRemovable##argNames##ConstructorToTable&) = delete; \
250  \
251  void operator= \
252  (const addRemovable##argNames##ConstructorToTable&) = delete; \
253  };
254 
255 
256 // Constructor aid
257 #define defineRunTimeSelectionTableConstructor(baseType,argNames) \
258  \
259  /* Table constructor called from the table add function */ \
260  void baseType::construct##argNames##ConstructorTables() \
261  { \
262  static bool constructed = false; \
263  if (!constructed) \
264  { \
265  constructed = true; \
266  baseType::argNames##ConstructorTablePtr_ \
267  = new baseType::argNames##ConstructorTable; \
268  } \
269  }
270 
271 
272 // Destructor aid
273 #define defineRunTimeSelectionTableDestructor(baseType,argNames) \
274  \
275  /* Table destructor called from the table add function destructor */ \
276  void baseType::destroy##argNames##ConstructorTables() \
277  { \
278  if (baseType::argNames##ConstructorTablePtr_) \
279  { \
280  delete baseType::argNames##ConstructorTablePtr_; \
281  baseType::argNames##ConstructorTablePtr_ = nullptr; \
282  } \
283  }
284 
285 
286 // Create pointer to hash-table of functions
287 #define defineRunTimeSelectionTablePtr(baseType,argNames) \
288  \
289  /* Define the constructor function table */ \
290  baseType::argNames##ConstructorTable* \
291  baseType::argNames##ConstructorTablePtr_(nullptr)
292 
293 
294 
295 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
296 
297 //- Define run-time selection table
298 #define defineRunTimeSelectionTable(baseType,argNames) \
299  \
300  defineRunTimeSelectionTablePtr(baseType,argNames); \
301  defineRunTimeSelectionTableConstructor(baseType,argNames); \
302  defineRunTimeSelectionTableDestructor(baseType,argNames)
303 
304 
305 //- Define run-time selection table for template classes
306 // use when baseType doesn't need a template argument (eg, is a typedef)
307 #define defineTemplateRunTimeSelectionTable(baseType,argNames) \
308  \
309  template<> \
310  defineRunTimeSelectionTablePtr(baseType,argNames); \
311  template<> \
312  defineRunTimeSelectionTableConstructor(baseType,argNames); \
313  template<> \
314  defineRunTimeSelectionTableDestructor(baseType,argNames)
315 
316 
317 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
318 
319 // Constructor aid: use when baseType requires the Targ template argument
320 #define defineTemplatedRunTimeSelectionTableConstructor(baseType,argNames,Targ)\
321  \
322  /* Table constructor called from the table add function */ \
323  void baseType<Targ>::construct##argNames##ConstructorTables() \
324  { \
325  static bool constructed = false; \
326  if (!constructed) \
327  { \
328  constructed = true; \
329  baseType<Targ>::argNames##ConstructorTablePtr_ \
330  = new baseType<Targ>::argNames##ConstructorTable; \
331  } \
332  }
333 
334 
335 // Destructor aid: use when baseType requires the Targ template argument
336 #define defineTemplatedRunTimeSelectionTableDestructor(baseType,argNames,Targ) \
337  \
338  /* Table destructor called from the table add function destructor */ \
339  void baseType<Targ>::destroy##argNames##ConstructorTables() \
340  { \
341  if (baseType<Targ>::argNames##ConstructorTablePtr_) \
342  { \
343  delete baseType<Targ>::argNames##ConstructorTablePtr_; \
344  baseType<Targ>::argNames##ConstructorTablePtr_ = nullptr; \
345  } \
346  }
347 
348 
349 //- Create pointer to hash-table of functions
350 // use when baseType requires the Targ template argument
351 #define defineTemplatedRunTimeSelectionTablePtr(baseType,argNames,Targ) \
352  \
353  /* Define the constructor function table */ \
354  baseType<Targ>::argNames##ConstructorTable* \
355  baseType<Targ>::argNames##ConstructorTablePtr_(nullptr)
356 
357 
358 //- Define run-time selection table for template classes
359 // use when baseType requires the Targ template argument
360 #define defineTemplatedRunTimeSelectionTable(baseType,argNames,Targ) \
361  \
362  template<> \
363  defineTemplatedRunTimeSelectionTablePtr(baseType,argNames,Targ); \
364  template<> \
365  defineTemplatedRunTimeSelectionTableConstructor(baseType,argNames,Targ); \
366  template<> \
367  defineTemplatedRunTimeSelectionTableDestructor(baseType,argNames,Targ)
368 
369 
370 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
371 
372 #endif
373 
374 // ************************************************************************* //
token.H
HashTable.H
defineRunTimeSelectionTableDestructor
#define defineRunTimeSelectionTableDestructor(baseType, argNames)
Definition: runTimeSelectionTables.H:273
defineTemplateRunTimeSelectionTable
#define defineTemplateRunTimeSelectionTable(baseType, argNames)
Define run-time selection table for template classes.
Definition: runTimeSelectionTables.H:307
defineRunTimeSelectionTableConstructor
#define defineRunTimeSelectionTableConstructor(baseType, argNames)
Definition: runTimeSelectionTables.H:257
defineRunTimeSelectionTablePtr
#define defineRunTimeSelectionTablePtr(baseType, argNames)
Definition: runTimeSelectionTables.H:287
defineTemplatedRunTimeSelectionTablePtr
#define defineTemplatedRunTimeSelectionTablePtr(baseType, argNames, Targ)
Create pointer to hash-table of functions.
Definition: runTimeSelectionTables.H:351
autoPtr.H