Transferred copyright to the OpenFOAM Foundation
[OpenFOAM-2.0.x.git] / src / OpenFOAM / db / runTimeSelection / globalFunctions / globalFunctionSelectionTables.H
blob64a8d639c3b4421dbf37fc39080d850fe9be9d56
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.
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::globalFunctionSelectionTables
27 Description
28     Macros to enable the easy declaration of global function selection tables.
30 \*---------------------------------------------------------------------------*/
32 #ifndef globalMemberFunctionSelectionTables_H
33 #define globalMemberFunctionSelectionTables_H
35 #include "memberFunctionSelectionTables.H"
37 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
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     }
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     }
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
127 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
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)
140 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
142 #endif
144 // ************************************************************************* //