FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/token.H>
33 
34 #ifndef memberFunctionSelectionTables_H
35 #define memberFunctionSelectionTables_H
36 
37 #include <OpenFOAM/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  \
102  if (!constructed) \
103  { \
104  baseType::memberFunction##argNames##MemberFunctionTablePtr_ \
105  = new baseType::memberFunction##argNames##MemberFunctionTable;\
106  \
107  constructed = true; \
108  } \
109  }
110 
111 
112 // internal use:
113 // destructor aid
114 #define defineMemberFunctionSelectionTableDestructor\
115 (baseType,memberFunction,argNames) \
116  \
117  /* Table destructor called from the table add function destructor */ \
118  void baseType::destroy##memberFunction##argNames##MemberFunctionTables() \
119  { \
120  if (baseType::memberFunction##argNames##MemberFunctionTablePtr_) \
121  { \
122  delete baseType::memberFunction##argNames##MemberFunctionTablePtr_;\
123  baseType::memberFunction##argNames##MemberFunctionTablePtr_ = NULL;\
124  } \
125  }
126 
127 
128 // internal use:
129 // create pointer to hash-table of functions
130 #define defineMemberFunctionSelectionTablePtr\
131 (baseType,memberFunction,argNames) \
132  \
133  /* Define the memberFunction table */ \
134  baseType::memberFunction##argNames##MemberFunctionTable* \
135  baseType::memberFunction##argNames##MemberFunctionTablePtr_ = NULL
136 
137 
138 // not much in use:
139 #define defineTemplateMemberFunctionSelectionTablePtr\
140 (baseType,memberFunction,argNames) \
141  \
142  /* Define the memberFunction table */ \
143  typename baseType::memberFunction##argNames##MemberFunctionTable* \
144  baseType::memberFunction##argNames##MemberFunctionTablePtr_ = NULL
145 
146 
147 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
148 
149 // external use:
150 // ~~~~~~~~~~~~~
151 // define run-time selection table
152 #define defineMemberFunctionSelectionTable\
153 (baseType,memberFunction,argNames) \
154  \
155  defineMemberFunctionSelectionTablePtr \
156  (baseType,memberFunction,argNames); \
157  defineMemberFunctionSelectionTableMemberFunction \
158  (baseType,memberFunction,argNames) \
159  defineMemberFunctionSelectionTableDestructor \
160  (baseType,memberFunction,argNames)
161 
162 
163 // external use:
164 // ~~~~~~~~~~~~~
165 // define run-time selection table for template classes
166 // use when baseType doesn't need a template argument (eg, is a typedef)
167 #define defineTemplateMemberFunctionSelectionTable\
168 (baseType,memberFunction,argNames) \
169  \
170  template<> \
171  defineMemberFunctionSelectionTablePtr \
172  (baseType,memberFunction,argNames); \
173  template<> \
174  defineMemberFunctionSelectionTableMemberFunction \
175  (baseType,memberFunction,argNames) \
176  template<> \
177  defineMemberFunctionSelectionTableDestructor \
178  (baseType,memberFunction,argNames)
179 
180 
181 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
182 
183 // internal use:
184 // constructor aid
185 // use when baseType requires the Targ template argument
186 #define defineTemplatedMemberFunctionSelectionTableMemberFunction\
187 (baseType,memberFunction,argNames,Targ) \
188  \
189  /* Table memberFunction called from the table add function */ \
190  void baseType<Targ>::construct##memberFunction##argNames##MemberFunctionTables()\
191  { \
192  static bool constructed = false; \
193  \
194  if (!constructed) \
195  { \
196  baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_ \
197  = new baseType<Targ>::memberFunction##argNames##MemberFunctionTable;\
198  \
199  constructed = true; \
200  } \
201  }
202 
203 
204 // internal use:
205 // destructor aid
206 // use when baseType requires the Targ template argument
207 #define defineTemplatedMemberFunctionSelectionTableDestructor\
208 (baseType,memberFunction,argNames,Targ) \
209  \
210  /* Table destructor called from the table add function destructor */ \
211  void baseType<Targ>::destroy##memberFunction##argNames##MemberFunctionTables() \
212  { \
213  if (baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_) \
214  { \
215  delete baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_;\
216  baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_ = NULL;\
217  } \
218  }
219 
220 
221 // internal use:
222 // create pointer to hash-table of functions
223 // use when baseType requires the Targ template argument
224 #define defineTemplatedMemberFunctionSelectionTablePtr\
225 (baseType,memberFunction,argNames,Targ) \
226  \
227  /* Define the memberFunction table */ \
228  baseType<Targ>::memberFunction##argNames##MemberFunctionTable* \
229  baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_ = NULL
230 
231 
232 // external use:
233 // ~~~~~~~~~~~~~
234 // define run-time selection table for template classes
235 // use when baseType requires the Targ template argument
236 #define defineTemplatedMemberFunctionSelectionTable\
237 (baseType,memberFunction,argNames,Targ) \
238  \
239  template<> \
240  defineTemplatedMemberFunctionSelectionTablePtr \
241  (baseType,memberFunction,argNames,Targ); \
242  template<> \
243  defineTemplatedMemberFunctionSelectionTableMemberFunction \
244  (baseType,memberFunction,argNames,Targ) \
245  template<> \
246  defineTemplatedMemberFunctionSelectionTableDestructor \
247  (baseType,memberFunction,argNames,Targ)
248 
249 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
250 
251 #endif
252 
253 // ************************ vim: set sw=4 sts=4 et: ************************ //