FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

memberFunctionSelectionTables.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software: you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
00023 
00024 Class
00025     Foam::memberFunctionSelectionTables
00026 
00027 Description
00028     Macros to enable the easy declaration of member function selection tables.
00029 
00030 \*---------------------------------------------------------------------------*/
00031 
00032 #include <OpenFOAM/token.H>
00033 
00034 #ifndef memberFunctionSelectionTables_H
00035 #define memberFunctionSelectionTables_H
00036 
00037 #include <OpenFOAM/HashTable.H>
00038 
00039 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00040 
00041 
00042 // external use:
00043 // ~~~~~~~~~~~~~
00044 // declare a run-time selection:
00045 #define declareMemberFunctionSelectionTable\
00046 (returnType,baseType,memberFunction,argNames,argList,parList)                 \
00047                                                                               \
00048     /* Construct from argList function pointer type */                        \
00049     typedef returnType (*memberFunction##argNames##MemberFunctionPtr)argList; \
00050                                                                               \
00051     /* Construct from argList function table type */                          \
00052     typedef HashTable                                                         \
00053         <memberFunction##argNames##MemberFunctionPtr, word, string::hash>     \
00054         memberFunction##argNames##MemberFunctionTable;                        \
00055                                                                               \
00056     /* Construct from argList function pointer table pointer */               \
00057     static memberFunction##argNames##MemberFunctionTable*                     \
00058         memberFunction##argNames##MemberFunctionTablePtr_;                    \
00059                                                                               \
00060     /* Class to add constructor from argList to table */                      \
00061     template<class baseType##Type>                                            \
00062     class add##memberFunction##argNames##MemberFunctionToTable                \
00063     {                                                                         \
00064     public:                                                                   \
00065                                                                               \
00066         add##memberFunction##argNames##MemberFunctionToTable                  \
00067         (                                                                     \
00068             const word& lookup = baseType##Type::typeName                     \
00069         )                                                                     \
00070         {                                                                     \
00071             construct##memberFunction##argNames##MemberFunctionTables();      \
00072             memberFunction##argNames##MemberFunctionTablePtr_->insert         \
00073             (                                                                 \
00074                 lookup,                                                       \
00075                 baseType##Type::memberFunction                                \
00076             );                                                                \
00077         }                                                                     \
00078                                                                               \
00079         ~add##memberFunction##argNames##MemberFunctionToTable()               \
00080         {                                                                     \
00081             destroy##memberFunction##argNames##MemberFunctionTables();        \
00082         }                                                                     \
00083     };                                                                        \
00084                                                                               \
00085     /* Table memberFunction called from the table add function */             \
00086     static void construct##memberFunction##argNames##MemberFunctionTables();  \
00087                                                                               \
00088     /* Table destructor called from the table add function destructor */      \
00089     static void destroy##memberFunction##argNames##MemberFunctionTables()
00090 
00091 
00092 // internal use:
00093 // constructor aid
00094 #define defineMemberFunctionSelectionTableMemberFunction\
00095 (baseType,memberFunction,argNames)                                            \
00096                                                                               \
00097     /* Table memberFunction called from the table add function */             \
00098     void baseType::construct##memberFunction##argNames##MemberFunctionTables()\
00099     {                                                                         \
00100         static bool constructed = false;                                      \
00101                                                                               \
00102         if (!constructed)                                                     \
00103         {                                                                     \
00104             baseType::memberFunction##argNames##MemberFunctionTablePtr_       \
00105                 = new baseType::memberFunction##argNames##MemberFunctionTable;\
00106                                                                               \
00107             constructed = true;                                               \
00108         }                                                                     \
00109     }
00110 
00111 
00112 // internal use:
00113 // destructor aid
00114 #define defineMemberFunctionSelectionTableDestructor\
00115 (baseType,memberFunction,argNames)                                            \
00116                                                                               \
00117     /* Table destructor called from the table add function destructor */      \
00118     void baseType::destroy##memberFunction##argNames##MemberFunctionTables()  \
00119     {                                                                         \
00120         if (baseType::memberFunction##argNames##MemberFunctionTablePtr_)      \
00121         {                                                                     \
00122             delete baseType::memberFunction##argNames##MemberFunctionTablePtr_;\
00123             baseType::memberFunction##argNames##MemberFunctionTablePtr_ = NULL;\
00124         }                                                                     \
00125     }
00126 
00127 
00128 // internal use:
00129 // create pointer to hash-table of functions
00130 #define defineMemberFunctionSelectionTablePtr\
00131 (baseType,memberFunction,argNames)                                            \
00132                                                                               \
00133     /* Define the memberFunction table */                                     \
00134     baseType::memberFunction##argNames##MemberFunctionTable*                  \
00135         baseType::memberFunction##argNames##MemberFunctionTablePtr_ = NULL
00136 
00137 
00138 // not much in use:
00139 #define defineTemplateMemberFunctionSelectionTablePtr\
00140 (baseType,memberFunction,argNames)                                            \
00141                                                                               \
00142     /* Define the memberFunction table */                                     \
00143     typename baseType::memberFunction##argNames##MemberFunctionTable*         \
00144         baseType::memberFunction##argNames##MemberFunctionTablePtr_ = NULL
00145 
00146 
00147 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00148 
00149 // external use:
00150 // ~~~~~~~~~~~~~
00151 // define run-time selection table
00152 #define defineMemberFunctionSelectionTable\
00153 (baseType,memberFunction,argNames)                                            \
00154                                                                               \
00155     defineMemberFunctionSelectionTablePtr                                     \
00156         (baseType,memberFunction,argNames);                                   \
00157     defineMemberFunctionSelectionTableMemberFunction                          \
00158         (baseType,memberFunction,argNames)                                    \
00159     defineMemberFunctionSelectionTableDestructor                              \
00160         (baseType,memberFunction,argNames)
00161 
00162 
00163 // external use:
00164 // ~~~~~~~~~~~~~
00165 // define run-time selection table for template classes
00166 // use when baseType doesn't need a template argument (eg, is a typedef)
00167 #define defineTemplateMemberFunctionSelectionTable\
00168 (baseType,memberFunction,argNames)                                            \
00169                                                                               \
00170     template<>                                                                \
00171     defineMemberFunctionSelectionTablePtr                                     \
00172         (baseType,memberFunction,argNames);                                   \
00173     template<>                                                                \
00174     defineMemberFunctionSelectionTableMemberFunction                          \
00175         (baseType,memberFunction,argNames)                                    \
00176     template<>                                                                \
00177     defineMemberFunctionSelectionTableDestructor                              \
00178         (baseType,memberFunction,argNames)
00179 
00180 
00181 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00182 
00183 // internal use:
00184 // constructor aid
00185 // use when baseType requires the Targ template argument
00186 #define defineTemplatedMemberFunctionSelectionTableMemberFunction\
00187 (baseType,memberFunction,argNames,Targ)                                       \
00188                                                                               \
00189     /* Table memberFunction called from the table add function */             \
00190     void baseType<Targ>::construct##memberFunction##argNames##MemberFunctionTables()\
00191     {                                                                         \
00192         static bool constructed = false;                                      \
00193                                                                               \
00194         if (!constructed)                                                     \
00195         {                                                                     \
00196             baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_       \
00197                 = new baseType<Targ>::memberFunction##argNames##MemberFunctionTable;\
00198                                                                               \
00199             constructed = true;                                               \
00200         }                                                                     \
00201     }
00202 
00203 
00204 // internal use:
00205 // destructor aid
00206 // use when baseType requires the Targ template argument
00207 #define defineTemplatedMemberFunctionSelectionTableDestructor\
00208 (baseType,memberFunction,argNames,Targ)                                       \
00209                                                                               \
00210     /* Table destructor called from the table add function destructor */      \
00211     void baseType<Targ>::destroy##memberFunction##argNames##MemberFunctionTables()  \
00212     {                                                                         \
00213         if (baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_)      \
00214         {                                                                     \
00215             delete baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_;\
00216             baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_ = NULL;\
00217         }                                                                     \
00218     }
00219 
00220 
00221 // internal use:
00222 // create pointer to hash-table of functions
00223 // use when baseType requires the Targ template argument
00224 #define defineTemplatedMemberFunctionSelectionTablePtr\
00225 (baseType,memberFunction,argNames,Targ)                                       \
00226                                                                               \
00227     /* Define the memberFunction table */                                     \
00228     baseType<Targ>::memberFunction##argNames##MemberFunctionTable*            \
00229         baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_ = NULL
00230 
00231 
00232 // external use:
00233 // ~~~~~~~~~~~~~
00234 // define run-time selection table for template classes
00235 // use when baseType requires the Targ template argument
00236 #define defineTemplatedMemberFunctionSelectionTable\
00237 (baseType,memberFunction,argNames,Targ)                                       \
00238                                                                               \
00239     template<>                                                                \
00240     defineTemplatedMemberFunctionSelectionTablePtr                            \
00241         (baseType,memberFunction,argNames,Targ);                              \
00242     template<>                                                                \
00243     defineTemplatedMemberFunctionSelectionTableMemberFunction                 \
00244         (baseType,memberFunction,argNames,Targ)                               \
00245     template<>                                                                \
00246     defineTemplatedMemberFunctionSelectionTableDestructor                     \
00247         (baseType,memberFunction,argNames,Targ)
00248 
00249 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00250 
00251 #endif
00252 
00253 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines