ENH: refactored and simplified runtime declaration macros

- improves future maintenance, avoids code/macro duplication
This commit is contained in:
Mark Olesen
2021-09-16 10:48:08 +02:00
parent 7f8ecd98f5
commit 0bd113f537
4 changed files with 376 additions and 552 deletions

View File

@ -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
#define addRemovableNamedToRunTimeSelectionTable\
(baseType,thisType,argNames,lookup) \
//- Add to construction table with 'lookupName' as the key
#define addNamedToRunTimeSelectionTable\
(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::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,lookupName) \
\
/* 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)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -45,334 +45,253 @@ 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 destructor called from the table add function destructor */ \
static void destroy##argNames##ConstructorTables(); \
\
/* Class to add constructor from argList to table */ \
template<class baseType##Type> \
class add##argNames##ConstructorToTable \
{ \
public: \
\
static autoPtr<baseType> New argList \
{ \
return autoPtr<baseType>(new baseType##Type parList); \
} \
\
explicit add##argNames##ConstructorToTable \
( \
const ::Foam::word& lookup = baseType##Type::typeName \
) \
{ \
construct##argNames##ConstructorTables(); \
if (!argNames##ConstructorTablePtr_->insert(lookup, New)) \
{ \
std::cerr<< "Duplicate entry " << lookup \
<< " in runtime selection table " << #baseType \
<< std::endl; \
error::safePrintStack(std::cerr); \
} \
} \
\
~add##argNames##ConstructorToTable() \
{ \
destroy##argNames##ConstructorTables(); \
} \
\
add##argNames##ConstructorToTable \
(const add##argNames##ConstructorToTable&) = delete; \
\
void operator= \
(const add##argNames##ConstructorToTable&) = delete; \
}; \
\
/* Class to add constructor from argList to table */ \
/* Remove only the entry (not the table) upon destruction */ \
template<class baseType##Type> \
class addRemovable##argNames##ConstructorToTable \
{ \
public: \
\
const ::Foam::word name; /* Lookup name for later removal */ \
\
static autoPtr<baseType> New argList \
{ \
return autoPtr<baseType>(new baseType##Type parList); \
} \
\
explicit addRemovable##argNames##ConstructorToTable \
( \
const ::Foam::word& lookup = baseType##Type::typeName \
) \
: \
name(lookup) \
{ \
construct##argNames##ConstructorTables(); \
argNames##ConstructorTablePtr_->set(lookup, New); \
} \
\
~addRemovable##argNames##ConstructorToTable() \
{ \
if (argNames##ConstructorTablePtr_) \
{ \
argNames##ConstructorTablePtr_->erase(name); \
} \
} \
\
addRemovable##argNames##ConstructorToTable \
(const addRemovable##argNames##ConstructorToTable&) = delete; \
\
void operator= \
(const addRemovable##argNames##ConstructorToTable&) = delete; \
};
/* Table construct/destruct helper */ \
static void prefix##TablePtr_construct(bool load)
//- Declare a run-time selection for derived classes
#define declareRunTimeNewSelectionTable( \
autoPtr,baseType,argNames,argList,parList) \
// Not used directly: storage and helper methods for runtime tables
#define defineRunTimeSelectionTableBase(baseType,prefix,Tspecialize) \
\
/* Construct from argList function pointer type */ \
typedef autoPtr<baseType> (*argNames##ConstructorPtr)argList; \
/* Define table singleton (storage) */ \
Tspecialize prefix##TableType* prefix##TablePtr_(nullptr); \
\
/* Function table type: construct from argList */ \
typedef ::Foam::HashTable \
< \
argNames##ConstructorPtr, \
::Foam::word, \
::Foam::string::hasher \
> argNames##ConstructorTableType; \
\
/* Construct from argList function pointer table pointer */ \
static argNames##ConstructorTableType* argNames##ConstructorTablePtr_; \
\
/* 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 */ \
template<class baseType##Type> \
class add##argNames##ConstructorToTable \
{ \
public: \
\
static autoPtr<baseType> New##baseType argList \
{ \
return autoPtr<baseType>(baseType##Type::New parList.ptr()); \
} \
\
explicit add##argNames##ConstructorToTable \
( \
const ::Foam::word& lookup = baseType##Type::typeName \
) \
{ \
construct##argNames##ConstructorTables(); \
if \
( \
!argNames##ConstructorTablePtr_->insert \
( \
lookup, \
New##baseType \
) \
) \
{ \
std::cerr<< "Duplicate entry " << lookup \
<< " in runtime selection table " << #baseType \
<< std::endl; \
error::safePrintStack(std::cerr); \
} \
} \
\
~add##argNames##ConstructorToTable() \
{ \
destroy##argNames##ConstructorTables(); \
} \
\
add##argNames##ConstructorToTable \
(const add##argNames##ConstructorToTable&) = delete; \
\
void operator= \
(const add##argNames##ConstructorToTable&) = delete; \
}; \
\
/* Class to add constructor from argList to table */ \
template<class baseType##Type> \
class addRemovable##argNames##ConstructorToTable \
{ \
public: \
\
const ::Foam::word name; /* Lookup name for later removal */ \
\
static autoPtr<baseType> New##baseType argList \
{ \
return autoPtr<baseType>(baseType##Type::New parList.ptr()); \
} \
\
explicit addRemovable##argNames##ConstructorToTable \
( \
const ::Foam::word& lookup = baseType##Type::typeName \
) \
: \
name(lookup) \
{ \
construct##argNames##ConstructorTables(); \
argNames##ConstructorTablePtr_->set \
( \
lookup, \
New##baseType \
); \
} \
\
~addRemovable##argNames##ConstructorToTable() \
{ \
if (argNames##ConstructorTablePtr_) \
{ \
argNames##ConstructorTablePtr_->erase(name); \
} \
} \
\
addRemovable##argNames##ConstructorToTable \
(const addRemovable##argNames##ConstructorToTable&) = delete; \
\
void operator= \
(const addRemovable##argNames##ConstructorToTable&) = delete; \
};
// Constructor aid
#define defineRunTimeSelectionTableConstructor(baseType,argNames) \
\
/* Table constructor called from the table add function */ \
void baseType::construct##argNames##ConstructorTables() \
/* Table construct/destruct helper */ \
Tspecialize void prefix##TablePtr_construct(bool load) \
{ \
static bool constructed = false; \
if (!constructed) \
if (load) \
{ \
constructed = true; \
baseType::argNames##ConstructorTablePtr_ \
= new baseType::argNames##ConstructorTableType; \
if (!constructed) \
{ \
prefix##TablePtr_ = new prefix##TableType; \
constructed = true; \
} \
} \
else if (prefix##TablePtr_) \
{ \
delete prefix##TablePtr_; \
prefix##TablePtr_ = nullptr; \
constructed = false; \
} \
}
// 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)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//
// 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) \
\
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 ptrWrapper<baseType>(new baseType##Type parList); \
} \
\
explicit add##argNames##ConstructorToTable \
( \
const ::Foam::word& k = baseType##Type::typeName \
) \
{ \
argNames##ConstructorTablePtr_construct(true); \
if (!argNames##ConstructorTablePtr_->insert(k, New)) \
{ \
std::cerr \
<< "Duplicate entry " << k << " in runtime table " \
<< #baseType << std::endl; \
::Foam::error::safePrintStack(std::cerr); \
} \
} \
\
~add##argNames##ConstructorToTable() \
{ \
argNames##ConstructorTablePtr_construct(false); \
} \
\
add##argNames##ConstructorToTable \
(const add##argNames##ConstructorToTable&) = delete; \
\
void operator= \
(const add##argNames##ConstructorToTable&) = delete; \
}; \
\
/* Helper to add constructor from argList to table */ \
/* Remove only the entry (not the table) upon destruction */ \
template<class baseType##Type> \
struct addRemovable##argNames##ConstructorToTable \
{ \
const ::Foam::word name; /* Lookup name for later removal */ \
\
static ptrWrapper<baseType> New argList \
{ \
return ptrWrapper<baseType>(new baseType##Type parList); \
} \
\
explicit addRemovable##argNames##ConstructorToTable \
( \
const ::Foam::word& k = baseType##Type::typeName \
) \
: \
name(k) \
{ \
argNames##ConstructorTablePtr_construct(true); \
argNames##ConstructorTablePtr_->set(k, New); \
} \
\
~addRemovable##argNames##ConstructorToTable() \
{ \
if (argNames##ConstructorTablePtr_) \
{ \
argNames##ConstructorTablePtr_->erase(name); \
} \
} \
\
addRemovable##argNames##ConstructorToTable \
(const addRemovable##argNames##ConstructorToTable&) = delete; \
\
void operator= \
(const addRemovable##argNames##ConstructorToTable&) = delete; \
};
//- Declare a run-time selection for derived classes
#define declareRunTimeNewSelectionTable\
(ptrWrapper,baseType,argNames,argList,parList) \
\
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##baseType argList \
{ \
return ptrWrapper<baseType>(baseType##Type::New parList.ptr()); \
} \
\
explicit add##argNames##ConstructorToTable \
( \
const ::Foam::word& k = baseType##Type::typeName \
) \
{ \
argNames##ConstructorTablePtr_construct(true); \
if (!argNames##ConstructorTablePtr_->insert(k, New##baseType)) \
{ \
std::cerr \
<< "Duplicate entry " << k << " in runtime table " \
<< #baseType << std::endl; \
::Foam::error::safePrintStack(std::cerr); \
} \
} \
\
~add##argNames##ConstructorToTable() \
{ \
argNames##ConstructorTablePtr_construct(false); \
} \
\
add##argNames##ConstructorToTable \
(const add##argNames##ConstructorToTable&) = delete; \
\
void operator= \
(const add##argNames##ConstructorToTable&) = delete; \
}; \
\
/* Helper to add constructor from argList to table */ \
template<class baseType##Type> \
struct addRemovable##argNames##ConstructorToTable \
{ \
const ::Foam::word name; /* Retain name for later removal */ \
\
static ptrWrapper<baseType> New##baseType argList \
{ \
return ptrWrapper<baseType>(baseType##Type::New parList.ptr()); \
} \
\
explicit addRemovable##argNames##ConstructorToTable \
( \
const ::Foam::word& k = baseType##Type::typeName \
) \
: \
name(k) \
{ \
argNames##ConstructorTablePtr_construct(true); \
argNames##ConstructorTablePtr_->set(k, New##baseType); \
} \
\
~addRemovable##argNames##ConstructorToTable() \
{ \
if (argNames##ConstructorTablePtr_) \
{ \
argNames##ConstructorTablePtr_->erase(name); \
} \
} \
\
addRemovable##argNames##ConstructorToTable \
(const addRemovable##argNames##ConstructorToTable&) = delete; \
\
void operator= \
(const addRemovable##argNames##ConstructorToTable&) = delete; \
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//
// 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<>)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -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
//- 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 thisType funcName to the table */ \
baseType<Targ>::add##funcName##argNames##MemberFunctionToTable \
<thisType<Targ>> \
add##thisType##Targ##funcName##argNames##MemberFunctionTo## \
baseType##Targ##Table_
// use when baseType requires the Targ template argument as well
//- 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 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) \
\
/* 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
#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 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)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -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) \
//
// 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 type */ \
typedef returnType (*memberFunction##argNames##MemberFunctionPtr)argList; \
declareRunTimeSelectionTableBase( \
returnType, funcName##argNames##MemberFunction, argList); \
\
/* Construct from argList function table type */ \
typedef ::Foam::HashTable \
< \
memberFunction##argNames##MemberFunctionPtr, \
::Foam::word, \
::Foam::string::hasher \
> memberFunction##argNames##MemberFunctionTableType; \
\
/* Construct from argList function pointer table pointer */ \
static memberFunction##argNames##MemberFunctionTableType* \
memberFunction##argNames##MemberFunctionTablePtr_; \
\
/* 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); \
} \
\
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 */ \
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) \
\
/* 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) \
{ \
constructed = true; \
baseType::memberFunction##argNames##MemberFunctionTablePtr_ \
= new baseType::memberFunction##argNames##MemberFunctionTableType; \
} \
}
// Destructor aid
#define defineMemberFunctionSelectionTableDestructor( \
baseType,memberFunction,argNames) \
const ::Foam::word name; /* Retain name for later removal */ \
\
/* Table destructor called from the table add function destructor */ \
void baseType::destroy##memberFunction##argNames##MemberFunctionTables() \
{ \
if (baseType::memberFunction##argNames##MemberFunctionTablePtr_) \
explicit addRemovable##funcName##argNames##MemberFunctionToTable \
( \
const ::Foam::word& k = baseType##Type::typeName \
) \
: \
name(k) \
{ \
delete baseType::memberFunction##argNames## \
MemberFunctionTablePtr_; \
baseType::memberFunction##argNames## \
MemberFunctionTablePtr_ = nullptr; \
funcName##argNames##MemberFunctionTablePtr_construct(true); \
funcName##argNames##MemberFunctionTablePtr_ \
->set(k, baseType##Type::funcName); \
} \
}
// 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() \
{ \
if (funcName##argNames##MemberFunctionTablePtr_) \
{ \
funcName##argNames##MemberFunctionTablePtr_->erase(name); \
} \
} \
\
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) \
#define defineTemplateMemberFunctionSelectionTable(baseType,funcName,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 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<>)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //