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