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-2016 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_ = NULL;\
117  } \
118  }
119 
120 
121 // Create pointer to hash-table of functions
122 #define defineMemberFunctionSelectionTablePtr(baseType,memberFunction,argNames)\
123  \
124  /* Define the memberFunction table */ \
125  baseType::memberFunction##argNames##MemberFunctionTable* \
126  baseType::memberFunction##argNames##MemberFunctionTablePtr_ = NULL
127 
128 
129 
130 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
131 
132 //- Define run-time selection table
133 #define defineMemberFunctionSelectionTable(baseType,memberFunction,argNames) \
134  \
135  defineMemberFunctionSelectionTablePtr \
136  (baseType,memberFunction,argNames); \
137  defineMemberFunctionSelectionTableMemberFunction \
138  (baseType,memberFunction,argNames) \
139  defineMemberFunctionSelectionTableDestructor \
140  (baseType,memberFunction,argNames)
141 
142 
143 //- Define run-time selection table for template classes
144 // use when baseType doesn't need a template argument (eg, is a typedef)
145 #define defineTemplateMemberFunctionSelectionTable( \
146  baseType,memberFunction,argNames) \
147  \
148  template<> \
149  defineMemberFunctionSelectionTablePtr \
150  (baseType,memberFunction,argNames); \
151  template<> \
152  defineMemberFunctionSelectionTableMemberFunction \
153  (baseType,memberFunction,argNames) \
154  template<> \
155  defineMemberFunctionSelectionTableDestructor \
156  (baseType,memberFunction,argNames)
157 
158 
159 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
160 
161 // Constructor aid: use when baseType requires the Targ template argument
162 #define defineTemplatedMemberFunctionSelectionTableMemberFunction( \
163  baseType,memberFunction,argNames,Targ) \
164  \
165  /* Table memberFunction called from the table add function */ \
166  void baseType<Targ>::construct##memberFunction##argNames## \
167  MemberFunctionTables() \
168  { \
169  static bool constructed = false; \
170  if (!constructed) \
171  { \
172  constructed = true; \
173  baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_ \
174  = new baseType<Targ>::memberFunction##argNames## \
175  MemberFunctionTable; \
176  } \
177  }
178 
179 
180 // Destructor aid
181 // use when baseType requires the Targ template argument
182 #define defineTemplatedMemberFunctionSelectionTableDestructor( \
183  baseType,memberFunction,argNames,Targ) \
184  \
185  /* Table destructor called from the table add function destructor */ \
186  void baseType<Targ>::destroy##memberFunction##argNames## \
187  MemberFunctionTables() \
188  { \
189  if \
190  ( \
191  baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_ \
192  ) \
193  { \
194  delete baseType<Targ>::memberFunction##argNames## \
195  MemberFunctionTablePtr_; \
196  baseType<Targ>::memberFunction##argNames## \
197  MemberFunctionTablePtr_ = NULL; \
198  } \
199  }
200 
201 
202 // Create pointer to hash-table of functions
203 // use when baseType requires the Targ template argument
204 #define defineTemplatedMemberFunctionSelectionTablePtr( \
205  baseType,memberFunction,argNames,Targ) \
206  \
207  /* Define the memberFunction table */ \
208  baseType<Targ>::memberFunction##argNames##MemberFunctionTable* \
209  baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_ = NULL
210 
211 
212 //- Define run-time selection table for template classes
213 // use when baseType requires the Targ template argument
214 #define defineTemplatedMemberFunctionSelectionTable( \
215  baseType,memberFunction,argNames,Targ) \
216  \
217  template<> \
218  defineTemplatedMemberFunctionSelectionTablePtr \
219  (baseType,memberFunction,argNames,Targ); \
220  template<> \
221  defineTemplatedMemberFunctionSelectionTableMemberFunction \
222  (baseType,memberFunction,argNames,Targ) \
223  template<> \
224  defineTemplatedMemberFunctionSelectionTableDestructor \
225  (baseType,memberFunction,argNames,Targ)
226 
227 
228 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
229 
230 #endif
231 
232 // ************************************************************************* //