OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

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-2009 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 the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     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, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Class
00026     Foam::memberFunctionSelectionTables
00027 
00028 Description
00029     Macros to enable the easy declaration of member function selection tables.
00030 
00031 \*---------------------------------------------------------------------------*/
00032 
00033 #include "token.H"
00034 
00035 #ifndef memberFunctionSelectionTables_H
00036 #define memberFunctionSelectionTables_H
00037 
00038 #include "HashTable.H"
00039 
00040 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00041 
00042 
00043 // external use:
00044 // ~~~~~~~~~~~~~
00045 // declare a run-time selection:
00046 #define declareMemberFunctionSelectionTable\
00047 (returnType,baseType,memberFunction,argNames,argList,parList)                 \
00048                                                                               \
00049     /* Construct from argList function pointer type */                        \
00050     typedef returnType (*memberFunction##argNames##MemberFunctionPtr)argList; \
00051                                                                               \
00052     /* Construct from argList function table type */                          \
00053     typedef HashTable                                                         \
00054         <memberFunction##argNames##MemberFunctionPtr, word, string::hash>     \
00055         memberFunction##argNames##MemberFunctionTable;                        \
00056                                                                               \
00057     /* Construct from argList function pointer table pointer */               \
00058     static memberFunction##argNames##MemberFunctionTable*                     \
00059         memberFunction##argNames##MemberFunctionTablePtr_;                    \
00060                                                                               \
00061     /* Class to add constructor from argList to table */                      \
00062     template<class baseType##Type>                                            \
00063     class add##memberFunction##argNames##MemberFunctionToTable                \
00064     {                                                                         \
00065     public:                                                                   \
00066                                                                               \
00067         add##memberFunction##argNames##MemberFunctionToTable                  \
00068         (                                                                     \
00069             const word& lookup = baseType##Type::typeName                     \
00070         )                                                                     \
00071         {                                                                     \
00072             construct##memberFunction##argNames##MemberFunctionTables();      \
00073             memberFunction##argNames##MemberFunctionTablePtr_->insert         \
00074             (                                                                 \
00075                 lookup,                                                       \
00076                 baseType##Type::memberFunction                                \
00077             );                                                                \
00078         }                                                                     \
00079                                                                               \
00080         ~add##memberFunction##argNames##MemberFunctionToTable()               \
00081         {                                                                     \
00082             destroy##memberFunction##argNames##MemberFunctionTables();        \
00083         }                                                                     \
00084     };                                                                        \
00085                                                                               \
00086     /* Table memberFunction called from the table add function */             \
00087     static void construct##memberFunction##argNames##MemberFunctionTables();  \
00088                                                                               \
00089     /* Table destructor called from the table add function destructor */      \
00090     static void destroy##memberFunction##argNames##MemberFunctionTables()
00091 
00092 
00093 // internal use:
00094 // constructor aid
00095 #define defineMemberFunctionSelectionTableMemberFunction\
00096 (baseType,memberFunction,argNames)                                            \
00097                                                                               \
00098     /* Table memberFunction called from the table add function */             \
00099     void baseType::construct##memberFunction##argNames##MemberFunctionTables()\
00100     {                                                                         \
00101         static bool constructed = false;                                      \
00102                                                                               \
00103         if (!constructed)                                                     \
00104         {                                                                     \
00105             baseType::memberFunction##argNames##MemberFunctionTablePtr_       \
00106                 = new baseType::memberFunction##argNames##MemberFunctionTable;\
00107                                                                               \
00108             constructed = true;                                               \
00109         }                                                                     \
00110     }
00111 
00112 
00113 // internal use:
00114 // destructor aid
00115 #define defineMemberFunctionSelectionTableDestructor\
00116 (baseType,memberFunction,argNames)                                            \
00117                                                                               \
00118     /* Table destructor called from the table add function destructor */      \
00119     void baseType::destroy##memberFunction##argNames##MemberFunctionTables()  \
00120     {                                                                         \
00121         if (baseType::memberFunction##argNames##MemberFunctionTablePtr_)      \
00122         {                                                                     \
00123             delete baseType::memberFunction##argNames##MemberFunctionTablePtr_;\
00124             baseType::memberFunction##argNames##MemberFunctionTablePtr_ = NULL;\
00125         }                                                                     \
00126     }
00127 
00128 
00129 // internal use:
00130 // create pointer to hash-table of functions
00131 #define defineMemberFunctionSelectionTablePtr\
00132 (baseType,memberFunction,argNames)                                            \
00133                                                                               \
00134     /* Define the memberFunction table */                                     \
00135     baseType::memberFunction##argNames##MemberFunctionTable*                  \
00136         baseType::memberFunction##argNames##MemberFunctionTablePtr_ = NULL
00137 
00138 
00139 // not much in use:
00140 #define defineTemplateMemberFunctionSelectionTablePtr\
00141 (baseType,memberFunction,argNames)                                            \
00142                                                                               \
00143     /* Define the memberFunction table */                                     \
00144     typename baseType::memberFunction##argNames##MemberFunctionTable*         \
00145         baseType::memberFunction##argNames##MemberFunctionTablePtr_ = NULL
00146 
00147 
00148 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00149 
00150 // external use:
00151 // ~~~~~~~~~~~~~
00152 // define run-time selection table
00153 #define defineMemberFunctionSelectionTable\
00154 (baseType,memberFunction,argNames)                                            \
00155                                                                               \
00156     defineMemberFunctionSelectionTablePtr                                     \
00157         (baseType,memberFunction,argNames);                                   \
00158     defineMemberFunctionSelectionTableMemberFunction                          \
00159         (baseType,memberFunction,argNames)                                    \
00160     defineMemberFunctionSelectionTableDestructor                              \
00161         (baseType,memberFunction,argNames)
00162 
00163 
00164 // external use:
00165 // ~~~~~~~~~~~~~
00166 // define run-time selection table for template classes
00167 // use when baseType doesn't need a template argument (eg, is a typedef)
00168 #define defineTemplateMemberFunctionSelectionTable\
00169 (baseType,memberFunction,argNames)                                            \
00170                                                                               \
00171     template<>                                                                \
00172     defineMemberFunctionSelectionTablePtr                                     \
00173         (baseType,memberFunction,argNames);                                   \
00174     template<>                                                                \
00175     defineMemberFunctionSelectionTableMemberFunction                          \
00176         (baseType,memberFunction,argNames)                                    \
00177     template<>                                                                \
00178     defineMemberFunctionSelectionTableDestructor                              \
00179         (baseType,memberFunction,argNames)
00180 
00181 
00182 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00183 
00184 // internal use:
00185 // constructor aid
00186 // use when baseType requires the Targ template argument
00187 #define defineTemplatedMemberFunctionSelectionTableMemberFunction\
00188 (baseType,memberFunction,argNames,Targ)                                       \
00189                                                                               \
00190     /* Table memberFunction called from the table add function */             \
00191     void baseType<Targ>::construct##memberFunction##argNames##MemberFunctionTables()\
00192     {                                                                         \
00193         static bool constructed = false;                                      \
00194                                                                               \
00195         if (!constructed)                                                     \
00196         {                                                                     \
00197             baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_       \
00198                 = new baseType<Targ>::memberFunction##argNames##MemberFunctionTable;\
00199                                                                               \
00200             constructed = true;                                               \
00201         }                                                                     \
00202     }
00203 
00204 
00205 // internal use:
00206 // destructor aid
00207 // use when baseType requires the Targ template argument
00208 #define defineTemplatedMemberFunctionSelectionTableDestructor\
00209 (baseType,memberFunction,argNames,Targ)                                       \
00210                                                                               \
00211     /* Table destructor called from the table add function destructor */      \
00212     void baseType<Targ>::destroy##memberFunction##argNames##MemberFunctionTables()  \
00213     {                                                                         \
00214         if (baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_)      \
00215         {                                                                     \
00216             delete baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_;\
00217             baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_ = NULL;\
00218         }                                                                     \
00219     }
00220 
00221 
00222 // internal use:
00223 // create pointer to hash-table of functions
00224 // use when baseType requires the Targ template argument
00225 #define defineTemplatedMemberFunctionSelectionTablePtr\
00226 (baseType,memberFunction,argNames,Targ)                                       \
00227                                                                               \
00228     /* Define the memberFunction table */                                     \
00229     baseType<Targ>::memberFunction##argNames##MemberFunctionTable*            \
00230         baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_ = NULL
00231 
00232 
00233 // external use:
00234 // ~~~~~~~~~~~~~
00235 // define run-time selection table for template classes
00236 // use when baseType requires the Targ template argument
00237 #define defineTemplatedMemberFunctionSelectionTable\
00238 (baseType,memberFunction,argNames,Targ)                                       \
00239                                                                               \
00240     template<>                                                                \
00241     defineTemplatedMemberFunctionSelectionTablePtr                            \
00242         (baseType,memberFunction,argNames,Targ);                              \
00243     template<>                                                                \
00244     defineTemplatedMemberFunctionSelectionTableMemberFunction                 \
00245         (baseType,memberFunction,argNames,Targ)                               \
00246     template<>                                                                \
00247     defineTemplatedMemberFunctionSelectionTableDestructor                     \
00248         (baseType,memberFunction,argNames,Targ)
00249 
00250 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00251 
00252 #endif
00253 
00254 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd