ENH: mutable libs() access in Time and other classes (#1737)

- This reflects the pre-existing coding situation where const_cast was
  used throughout to effect the same.

STYLE: fix private/protected access

- CodedField, codedMixedFvPatchField
This commit is contained in:
Mark Olesen
2020-07-14 10:48:50 +02:00
parent 41d3e6f1d4
commit 1e7c6ea2f1
30 changed files with 79 additions and 88 deletions

View File

@ -130,14 +130,14 @@ public:
private: private:
// Private data // Private Data
//- Profiling trigger for time-loop (for run, loop) //- Profiling trigger for time-loop (for run, loop)
mutable std::unique_ptr<profilingTrigger> loopProfiling_; mutable std::unique_ptr<profilingTrigger> loopProfiling_;
//- Any loaded dynamic libraries. Make sure to construct before //- Any loaded dynamic libraries
// reading controlDict. // Construct before reading controlDict
dlLibraryTable libs_; mutable dlLibraryTable libs_;
//- The controlDict //- The controlDict
unwatchedIOdictionary controlDict_; unwatchedIOdictionary controlDict_;
@ -145,7 +145,7 @@ private:
protected: protected:
// Protected data // Protected Data
label startTimeIndex_; label startTimeIndex_;
@ -502,14 +502,8 @@ public:
return functionObjects_; return functionObjects_;
} }
//- External access to the loaded libraries //- Mutable access to the loaded dynamic libraries
const dlLibraryTable& libs() const dlLibraryTable& libs() const
{
return libs_;
}
//- External access to the loaded libraries
dlLibraryTable& libs()
{ {
return libs_; return libs_;
} }

View File

@ -73,7 +73,7 @@ Foam::dlLibraryTable& Foam::functionEntries::codeStream::libs
( (
dict.topDict() dict.topDict()
); );
return const_cast<Time&>(d.time()).libs(); return d.time().libs();
} }

View File

@ -49,6 +49,7 @@ SourceFiles
#include "dictionary.H" #include "dictionary.H"
#include "dynamicCodeContext.H" #include "dynamicCodeContext.H"
#include "fileName.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -100,12 +101,6 @@ class codedBase
const dynamicCodeContext& context const dynamicCodeContext& context
) const; ) const;
//- No copy construct
codedBase(const codedBase&) = delete;
//- No copy assignment
void operator=(const codedBase&) = delete;
protected: protected:
@ -141,7 +136,7 @@ protected:
//- or use the codeDict() to generate one //- or use the codeDict() to generate one
void updateLibrary(const word& name) const; void updateLibrary(const word& name) const;
//- Get the loaded dynamic libraries //- Mutable access to the loaded dynamic libraries
virtual dlLibraryTable& libs() const = 0; virtual dlLibraryTable& libs() const = 0;
//- Adapt the context for the current object //- Adapt the context for the current object
@ -161,6 +156,13 @@ protected:
virtual const dictionary& codeDict() const = 0; virtual const dictionary& codeDict() const = 0;
//- No copy construct
codedBase(const codedBase&) = delete;
//- No copy assignment
void operator=(const codedBase&) = delete;
public: public:
//- Runtime type information //- Runtime type information
@ -169,7 +171,7 @@ public:
// Constructors // Constructors
//- Construct null //- Default construct
codedBase() = default; codedBase() = default;

View File

