memberFunctionSelectionTables.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 Class
25  Foam::memberFunctionSelectionTables
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  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##MemberFunctionTablePtr_;\
116  baseType::memberFunction##argNames##MemberFunctionTablePtr_ = \
117  nullptr; \
118  } \
119  }
120 
121 
122 // Create pointer to hash-table of functions
123 #define defineMemberFunctionSelectionTablePtr(baseType,memberFunction,argNames)\
124  \
125  /* Define the memberFunction table */ \
126  baseType::memberFunction##argNames##MemberFunctionTable* \
127  baseType::memberFunction##argNames##MemberFunctionTablePtr_ = nullptr
128 
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 \
191  ( \
192  baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_ \
193  ) \
194  { \
195  delete baseType<Targ>::memberFunction##argNames## \
196  MemberFunctionTablePtr_; \
197  baseType<Targ>::memberFunction##argNames## \
198  MemberFunctionTablePtr_ = nullptr; \
199  } \
200  }
201 
202 
203 // Create pointer to hash-table of functions
204 // use when baseType requires the Targ template argument
205 #define defineTemplatedMemberFunctionSelectionTablePtr( \
206  baseType,memberFunction,argNames,Targ) \
207  \
208  /* Define the memberFunction table */ \
209  baseType<Targ>::memberFunction##argNames##MemberFunctionTable* \
210  baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_ = \
211  nullptr
212 
213 
214 //- Define run-time selection table for template classes
215 // use when baseType requires the Targ template argument
216 #define defineTemplatedMemberFunctionSelectionTable( \
217  baseType,memberFunction,argNames,Targ) \
218  \
219  template<> \
220  defineTemplatedMemberFunctionSelectionTablePtr \
221  (baseType,memberFunction,argNames,Targ); \
222  template<> \
223  defineTemplatedMemberFunctionSelectionTableMemberFunction \
224  (baseType,memberFunction,argNames,Targ) \
225  template<> \
226  defineTemplatedMemberFunctionSelectionTableDestructor \
227  (baseType,memberFunction,argNames,Targ)
228 
229 
230 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
231 
232 #endif
233 
234 // ************************************************************************* //