From cc4c2989c349f28de7f683063d55dcd16d8ac592 Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Mon, 3 Oct 2016 09:08:01 +0100 Subject: [PATCH] fieldTypes: Using C++11 __VA_ARGS__ functionality created the FOR_ALL_FIELD_TYPES macro This supports the abstraction of the set of fields from the field code generation macros making it easier to change the set of fields supported by OpenFOAM. This functionality is demonstrated in the updated fvPatchFields macros and will be applied to the rest of the field code generation macros in the future. --- .../construction/addToRunTimeSelectionTable.H | 27 +++---- src/OpenFOAM/fields/Fields/fieldTypes.H | 18 ++++- src/OpenFOAM/global/debug/registerSwitch.H | 5 +- src/OpenFOAM/include/macros.H | 60 +++++++++++++++ .../basic/coupled/coupledFvPatchFields.C | 4 +- .../basic/transform/transformFvPatchFields.C | 4 +- .../jumpCyclic/jumpCyclicFvPatchFields.C | 2 +- .../jumpCyclicAMIFvPatchFields.C | 2 +- .../processorCyclicFvPatchFields.C | 7 +- .../derived/fan/fanFvPatchFields.C | 6 +- .../fixedNormalSlipFvPatchFields.C | 14 +--- .../fvPatchFields/fvPatchField/fvPatchField.H | 74 +++++++------------ 12 files changed, 129 insertions(+), 94 deletions(-) create mode 100644 src/OpenFOAM/include/macros.H diff --git a/src/OpenFOAM/db/runTimeSelection/construction/addToRunTimeSelectionTable.H b/src/OpenFOAM/db/runTimeSelection/construction/addToRunTimeSelectionTable.H index 2c23754e8a..997fa8287e 100644 --- a/src/OpenFOAM/db/runTimeSelection/construction/addToRunTimeSelectionTable.H +++ b/src/OpenFOAM/db/runTimeSelection/construction/addToRunTimeSelectionTable.H @@ -31,8 +31,7 @@ Description // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -// add to hash-table of functions with typename as the key +//- Add to hash-table of functions with typename as the key #define addToRunTimeSelectionTable\ (baseType,thisType,argNames) \ \ @@ -41,7 +40,7 @@ Description add##thisType##argNames##ConstructorTo##baseType##Table_ -// add to hash-table of functions with 'lookup' as the key +//- Add to hash-table of functions with 'lookup' as the key #define addNamedToRunTimeSelectionTable\ (baseType,thisType,argNames,lookup) \ \ @@ -51,7 +50,7 @@ Description (#lookup) -// add to hash-table of functions with typename as the key +//- Add to hash-table of functions with typename as the key #define addRemovableToRunTimeSelectionTable\ (baseType,thisType,argNames) \ \ @@ -60,7 +59,7 @@ Description addRemovable##thisType##argNames##ConstructorTo##baseType##Table_ -// add to hash-table of functions with 'lookup' as the key +//- Add to hash-table of functions with 'lookup' as the key #define addRemovableNamedToRunTimeSelectionTable\ (baseType,thisType,argNames,lookup) \ \ @@ -72,9 +71,8 @@ Description // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -// 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 addTemplateToRunTimeSelectionTable\ (baseType,thisType,Targ,argNames) \ \ @@ -83,8 +81,8 @@ Description add##thisType##Targ##argNames##ConstructorTo##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 'lookup' as the key. +// Use when baseType doesn't need a template argument (eg, is a typedef) #define addNamedTemplateToRunTimeSelectionTable\ (baseType,thisType,Targ,argNames,lookup) \ \ @@ -96,9 +94,8 @@ Description // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -// 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 addTemplatedToRunTimeSelectionTable\ (baseType,thisType,Targ,argNames) \ \ @@ -107,8 +104,8 @@ Description add##thisType##Targ##argNames##ConstructorTo##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 to hash-table of functions with 'lookup' as the key. +// Use when baseType requires the Targ template argument as well #define addNamedTemplatedToRunTimeSelectionTable\ (baseType,thisType,Targ,argNames,lookup) \ \ diff --git a/src/OpenFOAM/fields/Fields/fieldTypes.H b/src/OpenFOAM/fields/Fields/fieldTypes.H index 8550930357..13e17a3cce 100644 --- a/src/OpenFOAM/fields/Fields/fieldTypes.H +++ b/src/OpenFOAM/fields/Fields/fieldTypes.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -37,6 +37,22 @@ Description #include "symmTensor.H" #include "tensor.H" #include "triad.H" +#include "macros.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#define CAPITALIZE_scalar Scalar +#define CAPITALIZE_vector Vector +#define CAPITALIZE_sphericalTensor SphericalTensor +#define CAPITALIZE_symmTensor SymmTensor +#define CAPITALIZE_tensor Tensor + +#define FOR_ALL_FIELD_TYPES(Macro, ...) \ + Macro(scalar, __VA_ARGS__) \ + Macro(vector, __VA_ARGS__) \ + Macro(sphericalTensor, __VA_ARGS__) \ + Macro(symmTensor, __VA_ARGS__) \ + Macro(tensor, __VA_ARGS__) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/global/debug/registerSwitch.H b/src/OpenFOAM/global/debug/registerSwitch.H index 9e6d3580e4..7726529992 100644 --- a/src/OpenFOAM/global/debug/registerSwitch.H +++ b/src/OpenFOAM/global/debug/registerSwitch.H @@ -34,6 +34,7 @@ Description #define registerSwitch_H #include "simpleRegIOobject.H" +#include "macros.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -85,10 +86,6 @@ public: // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -#define CONCAT(x, y) x ## y -#define CONCAT2(x, y) CONCAT(x, y) -#define FILE_UNIQUE(x) CONCAT2(x, __LINE__) - #define registerOptSwitch(Name, Type, Switch) \ static Foam::RegisterSwitch FILE_UNIQUE(_addToOpt_) \ (Foam::debug::addOptimisationObject, Name, Switch) diff --git a/src/OpenFOAM/include/macros.H b/src/OpenFOAM/include/macros.H new file mode 100644 index 0000000000..c6648aeac2 --- /dev/null +++ b/src/OpenFOAM/include/macros.H @@ -0,0 +1,60 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2016 OpenFOAM Foundation + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Description + General C-preprocessor macros + +\*---------------------------------------------------------------------------*/ + +#ifndef macros_H +#define macros_H + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +//- Concatenate two preprocessor tokens +#define CAT_(a, b) a ## b +#define CAT(a, b) CAT_(a, b) + +//- Concatenate three preprocessor tokens +#define CAT3_(a, b, c) a ## b ## c +#define CAT3(a, b, c) CAT3_(a, b, c) + +//- Concatenate four preprocessor tokens +#define CAT4_(a, b, c, d) a ## b ## c ## d +#define CAT4(a, b, c, d) CAT4_(a, b, c, d) + +//- Concatenate five preprocessor tokens +#define CAT5_(a, b, c, d, e) a ## b ## c ## d ## e +#define CAT5(a, b, c, d, e) CAT5_(a, b, c, d, e) + +//- Generate an identifier unique within the file in which it is generated +#define FILE_UNIQUE(x) CAT(x, __LINE__) + +//- Map 'name' to 'Name' via the predefined macro CAPITALIZE_name +#define CAPITALIZE(name) CAPITALIZE_##name + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/finiteVolume/fields/fvPatchFields/basic/coupled/coupledFvPatchFields.C b/src/finiteVolume/fields/fvPatchFields/basic/coupled/coupledFvPatchFields.C index d919d49814..60d46338fa 100644 --- a/src/finiteVolume/fields/fvPatchFields/basic/coupled/coupledFvPatchFields.C +++ b/src/finiteVolume/fields/fvPatchFields/basic/coupled/coupledFvPatchFields.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -34,7 +34,7 @@ namespace Foam // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // -makePatchFieldsTypeName(coupled); +makePatchFieldTypeNames(coupled); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/finiteVolume/fields/fvPatchFields/basic/transform/transformFvPatchFields.C b/src/finiteVolume/fields/fvPatchFields/basic/transform/transformFvPatchFields.C index fa4a05aca9..e58113a03e 100644 --- a/src/finiteVolume/fields/fvPatchFields/basic/transform/transformFvPatchFields.C +++ b/src/finiteVolume/fields/fvPatchFields/basic/transform/transformFvPatchFields.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -34,7 +34,7 @@ namespace Foam // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // -makePatchFieldsTypeName(transform); +makePatchFieldTypeNames(transform); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/finiteVolume/fields/fvPatchFields/constraint/jumpCyclic/jumpCyclicFvPatchFields.C b/src/finiteVolume/fields/fvPatchFields/constraint/jumpCyclic/jumpCyclicFvPatchFields.C index 56ce7bdbe4..fc620604db 100644 --- a/src/finiteVolume/fields/fvPatchFields/constraint/jumpCyclic/jumpCyclicFvPatchFields.C +++ b/src/finiteVolume/fields/fvPatchFields/constraint/jumpCyclic/jumpCyclicFvPatchFields.C @@ -31,7 +31,7 @@ License namespace Foam { - makePatchFieldsTypeName(jumpCyclic); + makePatchFieldTypeNames(jumpCyclic); } // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // diff --git a/src/finiteVolume/fields/fvPatchFields/constraint/jumpCyclicAMI/jumpCyclicAMIFvPatchFields.C b/src/finiteVolume/fields/fvPatchFields/constraint/jumpCyclicAMI/jumpCyclicAMIFvPatchFields.C index 8985ed6900..c567c40062 100644 --- a/src/finiteVolume/fields/fvPatchFields/constraint/jumpCyclicAMI/jumpCyclicAMIFvPatchFields.C +++ b/src/finiteVolume/fields/fvPatchFields/constraint/jumpCyclicAMI/jumpCyclicAMIFvPatchFields.C @@ -34,7 +34,7 @@ namespace Foam // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // -makePatchFieldsTypeName(jumpCyclicAMI); +makePatchFieldTypeNames(jumpCyclicAMI); // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // diff --git a/src/finiteVolume/fields/fvPatchFields/constraint/processorCyclic/processorCyclicFvPatchFields.C b/src/finiteVolume/fields/fvPatchFields/constraint/processorCyclic/processorCyclicFvPatchFields.C index 737c6e81d5..5979a03139 100644 --- a/src/finiteVolume/fields/fvPatchFields/constraint/processorCyclic/processorCyclicFvPatchFields.C +++ b/src/finiteVolume/fields/fvPatchFields/constraint/processorCyclic/processorCyclicFvPatchFields.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -35,11 +35,6 @@ namespace Foam // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // makePatchFields(processorCyclic); -// makeTemplatePatchTypeField -// ( -// fvPatchScalarField, -// processorCyclicFvPatchScalarField -// ); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/finiteVolume/fields/fvPatchFields/derived/fan/fanFvPatchFields.C b/src/finiteVolume/fields/fvPatchFields/derived/fan/fanFvPatchFields.C index a4f2809df8..29b446df30 100644 --- a/src/finiteVolume/fields/fvPatchFields/derived/fan/fanFvPatchFields.C +++ b/src/finiteVolume/fields/fvPatchFields/derived/fan/fanFvPatchFields.C @@ -126,11 +126,7 @@ Foam::fanFvPatchField::fanFvPatchField namespace Foam { - makeTemplatePatchTypeField - ( - fvPatchScalarField, - fanFvPatchScalarField - ); + makeTemplatePatchTypeField(scalar, fan); } diff --git a/src/finiteVolume/fields/fvPatchFields/derived/fixedNormalSlip/fixedNormalSlipFvPatchFields.C b/src/finiteVolume/fields/fvPatchFields/derived/fixedNormalSlip/fixedNormalSlipFvPatchFields.C index d1bd512787..79bd108b8d 100644 --- a/src/finiteVolume/fields/fvPatchFields/derived/fixedNormalSlip/fixedNormalSlipFvPatchFields.C +++ b/src/finiteVolume/fields/fvPatchFields/derived/fixedNormalSlip/fixedNormalSlipFvPatchFields.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -34,16 +34,8 @@ namespace Foam // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // -makeTemplatePatchTypeField -( - fvPatchVectorField, - fixedNormalSlipFvPatchVectorField -); -makeTemplatePatchTypeField -( - fvPatchTensorField, - fixedNormalSlipFvPatchTensorField -); +makeTemplatePatchTypeField(vector, fixedNormalSlip); +makeTemplatePatchTypeField(tensor, fixedNormalSlip); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/finiteVolume/fields/fvPatchFields/fvPatchField/fvPatchField.H b/src/finiteVolume/fields/fvPatchFields/fvPatchField/fvPatchField.H index 1d689139b9..3fe1d2b0f6 100644 --- a/src/finiteVolume/fields/fvPatchFields/fvPatchField/fvPatchField.H +++ b/src/finiteVolume/fields/fvPatchFields/fvPatchField/fvPatchField.H @@ -46,6 +46,7 @@ SourceFiles #include "fvPatch.H" #include "DimensionedField.H" +#include "fieldTypes.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -628,7 +629,7 @@ defineTemplateRunTimeSelectionTable(fvPatchTypeField, dictionary); ); -// use with caution +// Use with caution #define addRemovableToPatchFieldRunTimeSelection\ (PatchTypeField, typePatchTypeField) \ \ @@ -652,67 +653,48 @@ defineTemplateRunTimeSelectionTable(fvPatchTypeField, dictionary); ); -// for non-templated patch fields +// For non-templated patch fields #define makePatchTypeField(PatchTypeField, typePatchTypeField) \ defineTypeNameAndDebug(typePatchTypeField, 0); \ addToPatchFieldRunTimeSelection(PatchTypeField, typePatchTypeField) -// for non-templated patch fields - use with caution +// For non-templated patch fields - use with caution #define makeRemovablePatchTypeField(PatchTypeField, typePatchTypeField) \ defineTypeNameAndDebug(typePatchTypeField, 0); \ addRemovableToPatchFieldRunTimeSelection(PatchTypeField, typePatchTypeField) - -// for templated patch fields -#define makeTemplatePatchTypeField(PatchTypeField, typePatchTypeField) \ - defineNamedTemplateTypeNameAndDebug(typePatchTypeField, 0); \ - addToPatchFieldRunTimeSelection(PatchTypeField, typePatchTypeField) - +// For templated patch fields +#define makeTemplatePatchTypeField(fieldType, type) \ + defineNamedTemplateTypeNameAndDebug \ + ( \ + CAT4(type, FvPatch, CAPITALIZE(fieldType), Field), \ + 0 \ + ); \ + addToPatchFieldRunTimeSelection \ + ( \ + CAT3(fvPatch, CAPITALIZE(fieldType), Field), \ + CAT4(type, FvPatch, CAPITALIZE(fieldType), Field) \ + ) #define makePatchFields(type) \ - makeTemplatePatchTypeField \ + FOR_ALL_FIELD_TYPES(makeTemplatePatchTypeField, type) + +#define makePatchFieldTypeName(fieldType, type) \ + defineNamedTemplateTypeNameAndDebug \ ( \ - fvPatchScalarField, \ - type##FvPatchScalarField \ - ); \ - makeTemplatePatchTypeField \ - ( \ - fvPatchVectorField, \ - type##FvPatchVectorField \ - ); \ - makeTemplatePatchTypeField \ - ( \ - fvPatchSphericalTensorField, \ - type##FvPatchSphericalTensorField \ - ); \ - makeTemplatePatchTypeField \ - ( \ - fvPatchSymmTensorField, \ - type##FvPatchSymmTensorField \ - ); \ - makeTemplatePatchTypeField \ - ( \ - fvPatchTensorField, \ - type##FvPatchTensorField \ + CAT4(type, FvPatch, CAPITALIZE(fieldType), Field), \ + 0 \ ); +#define makePatchFieldTypeNames(type) \ + FOR_ALL_FIELD_TYPES(makePatchFieldTypeName, type) -#define makePatchFieldsTypeName(type) \ - defineNamedTemplateTypeNameAndDebug(type##FvPatchScalarField, 0); \ - defineNamedTemplateTypeNameAndDebug(type##FvPatchVectorField, 0); \ - defineNamedTemplateTypeNameAndDebug(type##FvPatchSphericalTensorField, 0); \ - defineNamedTemplateTypeNameAndDebug(type##FvPatchSymmTensorField, 0); \ - defineNamedTemplateTypeNameAndDebug(type##FvPatchTensorField, 0) - +#define makePatchTypeFieldTypedef(fieldType, type) \ + typedef type##FvPatchField \ + CAT4(type, FvPatch, CAPITALIZE(fieldType), Field); #define makePatchTypeFieldTypedefs(type) \ - typedef type##FvPatchField type##FvPatchScalarField; \ - typedef type##FvPatchField type##FvPatchVectorField; \ - typedef type##FvPatchField \ - type##FvPatchSphericalTensorField; \ - typedef type##FvPatchField type##FvPatchSymmTensorField; \ - typedef type##FvPatchField type##FvPatchTensorField; - + FOR_ALL_FIELD_TYPES(makePatchTypeFieldTypedef, type) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //