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

runTimeSelectionTables.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::runTimeSelectionTables
00026 
00027 Description
00028     Macros to enable the easy declaration of run-time selection tables.
00029 
00030     declareRunTimeSelectionTable is used to create a run-time selection table
00031     for a base-class which holds constructor pointers on the table.
00032 
00033     declareRunTimeNewSelectionTable is used to create a run-time selection
00034     table for a derived-class which holds "New" pointers on the table.
00035 
00036 \*---------------------------------------------------------------------------*/
00037 
00038 #include <OpenFOAM/token.H>
00039 
00040 #ifndef runTimeSelectionTables_H
00041 #define runTimeSelectionTables_H
00042 
00043 #include <OpenFOAM/autoPtr.H>
00044 #include <OpenFOAM/HashTable.H>
00045 
00046 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00047 
00048 
00049 // external use:
00050 // ~~~~~~~~~~~~~
00051 // declare a run-time selection:
00052 #define declareRunTimeSelectionTable\
00053 (autoPtr,baseType,argNames,argList,parList)                                   \
00054                                                                               \
00055     /* Construct from argList function pointer type */                        \
00056     typedef autoPtr< baseType > (*argNames##ConstructorPtr)argList;           \
00057                                                                               \
00058     /* Construct from argList function table type */                          \
00059     typedef HashTable< argNames##ConstructorPtr, word, string::hash >         \
00060         argNames##ConstructorTable;                                           \
00061                                                                               \
00062     /* Construct from argList function pointer table pointer */               \
00063     static argNames##ConstructorTable* argNames##ConstructorTablePtr_;        \
00064                                                                               \
00065     /* Class to add constructor from argList to table */                      \
00066     template< class baseType##Type >                                          \
00067     class add##argNames##ConstructorToTable                                   \
00068     {                                                                         \
00069     public:                                                                   \
00070                                                                               \
00071         static autoPtr< baseType > New argList                                \
00072         {                                                                     \
00073             return autoPtr< baseType >(new baseType##Type parList);           \
00074         }                                                                     \
00075                                                                               \
00076         add##argNames##ConstructorToTable                                     \
00077         (                                                                     \
00078             const word& lookup = baseType##Type::typeName                     \
00079         )                                                                     \
00080         {                                                                     \
00081             construct##argNames##ConstructorTables();                         \
00082             argNames##ConstructorTablePtr_->insert(lookup, New);              \
00083         }                                                                     \
00084                                                                               \
00085         ~add##argNames##ConstructorToTable()                                  \
00086         {                                                                     \
00087             destroy##argNames##ConstructorTables();                           \
00088         }                                                                     \
00089     };                                                                        \
00090                                                                               \
00091     /* Table constructor called from the table add function */                \
00092     static void construct##argNames##ConstructorTables();                     \
00093                                                                               \
00094     /* Table destructor called from the table add function destructor */      \
00095     static void destroy##argNames##ConstructorTables()
00096 
00097 
00098 // external use:
00099 // ~~~~~~~~~~~~~
00100 // declare a run-time selection for derived classes:
00101 #define declareRunTimeNewSelectionTable\
00102 (autoPtr,baseType,argNames,argList,parList)                                   \
00103                                                                               \
00104     /* Construct from argList function pointer type */                        \
00105     typedef autoPtr< baseType > (*argNames##ConstructorPtr)argList;           \
00106                                                                               \
00107     /* Construct from argList function table type */                          \
00108     typedef HashTable< argNames##ConstructorPtr, word, string::hash >         \
00109         argNames##ConstructorTable;                                           \
00110                                                                               \
00111     /* Construct from argList function pointer table pointer */               \
00112     static argNames##ConstructorTable* argNames##ConstructorTablePtr_;        \
00113                                                                               \
00114     /* Class to add constructor from argList to table */                      \
00115     template< class baseType##Type >                                          \
00116     class add##argNames##ConstructorToTable                                   \
00117     {                                                                         \
00118     public:                                                                   \
00119                                                                               \
00120         static autoPtr< baseType > New##baseType argList                      \
00121         {                                                                     \
00122             return autoPtr< baseType >(baseType##Type::New parList.ptr());    \
00123         }                                                                     \
00124                                                                               \
00125         add##argNames##ConstructorToTable                                     \
00126         (                                                                     \
00127             const word& lookup = baseType##Type::typeName                     \
00128         )                                                                     \
00129         {                                                                     \
00130             construct##argNames##ConstructorTables();                         \
00131             argNames##ConstructorTablePtr_->insert                            \
00132             (                                                                 \
00133                 lookup,                                                       \
00134                 New##baseType                                                 \
00135             );                                                                \
00136         }                                                                     \
00137                                                                               \
00138         ~add##argNames##ConstructorToTable()                                  \
00139         {                                                                     \
00140             destroy##argNames##ConstructorTables();                           \
00141         }                                                                     \
00142     };                                                                        \
00143                                                                               \
00144     /* Table constructor called from the table add function */                \
00145     static void construct##argNames##ConstructorTables();                     \
00146                                                                               \
00147     /* Table destructor called from the table add function destructor */      \
00148     static void destroy##argNames##ConstructorTables()
00149 
00150 
00151 // internal use:
00152 // constructor aid
00153 #define defineRunTimeSelectionTableConstructor\
00154 (baseType,argNames)                                                           \
00155                                                                               \
00156     /* Table constructor called from the table add function */                \
00157     void baseType::construct##argNames##ConstructorTables()                   \
00158     {                                                                         \
00159         static bool constructed = false;                                      \
00160                                                                               \
00161         if (!constructed)                                                     \
00162         {                                                                     \
00163             baseType::argNames##ConstructorTablePtr_                          \
00164                 = new baseType::argNames##ConstructorTable;                   \
00165                                                                               \
00166             constructed = true;                                               \
00167         }                                                                     \
00168     }
00169 
00170 
00171 // internal use:
00172 // destructor aid
00173 #define defineRunTimeSelectionTableDestructor\
00174 (baseType,argNames)                                                           \
00175                                                                               \
00176     /* Table destructor called from the table add function destructor */      \
00177     void baseType::destroy##argNames##ConstructorTables()                     \
00178     {                                                                         \
00179         if (baseType::argNames##ConstructorTablePtr_)                         \
00180         {                                                                     \
00181             delete baseType::argNames##ConstructorTablePtr_;                  \
00182             baseType::argNames##ConstructorTablePtr_ = NULL;                  \
00183         }                                                                     \
00184     }
00185 
00186 
00187 // internal use:
00188 // create pointer to hash-table of functions
00189 #define defineRunTimeSelectionTablePtr\
00190 (baseType,argNames)                                                           \
00191                                                                               \
00192     /* Define the constructor function table */                               \
00193     baseType::argNames##ConstructorTable*                                     \
00194         baseType::argNames##ConstructorTablePtr_ = NULL
00195 
00196 
00197 // not much in use:
00198 #define defineTemplateRunTimeSelectionTablePtr(baseType,argNames)             \
00199                                                                               \
00200     /* Define the constructor function table */                               \
00201     typename baseType::argNames##ConstructorTable*                            \
00202         baseType::argNames##ConstructorTablePtr_ = NULL
00203 
00204 
00205 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00206 
00207 
00208 // external use:
00209 // ~~~~~~~~~~~~~
00210 // define run-time selection table
00211 #define defineRunTimeSelectionTable\
00212 (baseType,argNames)                                                           \
00213                                                                               \
00214     defineRunTimeSelectionTablePtr(baseType,argNames);                        \
00215     defineRunTimeSelectionTableConstructor(baseType,argNames);                \
00216     defineRunTimeSelectionTableDestructor(baseType,argNames)
00217 
00218 
00219 // external use:
00220 // ~~~~~~~~~~~~~
00221 // define run-time selection table for template classes
00222 // use when baseType doesn't need a template argument (eg, is a typedef)
00223 #define defineTemplateRunTimeSelectionTable\
00224 (baseType,argNames)                                                           \
00225                                                                               \
00226     template<>                                                                \
00227     defineRunTimeSelectionTablePtr(baseType,argNames);                        \
00228     template<>                                                                \
00229     defineRunTimeSelectionTableConstructor(baseType,argNames);                \
00230     template<>                                                                \
00231     defineRunTimeSelectionTableDestructor(baseType,argNames)
00232 
00233 
00234 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00235 
00236 
00237 // internal use:
00238 // constructor aid
00239 // use when baseType requires the Targ template argument
00240 #define defineTemplatedRunTimeSelectionTableConstructor\
00241 (baseType,argNames,Targ)                                                      \
00242                                                                               \
00243     /* Table constructor called from the table add function */                \
00244     void baseType< Targ >::construct##argNames##ConstructorTables()           \
00245     {                                                                         \
00246         static bool constructed = false;                                      \
00247                                                                               \
00248         if (!constructed)                                                     \
00249         {                                                                     \
00250             baseType< Targ >::argNames##ConstructorTablePtr_                  \
00251                 = new baseType< Targ >::argNames##ConstructorTable;           \
00252                                                                               \
00253             constructed = true;                                               \
00254         }                                                                     \
00255     }
00256 
00257 
00258 // internal use:
00259 // destructor aid
00260 // use when baseType requires the Targ template argument
00261 #define defineTemplatedRunTimeSelectionTableDestructor\
00262 (baseType,argNames,Targ)                                                      \
00263                                                                               \
00264     /* Table destructor called from the table add function destructor */      \
00265     void baseType< Targ >::destroy##argNames##ConstructorTables()             \
00266     {                                                                         \
00267         if (baseType< Targ >::argNames##ConstructorTablePtr_)                 \
00268         {                                                                     \
00269             delete baseType< Targ >::argNames##ConstructorTablePtr_;          \
00270             baseType< Targ >::argNames##ConstructorTablePtr_ = NULL;          \
00271         }                                                                     \
00272     }
00273 
00274 
00275 // internal use:
00276 // create pointer to hash-table of functions
00277 // use when baseType requires the Targ template argument
00278 #define defineTemplatedRunTimeSelectionTablePtr\
00279 (baseType,argNames,Targ)                                                      \
00280                                                                               \
00281     /* Define the constructor function table */                               \
00282     baseType< Targ >::argNames##ConstructorTable*                             \
00283         baseType< Targ >::argNames##ConstructorTablePtr_ = NULL
00284 
00285 
00286 // external use:
00287 // ~~~~~~~~~~~~~
00288 // define run-time selection table for template classes
00289 // use when baseType requires the Targ template argument
00290 #define defineTemplatedRunTimeSelectionTable\
00291 (baseType,argNames,Targ)                                                      \
00292                                                                               \
00293     template<>                                                                \
00294     defineTemplatedRunTimeSelectionTablePtr(baseType,argNames,Targ);          \
00295     template<>                                                                \
00296     defineTemplatedRunTimeSelectionTableConstructor(baseType,argNames,Targ);  \
00297     template<>                                                                \
00298     defineTemplatedRunTimeSelectionTableDestructor(baseType,argNames,Targ)
00299 
00300 
00301 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00302 
00303 #endif
00304 
00305 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines