diff --git a/src/OpenFOAM/db/runTimeSelection/construction/addToRunTimeSelectionTable.H b/src/OpenFOAM/db/runTimeSelection/construction/addToRunTimeSelectionTable.H index 56579765c8611f9f8600047cf23e22a6d5e671ac..dc839d6dab326241fc3ca6184bc894c9827dbd5a 100644 --- a/src/OpenFOAM/db/runTimeSelection/construction/addToRunTimeSelectionTable.H +++ b/src/OpenFOAM/db/runTimeSelection/construction/addToRunTimeSelectionTable.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation + Copyright (C) 2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -26,6 +27,10 @@ License Description Macros for easy insertion into run-time selection tables +Note + The helper macro names used here must remain synchronized with + definitions in runTimeSelectionTables.H + \*---------------------------------------------------------------------------*/ #ifndef addToRunTimeSelectionTable_H @@ -33,88 +38,88 @@ Description // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -//- Add to hash-table of functions with typename as the key +//- Add to construction table with typeName as the key #define addToRunTimeSelectionTable\ (baseType,thisType,argNames) \ \ - /* Add the thisType constructor function to the table */ \ + /* Add thisType factory method to the table */ \ baseType::add##argNames##ConstructorToTable<thisType> \ add##thisType##argNames##ConstructorTo##baseType##Table_ -//- Add to hash-table of functions with 'lookup' as the key -#define addNamedToRunTimeSelectionTable\ -(baseType,thisType,argNames,lookup) \ - \ - /* Add the thisType constructor function to the table, find by lookup */ \ - baseType::add##argNames##ConstructorToTable<thisType> \ - add_##lookup##_##thisType##argNames##ConstructorTo##baseType##Table_ \ - (#lookup) - - -//- Add to hash-table of functions with typename as the key +//- Add to construction table with typeName as the key #define addRemovableToRunTimeSelectionTable\ (baseType,thisType,argNames) \ \ - /* Add the thisType constructor function to the table */ \ + /* Add thisType factory method to the table */ \ baseType::addRemovable##argNames##ConstructorToTable<thisType> \ addRemovable##thisType##argNames##ConstructorTo##baseType##Table_ -//- Add to hash-table of functions with 'lookup' as the key +//- Add to construction table with 'lookupName' as the key +#define addNamedToRunTimeSelectionTable\ +(baseType,thisType,argNames,lookupName) \ + \ + /* Add thisType factory method to the table, find by lookupName */ \ + baseType::add##argNames##ConstructorToTable<thisType> \ + add##thisType##argNames##ConstructorTo## \ + baseType##Table_##lookupName##_(#lookupName) + + +//- Add to construction table with 'lookupName' as the key #define addRemovableNamedToRunTimeSelectionTable\ -(baseType,thisType,argNames,lookup) \ +(baseType,thisType,argNames,lookupName) \ \ - /* Add the thisType constructor function to the table, find by lookup */ \ + /* Add thisType factory method to the table, find by lookupName */ \ baseType::addRemovable##argNames##ConstructorToTable<thisType> \ - addRemovable_##lookup##_##thisType##argNames##ConstructorTo \ - ##baseType##Table_(#lookup) + addRemovable##thisType##argNames##ConstructorTo## \ + baseType##Table_##lookupName##_(#lookupName) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -//- Add to hash-table of functions with typename as the key. +//- Add to construction table with typeName as the key. // Use when baseType doesn't need a template argument (eg, is a typedef) #define addTemplateToRunTimeSelectionTable\ (baseType,thisType,Targ,argNames) \ \ - /* Add the thisType constructor function to the table */ \ + /* Add thisType factory method to the table */ \ baseType::add##argNames##ConstructorToTable<thisType<Targ>> \ add##thisType##Targ##argNames##ConstructorTo##baseType##Table_ -//- Add to hash-table of functions with 'lookup' as the key. +//- Add to construction table with 'lookupName' as the key. // Use when baseType doesn't need a template argument (eg, is a typedef) #define addNamedTemplateToRunTimeSelectionTable\ -(baseType,thisType,Targ,argNames,lookup) \ +(baseType,thisType,Targ,argNames,lookupName) \ \ - /* Add the thisType constructor function to the table, find by lookup */ \ + /* Add thisType factory method to the table, find by lookupName */ \ baseType::add##argNames##ConstructorToTable<thisType<Targ>> \ - add_##lookup##_##thisType##Targ##argNames##ConstructorTo##baseType \ - ##Table_(#lookup) + add##thisType##Targ##argNames##ConstructorTo## \ + baseType##Table_##lookupName##_(#lookupName) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -//- Add to hash-table of functions with typename as the key. +//- Add to construction table with typeName as the key. // Use when baseType requires the Targ template argument as well #define addTemplatedToRunTimeSelectionTable\ (baseType,thisType,Targ,argNames) \ \ - /* Add the thisType constructor function to the table */ \ + /* Add thisType factory method to the table */ \ baseType<Targ>::add##argNames##ConstructorToTable<thisType<Targ>> \ add##thisType##Targ##argNames##ConstructorTo##baseType##Targ##Table_ -//- Add to hash-table of functions with 'lookup' as the key. +//- Add to construction table with 'lookupName' as the key. // Use when baseType requires the Targ template argument as well #define addNamedTemplatedToRunTimeSelectionTable\ -(baseType,thisType,Targ,argNames,lookup) \ +(baseType,thisType,Targ,argNames,lookupName) \ \ - /* Add the thisType constructor function to the table, find by lookup */ \ + /* Add thisType factory method to the table, find by lookupName */ \ baseType<Targ>::add##argNames##ConstructorToTable<thisType<Targ>> \ - add_##lookup##_##thisType##Targ##argNames##ConstructorTo##baseType## \ - Targ##Table_(#lookup) + add##thisType##Targ##argNames##ConstructorTo## \ + baseType##Targ##Table_##lookupName##_(#lookupName) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/db/runTimeSelection/construction/runTimeSelectionTables.H b/src/OpenFOAM/db/runTimeSelection/construction/runTimeSelectionTables.H index 51b615a76bd193bf733887b9c1f8dccf5cfe15ea..0fa16f0e6f3c1fc516bb1b34cf804f7351f6cbbb 100644 --- a/src/OpenFOAM/db/runTimeSelection/construction/runTimeSelectionTables.H +++ b/src/OpenFOAM/db/runTimeSelection/construction/runTimeSelectionTables.H @@ -45,58 +45,97 @@ Description // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -//- Declare a run-time selection -#define declareRunTimeSelectionTable(autoPtr,baseType,argNames,argList,parList)\ +// Common infrastructure + +// Not used directly: declare run-time selection (variables and methods) +#define declareRunTimeSelectionTableBase(returnType,prefix,argList) \ \ - /* Construct from argList function pointer type */ \ - typedef autoPtr<baseType> (*argNames##ConstructorPtr)argList; \ + /* Function pointer type: construct from argList */ \ + typedef returnType (*prefix##Ptr)argList; \ \ /* Function table type: construct from argList */ \ typedef ::Foam::HashTable \ < \ - argNames##ConstructorPtr, \ + prefix##Ptr, \ ::Foam::word, \ ::Foam::string::hasher \ - > argNames##ConstructorTableType; \ + > prefix##TableType; \ \ - /* Construct from argList function pointer table pointer */ \ - static argNames##ConstructorTableType* argNames##ConstructorTablePtr_; \ + /* Table singleton (storage) */ \ + static prefix##TableType* prefix##TablePtr_; \ \ - /* Table constructor called from the table add function */ \ - static void construct##argNames##ConstructorTables(); \ + /* Table construct/destruct helper */ \ + static void prefix##TablePtr_construct(bool load) + + +// Not used directly: storage and helper methods for runtime tables +#define defineRunTimeSelectionTableBase(baseType,prefix,Tspecialize) \ \ - /* Table destructor called from the table add function destructor */ \ - static void destroy##argNames##ConstructorTables(); \ + /* Define table singleton (storage) */ \ + Tspecialize prefix##TableType* prefix##TablePtr_(nullptr); \ \ - /* Class to add constructor from argList to table */ \ - template<class baseType##Type> \ - class add##argNames##ConstructorToTable \ + /* Table construct/destruct helper */ \ + Tspecialize void prefix##TablePtr_construct(bool load) \ { \ - public: \ + static bool constructed = false; \ + if (load) \ + { \ + if (!constructed) \ + { \ + prefix##TablePtr_ = new prefix##TableType; \ + constructed = true; \ + } \ + } \ + else if (prefix##TablePtr_) \ + { \ + delete prefix##TablePtr_; \ + prefix##TablePtr_ = nullptr; \ + constructed = false; \ + } \ + } + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +// +// Declaration Macros +// + +//- Declare a run-time selection (variables and adder classes) +// The ptrWrapper is typically a Foam::autoPtr or a Foam::tmp container. +#define declareRunTimeSelectionTable\ +(ptrWrapper,baseType,argNames,argList,parList) \ \ - static autoPtr<baseType> New argList \ + declareRunTimeSelectionTableBase( \ + ptrWrapper<baseType>, argNames##Constructor, argList); \ + \ + /* Helper to add constructor from argList to table */ \ + template<class baseType##Type> \ + struct add##argNames##ConstructorToTable \ + { \ + static ptrWrapper<baseType> New argList \ { \ - return autoPtr<baseType>(new baseType##Type parList); \ + return ptrWrapper<baseType>(new baseType##Type parList); \ } \ \ explicit add##argNames##ConstructorToTable \ ( \ - const ::Foam::word& lookup = baseType##Type::typeName \ + const ::Foam::word& k = baseType##Type::typeName \ ) \ { \ - construct##argNames##ConstructorTables(); \ - if (!argNames##ConstructorTablePtr_->insert(lookup, New)) \ + argNames##ConstructorTablePtr_construct(true); \ + if (!argNames##ConstructorTablePtr_->insert(k, New)) \ { \ - std::cerr<< "Duplicate entry " << lookup \ - << " in runtime selection table " << #baseType \ - << std::endl; \ - error::safePrintStack(std::cerr); \ + std::cerr \ + << "Duplicate entry " << k << " in runtime table " \ + << #baseType << std::endl; \ + ::Foam::error::safePrintStack(std::cerr); \ } \ } \ \ ~add##argNames##ConstructorToTable() \ { \ - destroy##argNames##ConstructorTables(); \ + argNames##ConstructorTablePtr_construct(false); \ } \ \ add##argNames##ConstructorToTable \ @@ -106,29 +145,27 @@ Description (const add##argNames##ConstructorToTable&) = delete; \ }; \ \ - /* Class to add constructor from argList to table */ \ + /* Helper to add constructor from argList to table */ \ /* Remove only the entry (not the table) upon destruction */ \ template<class baseType##Type> \ - class addRemovable##argNames##ConstructorToTable \ + struct addRemovable##argNames##ConstructorToTable \ { \ - public: \ - \ const ::Foam::word name; /* Lookup name for later removal */ \ \ - static autoPtr<baseType> New argList \ + static ptrWrapper<baseType> New argList \ { \ - return autoPtr<baseType>(new baseType##Type parList); \ + return ptrWrapper<baseType>(new baseType##Type parList); \ } \ \ explicit addRemovable##argNames##ConstructorToTable \ ( \ - const ::Foam::word& lookup = baseType##Type::typeName \ + const ::Foam::word& k = baseType##Type::typeName \ ) \ : \ - name(lookup) \ + name(k) \ { \ - construct##argNames##ConstructorTables(); \ - argNames##ConstructorTablePtr_->set(lookup, New); \ + argNames##ConstructorTablePtr_construct(true); \ + argNames##ConstructorTablePtr_->set(k, New); \ } \ \ ~addRemovable##argNames##ConstructorToTable() \ @@ -147,67 +184,40 @@ Description }; - //- Declare a run-time selection for derived classes -#define declareRunTimeNewSelectionTable( \ - autoPtr,baseType,argNames,argList,parList) \ - \ - /* Construct from argList function pointer type */ \ - typedef autoPtr<baseType> (*argNames##ConstructorPtr)argList; \ - \ - /* Function table type: construct from argList */ \ - typedef ::Foam::HashTable \ - < \ - argNames##ConstructorPtr, \ - ::Foam::word, \ - ::Foam::string::hasher \ - > argNames##ConstructorTableType; \ +#define declareRunTimeNewSelectionTable\ +(ptrWrapper,baseType,argNames,argList,parList) \ \ - /* Construct from argList function pointer table pointer */ \ - static argNames##ConstructorTableType* argNames##ConstructorTablePtr_; \ + declareRunTimeSelectionTableBase( \ + ptrWrapper<baseType>,argNames##Constructor,argList); \ \ - /* Table constructor called from the table add function */ \ - static void construct##argNames##ConstructorTables(); \ - \ - /* Table destructor called from the table add function destructor */ \ - static void destroy##argNames##ConstructorTables(); \ - \ - /* Class to add constructor from argList to table */ \ + /* Helper to add constructor from argList to table */ \ template<class baseType##Type> \ - class add##argNames##ConstructorToTable \ + struct add##argNames##ConstructorToTable \ { \ - public: \ - \ - static autoPtr<baseType> New##baseType argList \ + static ptrWrapper<baseType> New##baseType argList \ { \ - return autoPtr<baseType>(baseType##Type::New parList.ptr()); \ + return ptrWrapper<baseType>(baseType##Type::New parList.ptr()); \ } \ \ explicit add##argNames##ConstructorToTable \ ( \ - const ::Foam::word& lookup = baseType##Type::typeName \ + const ::Foam::word& k = baseType##Type::typeName \ ) \ { \ - construct##argNames##ConstructorTables(); \ - if \ - ( \ - !argNames##ConstructorTablePtr_->insert \ - ( \ - lookup, \ - New##baseType \ - ) \ - ) \ + argNames##ConstructorTablePtr_construct(true); \ + if (!argNames##ConstructorTablePtr_->insert(k, New##baseType)) \ { \ - std::cerr<< "Duplicate entry " << lookup \ - << " in runtime selection table " << #baseType \ - << std::endl; \ - error::safePrintStack(std::cerr); \ + std::cerr \ + << "Duplicate entry " << k << " in runtime table " \ + << #baseType << std::endl; \ + ::Foam::error::safePrintStack(std::cerr); \ } \ } \ \ ~add##argNames##ConstructorToTable() \ { \ - destroy##argNames##ConstructorTables(); \ + argNames##ConstructorTablePtr_construct(false); \ } \ \ add##argNames##ConstructorToTable \ @@ -217,32 +227,26 @@ Description (const add##argNames##ConstructorToTable&) = delete; \ }; \ \ - /* Class to add constructor from argList to table */ \ + /* Helper to add constructor from argList to table */ \ template<class baseType##Type> \ - class addRemovable##argNames##ConstructorToTable \ + struct addRemovable##argNames##ConstructorToTable \ { \ - public: \ - \ - const ::Foam::word name; /* Lookup name for later removal */ \ + const ::Foam::word name; /* Retain name for later removal */ \ \ - static autoPtr<baseType> New##baseType argList \ + static ptrWrapper<baseType> New##baseType argList \ { \ - return autoPtr<baseType>(baseType##Type::New parList.ptr()); \ + return ptrWrapper<baseType>(baseType##Type::New parList.ptr()); \ } \ \ explicit addRemovable##argNames##ConstructorToTable \ ( \ - const ::Foam::word& lookup = baseType##Type::typeName \ + const ::Foam::word& k = baseType##Type::typeName \ ) \ : \ - name(lookup) \ + name(k) \ { \ - construct##argNames##ConstructorTables(); \ - argNames##ConstructorTablePtr_->set \ - ( \ - lookup, \ - New##baseType \ - ); \ + argNames##ConstructorTablePtr_construct(true); \ + argNames##ConstructorTablePtr_->set(k, New##baseType); \ } \ \ ~addRemovable##argNames##ConstructorToTable() \ @@ -261,118 +265,33 @@ Description }; -// Constructor aid -#define defineRunTimeSelectionTableConstructor(baseType,argNames) \ - \ - /* Table constructor called from the table add function */ \ - void baseType::construct##argNames##ConstructorTables() \ - { \ - static bool constructed = false; \ - if (!constructed) \ - { \ - constructed = true; \ - baseType::argNames##ConstructorTablePtr_ \ - = new baseType::argNames##ConstructorTableType; \ - } \ - } - - -// Destructor aid -#define defineRunTimeSelectionTableDestructor(baseType,argNames) \ - \ - /* Table destructor called from the table add function destructor */ \ - void baseType::destroy##argNames##ConstructorTables() \ - { \ - if (baseType::argNames##ConstructorTablePtr_) \ - { \ - delete baseType::argNames##ConstructorTablePtr_; \ - baseType::argNames##ConstructorTablePtr_ = nullptr; \ - } \ - } - - -// Create pointer to hash-table of functions -#define defineRunTimeSelectionTablePtr(baseType,argNames) \ - \ - /* Define the constructor function table */ \ - baseType::argNames##ConstructorTableType* \ - baseType::argNames##ConstructorTablePtr_(nullptr) - - - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// +// Definition Macros +// + //- Define run-time selection table #define defineRunTimeSelectionTable(baseType,argNames) \ \ - defineRunTimeSelectionTablePtr(baseType,argNames); \ - defineRunTimeSelectionTableConstructor(baseType,argNames); \ - defineRunTimeSelectionTableDestructor(baseType,argNames) + defineRunTimeSelectionTableBase( \ + baseType,baseType::argNames##Constructor,) //- Define run-time selection table for template classes // use when baseType doesn't need a template argument (eg, is a typedef) #define defineTemplateRunTimeSelectionTable(baseType,argNames) \ \ - template<> \ - defineRunTimeSelectionTablePtr(baseType,argNames); \ - template<> \ - defineRunTimeSelectionTableConstructor(baseType,argNames); \ - template<> \ - defineRunTimeSelectionTableDestructor(baseType,argNames) - - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -// Constructor aid: use when baseType requires the Targ template argument -#define defineTemplatedRunTimeSelectionTableConstructor(baseType,argNames,Targ)\ - \ - /* Table constructor called from the table add function */ \ - void baseType<Targ>::construct##argNames##ConstructorTables() \ - { \ - static bool constructed = false; \ - if (!constructed) \ - { \ - constructed = true; \ - baseType<Targ>::argNames##ConstructorTablePtr_ \ - = new baseType<Targ>::argNames##ConstructorTableType; \ - } \ - } - - -// Destructor aid: use when baseType requires the Targ template argument -#define defineTemplatedRunTimeSelectionTableDestructor(baseType,argNames,Targ) \ - \ - /* Table destructor called from the table add function destructor */ \ - void baseType<Targ>::destroy##argNames##ConstructorTables() \ - { \ - if (baseType<Targ>::argNames##ConstructorTablePtr_) \ - { \ - delete baseType<Targ>::argNames##ConstructorTablePtr_; \ - baseType<Targ>::argNames##ConstructorTablePtr_ = nullptr; \ - } \ - } - - -//- Create pointer to hash-table of functions -// use when baseType requires the Targ template argument -#define defineTemplatedRunTimeSelectionTablePtr(baseType,argNames,Targ) \ - \ - /* Define the constructor function table */ \ - baseType<Targ>::argNames##ConstructorTableType* \ - baseType<Targ>::argNames##ConstructorTablePtr_(nullptr) + defineRunTimeSelectionTableBase( \ + baseType,baseType::argNames##Constructor,template<>) //- Define run-time selection table for template classes // use when baseType requires the Targ template argument #define defineTemplatedRunTimeSelectionTable(baseType,argNames,Targ) \ \ - template<> \ - defineTemplatedRunTimeSelectionTablePtr(baseType,argNames,Targ); \ - template<> \ - defineTemplatedRunTimeSelectionTableConstructor(baseType,argNames,Targ); \ - template<> \ - defineTemplatedRunTimeSelectionTableDestructor(baseType,argNames,Targ) + defineRunTimeSelectionTableBase( \ + baseType,baseType<Targ>::argNames##Constructor,template<>) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/db/runTimeSelection/memberFunctions/addToMemberFunctionSelectionTable.H b/src/OpenFOAM/db/runTimeSelection/memberFunctions/addToMemberFunctionSelectionTable.H index 07f83717d7cd8eef4300417197712cb7de1b2b34..1fc559276f286816c34c0588a0381195301c4fa7 100644 --- a/src/OpenFOAM/db/runTimeSelection/memberFunctions/addToMemberFunctionSelectionTable.H +++ b/src/OpenFOAM/db/runTimeSelection/memberFunctions/addToMemberFunctionSelectionTable.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation + Copyright (C) 2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -29,6 +30,10 @@ InClass Description Macros for easy insertion into member function selection tables +Note + The helper macro names used here must remain synchronized with + definitions in memberFunctionSelectionTables.H + \*---------------------------------------------------------------------------*/ #ifndef addToMemberFunctionSelectionTable_H @@ -36,99 +41,70 @@ Description // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -// add to hash-table of functions with typename as the key +//- Add to hash-table of functions with typeName as the key. #define addToMemberFunctionSelectionTable\ -(baseType,thisType,memberFunction,argNames) \ +(baseType,thisType,funcName,argNames) \ \ - /* Add the thisType memberFunction to the table */ \ - baseType::add##memberFunction##argNames##MemberFunctionToTable<thisType> \ - add##thisType##memberFunction##argNames##MemberFunctionTo##baseType##Table_ + /* Add thisType funcName to the table */ \ + baseType::add##funcName##argNames##MemberFunctionToTable<thisType> \ + add##thisType##funcName##argNames##MemberFunctionTo##baseType##Table_ - -// add to hash-table of functions with 'lookup' as the key +//- Add to hash-table of functions with 'lookupName' as the key. #define addNamedToMemberFunctionSelectionTable\ -(baseType,thisType,memberFunction,argNames,lookup) \ +(baseType,thisType,funcName,argNames,lookupName) \ \ - /* Add the thisType memberFunction to the table, find by lookup name */ \ - baseType::add##memberFunction##argNames##MemberFunctionToTable<thisType> \ - add_##lookup##_##thisType##memberFunction##argNames##MemberFunctionTo## \ - baseType##Table_(#lookup) + /* Add thisType funcName to the table, find by lookup name */ \ + baseType::add##funcName##argNames##MemberFunctionToTable<thisType> \ + add##thisType##funcName##argNames##MemberFunctionTo## \ + baseType##Table_##lookupName##_(#lookupName) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -// add to hash-table of functions with typename as the key -// use when baseType doesn't need a template argument (eg, is a typedef) +//- Add to hash-table of functions with typeName as the key. +// Use when baseType doesn't need a template argument (eg, is a typedef) #define addTemplateToMemberFunctionSelectionTable\ -(baseType,thisType,Targ,memberFunction,argNames) \ +(baseType,thisType,Targ,funcName,argNames) \ \ - /* Add the thisType memberFunction to the table */ \ - baseType::add##memberFunction##argNames## \ - MemberFunctionToTable<thisType<Targ>> \ - add##thisType##Targ##memberFunction##argNames##MemberFunctionTo## \ - baseType##Table_ + /* Add thisType funcName to the table */ \ + baseType::add##funcName##argNames##MemberFunctionToTable<thisType<Targ>> \ + add##thisType##Targ##funcName##argNames##MemberFunctionTo## \ + baseType##Table_ -// add to hash-table of functions with 'lookup' as the key -// use when baseType doesn't need a template argument (eg, is a typedef) +//- Add to hash-table of functions with 'lookupName' as the key. +// Use when baseType doesn't need a template argument (eg, is a typedef) #define addNamedTemplateToMemberFunctionSelectionTable\ -(baseType,thisType,Targ,memberFunction,argNames,lookup) \ +(baseType,thisType,Targ,funcName,argNames,lookupName) \ \ - /* Add the thisType memberFunction to the table, find by lookup name */ \ - baseType::add##memberFunction##argNames## \ - MemberFunctionToTable<thisType<Targ>> \ - add_##lookup##_##thisType##Targ##memberFunction##argNames## \ - MemberFunctionTo##baseType##Table_(#lookup) + /* Add thisType funcName to the table, find by lookupName */ \ + baseType::add##funcName##argNames##MemberFunctionToTable<thisType<Targ>> \ + add##thisType##Targ##funcName##argNames##MemberFunctionTo## \ + baseType##Table_##lookupName##_(#lookupName) -// use when baseType requires the Targ template argument as well -#define addTemplatedToMemberFunctionSelectionTable\ -(baseType,thisType,Targ,memberFunction,argNames) \ - \ - /* Add the thisType memberFunction to the table */ \ - baseType<Targ>::add##memberFunction##argNames## \ - MemberFunctionToTable<thisType<Targ>> \ - add##thisType##Targ##memberFunction##argNames##MemberFunctionTo## \ - baseType##Targ##Table_ -// use when baseType requires the Targ template argument as well -#define addNamedTemplatedToMemberFunctionSelectionTable\ -(baseType,thisType,Targ,memberFunction,argNames,lookup) \ - \ - /* Add the thisType memberFunction to the table, find by lookup name */ \ - baseType<Targ>::add##memberFunction##argNames## \ - MemberFunctionToTable<thisType<Targ>> \ - add_##lookup##_##thisType##Targ##memberFunction##argNames## \ - MemberFunctionTo##baseType##Targ##Table_(#lookup) - - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - - -// add to hash-table of functions with typename as the key -// use when baseType requires the Targ template argument as well +//- Add to hash-table of functions with typeName as the key. +// Use when baseType requires the Targ template argument as well #define addTemplatedToMemberFunctionSelectionTable\ -(baseType,thisType,Targ,memberFunction,argNames) \ +(baseType,thisType,Targ,funcName,argNames) \ \ - /* Add the thisType memberFunction to the table */ \ - baseType<Targ>::add##memberFunction##argNames## \ - MemberFunctionToTable<thisType<Targ>> \ - add##thisType##Targ##memberFunction##argNames##MemberFunctionTo## \ - baseType##Targ##Table_ - - -// add to hash-table of functions with 'lookup' as the key -// use when baseType requires the Targ template argument as well + /* Add thisType funcName to the table */ \ + baseType<Targ>::add##funcName##argNames##MemberFunctionToTable \ + <thisType<Targ>> \ + add##thisType##Targ##funcName##argNames##MemberFunctionTo## \ + baseType##Targ##Table_ + +//- Add to hash-table of functions with 'lookupName' as the key. +// Use when baseType requires the Targ template argument as well #define addNamedTemplatedToMemberFunctionSelectionTable\ -(baseType,thisType,Targ,memberFunction,argNames,lookup) \ +(baseType,thisType,Targ,funcName,argNames,lookupName) \ \ - /* Add the thisType memberFunction to the table, find by lookup name */ \ - baseType<Targ>::add##memberFunction##argNames## \ - MemberFunctionToTable<thisType<Targ>> \ - add_##lookup##_##thisType##Targ##memberFunction##argNames## \ - MemberFunctionTo##baseType##Targ##Table_(#lookup) + /* Add thisType funcName to the table, find by lookupName */ \ + baseType<Targ>::add##funcName##argNames##MemberFunctionToTable \ + <thisType<Targ>> \ + add##thisType##Targ##funcName##argNames##MemberFunctionTo## \ + baseType##Targ##Table_##lookupName##_(#lookupName) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/db/runTimeSelection/memberFunctions/memberFunctionSelectionTables.H b/src/OpenFOAM/db/runTimeSelection/memberFunctions/memberFunctionSelectionTables.H index b807c40f6992cfca36fbe6e57fe25c20806df1c5..206007a9b2ae40f2136ec44317627fa8a31eaf15 100644 --- a/src/OpenFOAM/db/runTimeSelection/memberFunctions/memberFunctionSelectionTables.H +++ b/src/OpenFOAM/db/runTimeSelection/memberFunctions/memberFunctionSelectionTables.H @@ -25,206 +25,130 @@ License along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. Description - Macros to enable the easy declaration of member function selection tables. + Macros to ease declaration of member function selection tables. -\*---------------------------------------------------------------------------*/ +Note + Uses macros from runTimeSelectionTables.H -#include "token.H" +\*---------------------------------------------------------------------------*/ #ifndef memberFunctionSelectionTables_H #define memberFunctionSelectionTables_H -#include "HashTable.H" +#include "runTimeSelectionTables.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -//- Declare a run-time selection: -#define declareMemberFunctionSelectionTable( \ - returnType,baseType,memberFunction,argNames,argList,parList) \ - \ - /* Construct from argList function pointer type */ \ - typedef returnType (*memberFunction##argNames##MemberFunctionPtr)argList; \ - \ - /* Construct from argList function table type */ \ - typedef ::Foam::HashTable \ - < \ - memberFunction##argNames##MemberFunctionPtr, \ - ::Foam::word, \ - ::Foam::string::hasher \ - > memberFunction##argNames##MemberFunctionTableType; \ +// +// Declaration Macros +// + +//- Declare a run-time member-function selection (variables and adder classes) +#define declareMemberFunctionSelectionTable\ +(returnType,baseType,funcName,argNames,argList,parListUnused) \ \ - /* Construct from argList function pointer table pointer */ \ - static memberFunction##argNames##MemberFunctionTableType* \ - memberFunction##argNames##MemberFunctionTablePtr_; \ + declareRunTimeSelectionTableBase( \ + returnType, funcName##argNames##MemberFunction, argList); \ \ - /* Class to add constructor from argList to table */ \ + /* Helper to add funcName to table */ \ template<class baseType##Type> \ - class add##memberFunction##argNames##MemberFunctionToTable \ + struct add##funcName##argNames##MemberFunctionToTable \ { \ - public: \ - \ - explicit add##memberFunction##argNames##MemberFunctionToTable \ + explicit add##funcName##argNames##MemberFunctionToTable \ ( \ - const ::Foam::word& lookup = baseType##Type::typeName \ + const ::Foam::word& k = baseType##Type::typeName \ ) \ { \ - construct##memberFunction##argNames##MemberFunctionTables(); \ - memberFunction##argNames##MemberFunctionTablePtr_->insert \ + funcName##argNames##MemberFunctionTablePtr_construct(true); \ + if \ ( \ - lookup, \ - baseType##Type::memberFunction \ - ); \ + !funcName##argNames##MemberFunctionTablePtr_ \ + ->insert(k, baseType##Type::funcName) \ + ) \ + { \ + std::cerr \ + << "Duplicate entry " << k << " in member table " \ + << #baseType << std::endl; \ + ::Foam::error::safePrintStack(std::cerr); \ + } \ } \ \ - ~add##memberFunction##argNames##MemberFunctionToTable() \ + ~add##funcName##argNames##MemberFunctionToTable() \ { \ - destroy##memberFunction##argNames##MemberFunctionTables(); \ + funcName##argNames##MemberFunctionTablePtr_construct(false); \ } \ - }; \ - \ - /* Table memberFunction called from the table add function */ \ - static void construct##memberFunction##argNames##MemberFunctionTables(); \ \ - /* Table destructor called from the table add function destructor */ \ - static void destroy##memberFunction##argNames##MemberFunctionTables() - - -// Constructor aid -#define defineMemberFunctionSelectionTableMemberFunction( \ - baseType,memberFunction,argNames) \ + add##funcName##argNames##MemberFunctionToTable \ + (const add##funcName##argNames##MemberFunctionToTable&) \ + = delete; \ + void operator= \ + (const add##funcName##argNames##MemberFunctionToTable&) \ + = delete; \ + }; \ \ - /* Table memberFunction called from the table add function */ \ - void baseType::construct##memberFunction##argNames##MemberFunctionTables() \ + /* Helper to add funcName to table */ \ + template<class baseType##Type> \ + struct addRemovable##funcName##argNames##MemberFunctionToTable \ { \ - static bool constructed = false; \ - if (!constructed) \ + const ::Foam::word name; /* Retain name for later removal */ \ + \ + explicit addRemovable##funcName##argNames##MemberFunctionToTable \ + ( \ + const ::Foam::word& k = baseType##Type::typeName \ + ) \ + : \ + name(k) \ { \ - constructed = true; \ - baseType::memberFunction##argNames##MemberFunctionTablePtr_ \ - = new baseType::memberFunction##argNames##MemberFunctionTableType; \ + funcName##argNames##MemberFunctionTablePtr_construct(true); \ + funcName##argNames##MemberFunctionTablePtr_ \ + ->set(k, baseType##Type::funcName); \ } \ - } - - -// Destructor aid -#define defineMemberFunctionSelectionTableDestructor( \ - baseType,memberFunction,argNames) \ \ - /* Table destructor called from the table add function destructor */ \ - void baseType::destroy##memberFunction##argNames##MemberFunctionTables() \ - { \ - if (baseType::memberFunction##argNames##MemberFunctionTablePtr_) \ + ~addRemovable##funcName##argNames##MemberFunctionToTable() \ { \ - delete baseType::memberFunction##argNames## \ - MemberFunctionTablePtr_; \ - baseType::memberFunction##argNames## \ - MemberFunctionTablePtr_ = nullptr; \ + if (funcName##argNames##MemberFunctionTablePtr_) \ + { \ + funcName##argNames##MemberFunctionTablePtr_->erase(name); \ + } \ } \ - } - - -// Create pointer to hash-table of functions -#define defineMemberFunctionSelectionTablePtr(baseType,memberFunction,argNames)\ \ - /* Define the memberFunction table */ \ - baseType::memberFunction##argNames##MemberFunctionTableType* \ - baseType::memberFunction##argNames##MemberFunctionTablePtr_(nullptr) + addRemovable##funcName##argNames##MemberFunctionToTable \ + (const addRemovable##funcName##argNames##MemberFunctionToTable&) \ + = delete; \ + void operator= \ + (const addRemovable##funcName##argNames##MemberFunctionToTable&) \ + = delete; \ + }; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// +// Definition Macros +// + //- Define run-time selection table -#define defineMemberFunctionSelectionTable(baseType,memberFunction,argNames) \ +#define defineMemberFunctionSelectionTable(baseType,funcName,argNames) \ \ - defineMemberFunctionSelectionTablePtr \ - (baseType,memberFunction,argNames); \ - defineMemberFunctionSelectionTableMemberFunction \ - (baseType,memberFunction,argNames) \ - defineMemberFunctionSelectionTableDestructor \ - (baseType,memberFunction,argNames) + declareRunTimeSelectionTableBase( \ + baseType,baseType::funcName##argNames##MemberFunction,) //- Define run-time selection table for template classes // use when baseType doesn't need a template argument (eg, is a typedef) -#define defineTemplateMemberFunctionSelectionTable( \ - baseType,memberFunction,argNames) \ - \ - template<> \ - defineMemberFunctionSelectionTablePtr \ - (baseType,memberFunction,argNames); \ - template<> \ - defineMemberFunctionSelectionTableMemberFunction \ - (baseType,memberFunction,argNames) \ - template<> \ - defineMemberFunctionSelectionTableDestructor \ - (baseType,memberFunction,argNames) - - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -// Constructor aid: use when baseType requires the Targ template argument -#define defineTemplatedMemberFunctionSelectionTableMemberFunction( \ - baseType,memberFunction,argNames,Targ) \ - \ - /* Table memberFunction called from the table add function */ \ - void baseType<Targ>::construct##memberFunction##argNames## \ - MemberFunctionTables() \ - { \ - static bool constructed = false; \ - if (!constructed) \ - { \ - constructed = true; \ - baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_ \ - = new baseType<Targ>::memberFunction##argNames## \ - MemberFunctionTableType; \ - } \ - } - - -// Destructor aid -// use when baseType requires the Targ template argument -#define defineTemplatedMemberFunctionSelectionTableDestructor( \ - baseType,memberFunction,argNames,Targ) \ - \ - /* Table destructor called from the table add function destructor */ \ - void baseType<Targ>::destroy##memberFunction##argNames## \ - MemberFunctionTables() \ - { \ - if (baseType<Targ>::memberFunction##argNames##MemberFunctionTablePtr_) \ - { \ - delete baseType<Targ>::memberFunction##argNames## \ - MemberFunctionTablePtr_; \ - baseType<Targ>::memberFunction##argNames## \ - MemberFunctionTablePtr_ = nullptr; \ - } \ - } - - -// Create pointer to hash-table of functions -// use when baseType requires the Targ template argument -#define defineTemplatedMemberFunctionSelectionTablePtr( \ - baseType,memberFunction,argNames,Targ) \ +#define defineTemplateMemberFunctionSelectionTable(baseType,funcName,argNames) \ \ - /* Define the memberFunction table */ \ - baseType<Targ>::memberFunction##argNames##MemberFunctionTableType* \ - baseType<Targ>::memberFunction##argNames## \ - MemberFunctionTablePtr_(nullptr) + declareRunTimeSelectionTableBase( \ + baseType,baseType::funcName##argNames##MemberFunction,template<>) //- Define run-time selection table for template classes // use when baseType requires the Targ template argument -#define defineTemplatedMemberFunctionSelectionTable( \ - baseType,memberFunction,argNames,Targ) \ +#define defineTemplatedMemberFunctionSelectionTable\ +(baseType,funcName,argNames,Targ) \ \ - template<> \ - defineTemplatedMemberFunctionSelectionTablePtr \ - (baseType,memberFunction,argNames,Targ); \ - template<> \ - defineTemplatedMemberFunctionSelectionTableMemberFunction \ - (baseType,memberFunction,argNames,Targ) \ - template<> \ - defineTemplatedMemberFunctionSelectionTableDestructor \ - (baseType,memberFunction,argNames,Targ) + declareRunTimeSelectionTableBase( \ + baseType,baseType<Targ>::funcName##argNames##MemberFunction,template<>) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //