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

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