memberFunctionSelectionTables.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 enable the easy declaration of member function selection tables.
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #include "token.H"
33 
34 #ifndef memberFunctionSelectionTables_H
35 #define memberFunctionSelectionTables_H
36 
37 #include "HashTable.H"
38 
39 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
40 
41 //- Declare a run-time selection:
42 #define declareMemberFunctionSelectionTable( \
43  returnType,baseType,memberFunction,argNames,argList,parList) \
44  \
45  /* Construct from argList function pointer type */ \
46  typedef returnType (*memberFunction##argNames##MemberFunctionPtr)argList; \
47  \
48  /* Construct from argList function table type */ \
49  typedef HashTable \
50  <memberFunction##argNames##MemberFunctionPtr, word, string::hash> \
51  memberFunction##argNames##MemberFunctionTable; \
52  \
53  /* Construct from argList function pointer table pointer */ \
54  static memberFunction##argNames##MemberFunctionTable* \
55  memberFunction##argNames##MemberFunctionTablePtr_; \
56  \
57  /* Class to add constructor from argList to table */ \
58  template<class baseType##Type> \
59  class add##memberFunction##argNames##MemberFunctionToTable \
60  { \
61  public: \
62  \
63  explicit add##memberFunction##argNames##MemberFunctionToTable \
64  ( \
65  const word& lookup = baseType##Type::typeName \
66  ) \
67  { \
68  construct##memberFunction##argNames##MemberFunctionTables(); \
69  memberFunction##argNames##MemberFunctionTablePtr_->insert \
70  ( \
71  lookup, \
72  baseType##Type::memberFunction \
73  ); \
74  } \
75  \
76  ~add##memberFunction##argNames##MemberFunctionToTable() \
77  { \
78  destroy##memberFunction##argNames##MemberFunctionTables(); \
79  } \
80  }; \
81  \
82  /* Table memberFunction called from the table add function */ \
83  static void construct##memberFunction##argNames##MemberFunctionTables(); \
84  \
85  /* Table destructor called from the table add function destructor */ \
86  static void destroy##memberFunction##argNames##MemberFunctionTables()
87 
88 
89 // Constructor aid
90 #define defineMemberFunctionSelectionTableMemberFunction( \
91  baseType,memberFunction,argNames) \
92  \
93  /* Table memberFunction called from the table add function */ \
94  void baseType::construct##memberFunction##argNames##MemberFunctionTables() \
95  { \
96  static bool constructed = false; \
97  if (!constructed) \
98  { \
99  constructed = true; \
100  baseType::memberFunction##argNames##MemberFunctionTablePtr_ \
101  = new baseType::memberFunction##argNames##MemberFunctionTable; \
102  } \
103  }
104 
105 
106 // Destructor aid
107 #define defineMemberFunctionSelectionTableDestructor( \
108  baseType,memberFunction,argNames) \
109  \
110  /* Table destructor called from the table add function destructor */ \
111  void baseType::destroy##memberFunction##argNames##MemberFunctionTables() \
112  { \
113  if (baseType::memberFunction##argNames##MemberFunctionTablePtr_) \
114  { \
115  delete baseType::memberFunction##argNames## \
116  MemberFunctionTablePtr_; \
117  baseType::memberFunction##argNames## \
118  MemberFunctionTablePtr_ = nullptr; \
119  } \
120  }
121 
122 
123 // Create pointer to hash-table of functions
124 #define defineMemberFunctionSelectionTablePtr(baseType,memberFunction,argNames)\
125  \
126  /* Define the memberFunction table */ \
127  baseType::memberFunction##argNames##MemberFunctionTable* \
128  baseType::memberFunction##argNames##MemberFunctionTablePtr_(nullptr)
129 
130 
131 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
132 
133 //- Define run-time selection table
134 #define defineMemberFunctionSelectionTable(baseType,memberFunction,argNames) \
135  \
136  defineMemberFunctionSelectionTablePtr \
137  (baseType,memberFunction,argNames); \
138  defineMemberFunctionSelectionTableMemberFunction \
139  (baseType,memberFunction,argNames) \
140  defineMemberFunctionSelectionTableDestructor \
141  (baseType,memberFunction,argNames)
142 
143 
144 //- Define run-time selection table for template classes
145 // use when baseType doesn't need a template argument (eg, is a typedef)
146 #define defineTemplateMemberFunctionSelectionTable( \
147  baseType,memberFunction,argNames) \
148  \
149  template<> \
150  defineMemberFunctionSelectionTablePtr \
151  (baseType,memberFunction,argNames); \
152  template<> \
153  defineMemberFunctionSelectionTableMemberFunction \
154  (baseType,memberFunction,argNames) \
155  template<> \
156  defineMemberFunctionSelectionTableDestructor \
157  (baseType,memberFunction,argNames)
158 
159 
160 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
161 
162 // Constructor aid: use when baseType requires the Targ template argument
163 #define defineTemplatedMemberFunctionSelectionTableMemberFunction( \
164  baseType,memberFunction,argNames,Targ) \
165  \
166  /* Table memberFunction called from the table add function */ \
167  void baseType<Targ>::construct##memberFunction##argNames## \
168  MemberFunctionTables() \
169  { \
170  static bool constructed = false; \
171  if (!constructed) \
172  { \
173  constructed = true; \
174  baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_ \
175  = new baseType<Targ>::memberFunction##argNames## \
176  MemberFunctionTable; \
177  } \
178  }
179 
180 
181 // Destructor aid
182 // use when baseType requires the Targ template argument
183 #define defineTemplatedMemberFunctionSelectionTableDestructor( \
184  baseType,memberFunction,argNames,Targ) \
185  \
186  /* Table destructor called from the table add function destructor */ \
187  void baseType<Targ>::destroy##memberFunction##argNames## \
188  MemberFunctionTables() \
189  { \
190  if (baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_) \
191  { \
192  delete baseType<Targ>::memberFunction##argNames## \
193  MemberFunctionTablePtr_; \
194  baseType<Targ>::memberFunction##argNames## \
195  MemberFunctionTablePtr_ = nullptr; \
196  } \
197  }
198 
199 
200 // Create pointer to hash-table of functions
201 // use when baseType requires the Targ template argument
202 #define defineTemplatedMemberFunctionSelectionTablePtr( \
203  baseType,memberFunction,argNames,Targ) \
204  \
205  /* Define the memberFunction table */ \
206  baseType<Targ>::memberFunction##argNames##MemberFunctionTable* \
207  baseType<Targ>::memberFunction##argNames## \
208  MemberFunctionTablePtr_(nullptr)
209 
210 
211 //- Define run-time selection table for template classes
212 // use when baseType requires the Targ template argument
213 #define defineTemplatedMemberFunctionSelectionTable( \
214  baseType,memberFunction,argNames,Targ) \
215  \
216  template<> \
217  defineTemplatedMemberFunctionSelectionTablePtr \
218  (baseType,memberFunction,argNames,Targ); \
219  template<> \
220  defineTemplatedMemberFunctionSelectionTableMemberFunction \
221  (baseType,memberFunction,argNames,Targ) \
222  template<> \
223  defineTemplatedMemberFunctionSelectionTableDestructor \
224  (baseType,memberFunction,argNames,Targ)
225 
226 
227 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
228 
229 #endif
230 
231 // ************************************************************************* //
token.H
defineTemplateMemberFunctionSelectionTable
#define defineTemplateMemberFunctionSelectionTable( baseType, memberFunction, argNames)
Define run-time selection table for template classes.
Definition: memberFunctionSelectionTables.H:146
HashTable.H
defineMemberFunctionSelectionTableDestructor
#define defineMemberFunctionSelectionTableDestructor( baseType, memberFunction, argNames)
Definition: memberFunctionSelectionTables.H:107
defineTemplatedMemberFunctionSelectionTable
#define defineTemplatedMemberFunctionSelectionTable( baseType, memberFunction, argNames, Targ)
Define run-time selection table for template classes.
Definition: memberFunctionSelectionTables.H:213
defineTemplatedMemberFunctionSelectionTableDestructor
#define defineTemplatedMemberFunctionSelectionTableDestructor( baseType, memberFunction, argNames, Targ)
Definition: memberFunctionSelectionTables.H:183