Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / src / OpenFOAM / db / runTimeSelection / staticMemberFunctions / staticMemberFunctionSelectionTables.H
blob392250477b01b32646aaabe039ac1e03e506e415
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2010-2011 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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.
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.
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/>.
24 Global
25     Foam::staticMemberFunctionSelectionTables
27 Description
28     Macros to enable the easy declaration of member function selection tables.
30 \*---------------------------------------------------------------------------*/
32 #ifndef staticMemberFunctionSelectionTables_H
33 #define staticMemberFunctionSelectionTables_H
35 #include "memberFunctionSelectionTables.H"
37 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
39 // external use:
40 // ~~~~~~~~~~~~~
41 // declare a run-time selection:
42 #define declareStaticMemberFunctionSelectionTable\
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     /* Table memberFunction called from the table add function */             \
58     static void construct##memberFunction##argNames##MemberFunctionTables();  \
59                                                                               \
60     /* Table destructor called from the table add function destructor */      \
61     static void destroy##memberFunction##argNames##MemberFunctionTables();    \
62                                                                               \
63     /* Class to add constructor from argList to table */                      \
64     template<class baseType##Type>                                            \
65     class add##memberFunction##argNames##StaticMemberFunctionToTable          \
66     {                                                                         \
67     public:                                                                   \
68                                                                               \
69         add##memberFunction##argNames##StaticMemberFunctionToTable            \
70         (                                                                     \
71             const word& lookup,                                               \
72             memberFunction##argNames##MemberFunctionPtr function              \
73         )                                                                     \
74         {                                                                     \
75             construct##memberFunction##argNames##MemberFunctionTables();      \
76             memberFunction##argNames##MemberFunctionTablePtr_->insert         \
77             (                                                                 \
78                 lookup,                                                       \
79                 function                                                      \
80             );                                                                \
81         }                                                                     \
82                                                                               \
83         ~add##memberFunction##argNames##StaticMemberFunctionToTable()         \
84         {                                                                     \
85             destroy##memberFunction##argNames##MemberFunctionTables();        \
86         }                                                                     \
87     }
90 // internal use:
91 // constructor/destructor aid
92 #define defineStaticMemberFunctionSelectionTableConstructDestruct\
93 (baseType,memberFunction,argNames)                                            \
94                                                                               \
95     /* Table constructor called from the table add function constructor */    \
96     void baseType::construct##memberFunction##argNames##MemberFunctionTables()\
97     {                                                                         \
98         static bool constructed = false;                                      \
99         if (!constructed)                                                     \
100         {                                                                     \
101             constructed = true;                                               \
102             baseType::memberFunction##argNames##MemberFunctionTablePtr_       \
103                 = new baseType::memberFunction##argNames##MemberFunctionTable;\
104         }                                                                     \
105     };                                                                        \
106                                                                               \
107     /* Table destructor called from the table add function destructor */      \
108     void baseType::destroy##memberFunction##argNames##MemberFunctionTables()  \
109     {                                                                         \
110         if (baseType::memberFunction##argNames##MemberFunctionTablePtr_)      \
111         {                                                                     \
112             delete baseType::memberFunction##argNames##MemberFunctionTablePtr_;\
113             baseType::memberFunction##argNames##MemberFunctionTablePtr_ = NULL;\
114         }                                                                     \
115     }
118 // internal use:
119 // create pointer to hash-table of functions
120 #define defineStaticMemberFunctionSelectionTablePtr\
121 (baseType,memberFunction,argNames)                                            \
122                                                                               \
123     /* Define the memberFunction table */                                     \
124     baseType::memberFunction##argNames##MemberFunctionTable*                  \
125         baseType::memberFunction##argNames##MemberFunctionTablePtr_ = NULL
128 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
130 // external use:
131 // ~~~~~~~~~~~~~
132 // define run-time selection table
133 #define defineStaticMemberFunctionSelectionTable\
134 (baseType,memberFunction,argNames)                                            \
135                                                                               \
136     defineStaticMemberFunctionSelectionTablePtr                               \
137         (baseType,memberFunction,argNames);                                   \
138     defineStaticMemberFunctionSelectionTableConstructDestruct                 \
139         (baseType,memberFunction,argNames)                                    \
141 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
143 #endif
145 // ************************************************************************* //