@ -141,7 +141,7 @@ bool Foam::dynamicCode::resolveTemplates
DynamicList<fileName>& badFiles DynamicList<fileName>& badFiles
) )
{ {
// Try to get template from FOAM_CODESTREAM_TEMPLATES // Try to get template from FOAM_CODE_TEMPLATES
const fileName templateDir(Foam::getEnv(codeTemplateEnvName)); const fileName templateDir(Foam::getEnv(codeTemplateEnvName));
bool allOkay = true; bool allOkay = true;

View File

@ -83,7 +83,7 @@ Foam::autoPtr<Foam::functionObject> Foam::functionObject::New
if (finder.found()) if (finder.found())
{ {
const_cast<Time&>(runTime).libs().open runTime.libs().open
( (
dict, dict,
finder.ref().keyword(), finder.ref().keyword(),
@ -93,7 +93,7 @@ Foam::autoPtr<Foam::functionObject> Foam::functionObject::New
} }
// This is the simplified version without compatibility messages // This is the simplified version without compatibility messages
// const_cast<Time&>(runTime).libs().open // runTime.libs().open
// ( // (
// dict, // dict,
// "libs", // "libs",

View File

@ -771,7 +771,7 @@ bool Foam::functionObjectList::read()
addProfiling(fo, "functionObjects::read"); addProfiling(fo, "functionObjects::read");
const_cast<Time&>(time_).libs().open time_.libs().open
( (
functionsDict, functionsDict,
"libs", "libs",

View File

@ -66,7 +66,7 @@ const
template<class Type> template<class Type>
Foam::dlLibraryTable& Foam::codedFixedValuePointPatchField<Type>::libs() const Foam::dlLibraryTable& Foam::codedFixedValuePointPatchField<Type>::libs() const
{ {
return const_cast<dlLibraryTable&>(this->db().time().libs()); return this->db().time().libs();
} }

View File

@ -106,7 +106,7 @@ class codedFixedValuePointPatchField
public fixedValuePointPatchField<Type>, public fixedValuePointPatchField<Type>,
protected codedBase protected codedBase
{ {
// Private data // Private Data
//- Dictionary contents for the boundary condition //- Dictionary contents for the boundary condition
mutable dictionary dict_; mutable dictionary dict_;
@ -120,7 +120,7 @@ class codedFixedValuePointPatchField
const IOdictionary& dict() const; const IOdictionary& dict() const;
//- Get the loaded dynamic libraries //- Mutable access to the loaded dynamic libraries
virtual dlLibraryTable& libs() const; virtual dlLibraryTable& libs() const;
//- Adapt the context for the current object //- Adapt the context for the current object

View File

@ -854,8 +854,8 @@ Foam::argList::argList
if (strcmp(optName, "lib") == 0) if (strcmp(optName, "lib") == 0)
{ {
// The '-lib' option: // The '-lib' option:
// Append name(s) to libs_ for later opening // Append name(s) to libs for later opening
libs_.append(this->getList<fileName>(argi)); libs().append(this->getList<fileName>(argi));
} }
else if (strcmp(optName, "debug-switch") == 0) else if (strcmp(optName, "debug-switch") == 0)
{ {
@ -1066,8 +1066,8 @@ void Foam::argList::parse
jobInfo.add("foamBuild", build); jobInfo.add("foamBuild", build);
} }
// Load additional libraries // Load additional libraries (verbosity according to banner setting)
libs_.open(bannerEnabled()); libs().open(bannerEnabled());
} }

View File

@ -142,7 +142,7 @@ class argList
HashTable<string> options_; HashTable<string> options_;
//- Additional libraries //- Additional libraries
dlLibraryTable libs_; mutable dlLibraryTable libs_;
word executable_; word executable_;
fileName rootPath_; fileName rootPath_;
@ -346,11 +346,8 @@ public:
//- Return the ParRunControl //- Return the ParRunControl
inline const ParRunControl& parRunControl() const; inline const ParRunControl& parRunControl() const;
//- Access to the loaded libraries //- Mutable access to the loaded dynamic libraries
inline const dlLibraryTable& libs() const; inline dlLibraryTable& libs() const;
//- Access to the loaded libraries
inline dlLibraryTable& libs();
//- The number of arguments //- The number of arguments
inline label size() const noexcept; inline label size() const noexcept;

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2013 OpenFOAM Foundation Copyright (C) 2011-2013 OpenFOAM Foundation
Copyright (C) 2017-2019 OpenCFD Ltd. Copyright (C) 2017-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -112,13 +112,7 @@ inline const Foam::ParRunControl& Foam::argList::parRunControl() const
} }
inline const Foam::dlLibraryTable& Foam::argList::libs() const inline Foam::dlLibraryTable& Foam::argList::libs() const
{
return libs_;
}
inline Foam::dlLibraryTable& Foam::argList::libs()
{ {
return libs_; return libs_;
} }

View File

@ -308,7 +308,7 @@ const Foam::GAMGAgglomeration& Foam::GAMGAgglomeration::New
controlDict.getOrDefault<word>("agglomerator", "faceAreaPair") controlDict.getOrDefault<word>("agglomerator", "faceAreaPair")
); );
const_cast<Time&>(mesh.thisDb().time()).libs().open mesh.thisDb().time().libs().open
( (
controlDict, controlDict,
"geometricGAMGAgglomerationLibs", "geometricGAMGAgglomerationLibs",
@ -362,7 +362,7 @@ const Foam::GAMGAgglomeration& Foam::GAMGAgglomeration::New
controlDict.getOrDefault<word>("agglomerator", "faceAreaPair") controlDict.getOrDefault<word>("agglomerator", "faceAreaPair")
); );
const_cast<Time&>(mesh.thisDb().time()).libs().open mesh.thisDb().time().libs().open
( (
controlDict, controlDict,
"algebraicGAMGAgglomerationLibs", "algebraicGAMGAgglomerationLibs",
@ -408,7 +408,7 @@ Foam::autoPtr<Foam::GAMGAgglomeration> Foam::GAMGAgglomeration::New
controlDict.getOrDefault<word>("agglomerator", "faceAreaPair") controlDict.getOrDefault<word>("agglomerator", "faceAreaPair")
); );
const_cast<Time&>(mesh.thisDb().time()).libs().open mesh.thisDb().time().libs().open
( (
controlDict, controlDict,
"geometricGAMGAgglomerationLibs", "geometricGAMGAgglomerationLibs",

View File

@ -58,7 +58,7 @@ Foam::autoPtr<Foam::dynamicFvMesh> Foam::dynamicFvMesh::New(const IOobject& io)
Info<< "Selecting dynamicFvMesh " << modelType << endl; Info<< "Selecting dynamicFvMesh " << modelType << endl;
const_cast<Time&>(io.time()).libs().open io.time().libs().open
( (
dict, dict,
"dynamicFvMeshLibs", "dynamicFvMeshLibs",

View File

@ -89,7 +89,7 @@ void Foam::codedPoints0MotionSolver::prepare
Foam::dlLibraryTable& Foam::codedPoints0MotionSolver::libs() const Foam::dlLibraryTable& Foam::codedPoints0MotionSolver::libs() const
{ {
return const_cast<Time&>(mesh().time()).libs(); return mesh().time().libs();
} }

View File

@ -103,7 +103,7 @@ protected:
// Protected Member Functions // Protected Member Functions
//- Get the loaded dynamic libraries //- Mutable access to the loaded dynamic libraries
virtual dlLibraryTable& libs() const; virtual dlLibraryTable& libs() const;
//- Adapt the context for the current object //- Adapt the context for the current object

View File

@ -94,7 +94,7 @@ Foam::displacementMotionSolver::New
{ {
Info<< "Selecting motion solver: " << solverTypeName << endl; Info<< "Selecting motion solver: " << solverTypeName << endl;
const_cast<Time&>(mesh.time()).libs().open mesh.time().libs().open
( (
solverDict, solverDict,
"motionSolverLibs", "motionSolverLibs",

View File

@ -116,7 +116,7 @@ Foam::autoPtr<Foam::motionSolver> Foam::motionSolver::New
Info<< "Selecting motion solver: " << solverName << nl; Info<< "Selecting motion solver: " << solverName << nl;
const_cast<Time&>(mesh.time()).libs().open mesh.time().libs().open
( (
solverDict, solverDict,
"motionSolverLibs", "motionSolverLibs",

View File

@ -79,7 +79,7 @@ Foam::autoPtr<Foam::fv::option> Foam::fv::option::New
Info<< indent Info<< indent
<< "Selecting finite volume options type " << modelType << endl; << "Selecting finite volume options type " << modelType << endl;
const_cast<Time&>(mesh.time()).libs().open mesh.time().libs().open
( (
coeffs, coeffs,
"libs", "libs",

View File

@ -112,7 +112,6 @@ Foam::expressions::fvExprDriver::fvExprDriver
storedVariables_(), storedVariables_(),
specialVariablesIndex_(-1), specialVariablesIndex_(-1),
otherMeshName_(), otherMeshName_(),
libs_(),
writer_(nullptr) writer_(nullptr)
{} {}
@ -128,7 +127,6 @@ Foam::expressions::fvExprDriver::fvExprDriver
storedVariables_(rhs.storedVariables_), storedVariables_(rhs.storedVariables_),
specialVariablesIndex_(rhs.specialVariablesIndex_), specialVariablesIndex_(rhs.specialVariablesIndex_),
otherMeshName_(), otherMeshName_(),
libs_(),
writer_(nullptr) writer_(nullptr)
{} {}
@ -165,12 +163,15 @@ bool Foam::expressions::fvExprDriver::readDict
{ {
expressions::exprDriver::readDict(dict); expressions::exprDriver::readDict(dict);
// wordList plugins; // fileNameList plugins;
// if (dict.readIfPresent("functionPlugins", plugins)) // if (dict.readIfPresent("functionPlugins", plugins))
// { // {
// for (const word& plugin : plugins) // for (const fileName& libName : plugins)
// { // {
// libs_.open("libswak" + plugin + "FunctionPlugin.so"); // this->runTime().libs().open
// (
// "libswak" + libName + "FunctionPlugin" // verbose = true
// );
// } // }
// } // }

View File

@ -69,7 +69,6 @@ SourceFiles
#include "pointMesh.H" #include "pointMesh.H"
#include "volFields.H" #include "volFields.H"
#include "topoSetSource.H" #include "topoSetSource.H"
#include "dlLibraryTable.H"
#include "runTimeSelectionTables.H" #include "runTimeSelectionTables.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -118,9 +117,6 @@ class fvExprDriver
//- The name of the other mesh (if it is to be required) //- The name of the other mesh (if it is to be required)
word otherMeshName_; word otherMeshName_;
//- Additional libraries
dlLibraryTable libs_;
//- Writing and restoring //- Writing and restoring
autoPtr<exprDriverWriter> writer_; autoPtr<exprDriverWriter> writer_;

View File

@ -66,7 +66,7 @@ const Foam::IOdictionary& Foam::codedFixedValueFvPatchField<Type>::dict() const
template<class Type> template<class Type>
Foam::dlLibraryTable& Foam::codedFixedValueFvPatchField<Type>::libs() const Foam::dlLibraryTable& Foam::codedFixedValueFvPatchField<Type>::libs() const
{ {
return const_cast<dlLibraryTable&>(this->db().time().libs()); return this->db().time().libs();
} }

View File

@ -121,7 +121,7 @@ class codedFixedValueFvPatchField
const IOdictionary& dict() const; const IOdictionary& dict() const;
//- Get the loaded dynamic libraries //- Mutable access to the loaded dynamic libraries
virtual dlLibraryTable& libs() const; virtual dlLibraryTable& libs() const;
//- Adapt the context for the current object //- Adapt the context for the current object

View File

@ -65,7 +65,7 @@ const Foam::IOdictionary& Foam::codedMixedFvPatchField<Type>::dict() const
template<class Type> template<class Type>
Foam::dlLibraryTable& Foam::codedMixedFvPatchField<Type>::libs() const Foam::dlLibraryTable& Foam::codedMixedFvPatchField<Type>::libs() const
{ {
return const_cast<dlLibraryTable&>(this->db().time().libs()); return this->db().time().libs();
} }

View File

@ -104,7 +104,7 @@ SourceFiles
namespace Foam namespace Foam
{ {
// Forward declaration of classes // Forward Declarations
class dynamicCode; class dynamicCode;
class dynamicCodeContext; class dynamicCodeContext;
class IOdictionary; class IOdictionary;
@ -119,7 +119,7 @@ class codedMixedFvPatchField
public mixedFvPatchField<Type>, public mixedFvPatchField<Type>,
public codedBase public codedBase
{ {
// Private data // Private Data
//- Dictionary contents for the boundary condition //- Dictionary contents for the boundary condition
mutable dictionary dict_; mutable dictionary dict_;
@ -133,7 +133,12 @@ class codedMixedFvPatchField
const IOdictionary& dict() const; const IOdictionary& dict() const;
//- Get the loaded dynamic libraries
protected:
// Protected Member Functions
//- Mutable access to the loaded dynamic libraries
virtual dlLibraryTable& libs() const; virtual dlLibraryTable& libs() const;
//- Adapt the context for the current object //- Adapt the context for the current object

View File

@ -96,7 +96,7 @@ void Foam::functionObjects::codedFunctionObject::prepare
Foam::dlLibraryTable& Foam::functionObjects::codedFunctionObject::libs() const Foam::dlLibraryTable& Foam::functionObjects::codedFunctionObject::libs() const
{ {
return const_cast<Time&>(time_).libs(); return time_.libs();
} }

View File

@ -60,6 +60,7 @@ Usage
#{ #{
// Lookup U // Lookup U
const volVectorField& U = mesh().lookupObject<volVectorField>("U"); const volVectorField& U = mesh().lookupObject<volVectorField>("U");
// Write // Write
mag(U)().write(); mag(U)().write();
#}; #};
@ -99,7 +100,7 @@ class codedFunctionObject
{ {
protected: protected:
// Protected data // Protected Data
//- Input dictionary //- Input dictionary
dictionary dict_; dictionary dict_;
@ -118,7 +119,7 @@ protected:
// Protected Member Functions // Protected Member Functions
//- Get the loaded dynamic libraries //- Mutable access to the loaded dynamic libraries
virtual dlLibraryTable& libs() const; virtual dlLibraryTable& libs() const;
//- Adapt the context for the current object //- Adapt the context for the current object

View File

@ -87,7 +87,7 @@ void Foam::fv::CodedSource<Type>::prepare
template<class Type> template<class Type>
Foam::dlLibraryTable& Foam::fv::CodedSource<Type>::libs() const Foam::dlLibraryTable& Foam::fv::CodedSource<Type>::libs() const
{ {
return const_cast<Time&>(mesh_.time()).libs(); return mesh_.time().libs();
} }

View File

@ -126,7 +126,6 @@ class CodedSource
public cellSetOption, public cellSetOption,
protected codedBase protected codedBase
{ {
protected: protected:
// Protected Data // Protected Data
@ -143,7 +142,7 @@ protected:
// Protected Member Functions // Protected Member Functions
//- Get the loaded dynamic libraries //- Mutable access to the loaded dynamic libraries
virtual dlLibraryTable& libs() const; virtual dlLibraryTable& libs() const;
//- Adapt the context for the current object //- Adapt the context for the current object

View File

@ -27,16 +27,13 @@ License
#include "dynamicCode.H" #include "dynamicCode.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
template<class Type> template<class Type>
Foam::dlLibraryTable& Foam::dlLibraryTable&
Foam::PatchFunction1Types::CodedField<Type>::libs() const Foam::PatchFunction1Types::CodedField<Type>::libs() const
{ {
const dlLibraryTable& timeLibs = return this->patch_.boundaryMesh().mesh().time().libs();
this->patch_.boundaryMesh().mesh().time().libs();
return const_cast<dlLibraryTable&>(timeLibs);
} }

View File

@ -109,7 +109,15 @@ class CodedField
// Private Member Functions // Private Member Functions
//- Get the loaded dynamic libraries //- Get reference to the underlying Function1
const PatchFunction1<Type>& redirectFunction() const;
protected:
// Protected Member Functions
//- Mutable access to the loaded dynamic libraries
virtual dlLibraryTable& libs() const; virtual dlLibraryTable& libs() const;
//- Adapt the context for the current object //- Adapt the context for the current object
@ -121,9 +129,6 @@ class CodedField
// Clear the ptr to the redirected object // Clear the ptr to the redirected object
virtual void clearRedirect() const; virtual void clearRedirect() const;
//- Get reference to the underlying Function1
const PatchFunction1<Type>& redirectFunction() const;
// Get the (sub)dictionary to initialize the codeContext // Get the (sub)dictionary to initialize the codeContext
virtual const dictionary& codeDict(const dictionary& fullDict) const; virtual const dictionary& codeDict(const dictionary& fullDict) const;