globalFunctionSelectionTables.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 Global
25  Foam::globalFunctionSelectionTables
26 
27 Description
28  Macros to enable the easy declaration of global function selection tables.
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #ifndef globalMemberFunctionSelectionTables_H
33 #define globalMemberFunctionSelectionTables_H
34 
36 
37 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 
39 // external use:
40 // ~~~~~~~~~~~~~
41 // declare a run-time selection:
42 #define declareGlobalFunctionSelectionTable\
43 (returnType,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  extern memberFunction##argNames##MemberFunctionTable* \
55  memberFunction##argNames##MemberFunctionTablePtr_; \
56  \
57  /* Table memberFunction called from the table add function */ \
58  void construct##memberFunction##argNames##MemberFunctionTables(); \
59  \
60  /* Table destructor called from the table add function destructor */ \
61  void destroy##memberFunction##argNames##MemberFunctionTables(); \
62  \
63  /* Class to add constructor from argList to table */ \
64  class add##memberFunction##argNames##GlobalMemberFunctionToTable \
65  { \
66  public: \
67  \
68  add##memberFunction##argNames##GlobalMemberFunctionToTable \
69  ( \
70  const word& lookup, \
71  memberFunction##argNames##MemberFunctionPtr function \
72  ) \
73  { \
74  construct##memberFunction##argNames##MemberFunctionTables(); \
75  memberFunction##argNames##MemberFunctionTablePtr_->insert \
76  ( \
77  lookup, \
78  function \
79  ); \
80  } \
81  \
82  ~add##memberFunction##argNames##GlobalMemberFunctionToTable() \
83  { \
84  destroy##memberFunction##argNames##MemberFunctionTables(); \
85  } \
86  }
87 
88 
89 // internal use:
90 // constructor/destructor aid
91 #define defineGlobalFunctionSelectionTableConstructDestruct\
92 (memberFunction,argNames) \
93  \
94  /* Table constructor called from the table add function */ \
95  void construct##memberFunction##argNames##MemberFunctionTables() \
96  { \
97  static bool constructed = false; \
98  if (!constructed) \
99  { \
100  constructed = true; \
101  memberFunction##argNames##MemberFunctionTablePtr_ \
102  = new memberFunction##argNames##MemberFunctionTable; \
103  } \
104  } \
105  \
106  /* Table destructor called from the table add function destructor */ \
107  void destroy##memberFunction##argNames##MemberFunctionTables() \
108  { \
109  if (memberFunction##argNames##MemberFunctionTablePtr_) \
110  { \
111  delete memberFunction##argNames##MemberFunctionTablePtr_; \
112  memberFunction##argNames##MemberFunctionTablePtr_ = NULL; \
113  } \
114  }
115 
117 // internal use:
118 // create pointer to hash-table of functions
119 #define defineGlobalFunctionSelectionTablePtr\
120 (memberFunction,argNames) \
121  \
122  /* Define the memberFunction table */ \
123  memberFunction##argNames##MemberFunctionTable* \
124  memberFunction##argNames##MemberFunctionTablePtr_ = NULL
125 
126 
127 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
128 
129 // external use:
130 // ~~~~~~~~~~~~~
131 // define run-time selection table
132 #define defineGlobalFunctionSelectionTable\
133 (memberFunction,argNames) \
134  \
135  defineGlobalFunctionSelectionTablePtr \
136  (memberFunction,argNames); \
137  defineGlobalFunctionSelectionTableConstructDestruct \
138  (memberFunction,argNames)
139 
140 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
141 
142 #endif
143 
144 // ************************************************************************* //