ENH: calcEntry: new functionEntry method

This commit is contained in:
mattijs
2011-06-29 09:56:01 +01:00
parent ec67450164
commit 9fa370f54a
5 changed files with 259 additions and 18 deletions

View File

@ -160,6 +160,7 @@ $(dictionaryEntry)/dictionaryEntry.C
$(dictionaryEntry)/dictionaryEntryIO.C $(dictionaryEntry)/dictionaryEntryIO.C
functionEntries = $(dictionary)/functionEntries functionEntries = $(dictionary)/functionEntries
$(functionEntries)/calcEntry/calcEntry.C
$(functionEntries)/codeStream/codeStream.C $(functionEntries)/codeStream/codeStream.C
$(functionEntries)/functionEntry/functionEntry.C $(functionEntries)/functionEntry/functionEntry.C
$(functionEntries)/includeEntry/includeEntry.C $(functionEntries)/includeEntry/includeEntry.C

View File

@ -0,0 +1,99 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2011 OpenCFD Ltd.
\\/ 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 <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "calcEntry.H"
#include "addToMemberFunctionSelectionTable.H"
#include "dictionary.H"
#include "dynamicCode.H"
#include "codeStream.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace functionEntries
{
defineTypeNameAndDebug(calcEntry, 0);
addToMemberFunctionSelectionTable
(
functionEntry,
calcEntry,
execute,
primitiveEntryIstream
);
}
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::functionEntries::calcEntry::execute
(
const dictionary& parentDict,
primitiveEntry& thisEntry,
Istream& is
)
{
Info<< "Using #calcEntry at line " << is.lineNumber()
<< " in file " << parentDict.name() << endl;
dynamicCode::checkSecurity
(
"functionEntries::calcEntry::execute(..)",
parentDict
);
// Read string
string s(is);
// Make sure we stop this entry
//is.putBack(token(token::END_STATEMENT, is.lineNumber()));
// Construct codeDict for codeStream
// must reference parent for stringOps::expand to work nicely.
dictionary codeSubDict;
codeSubDict.add("code", "os << (" + s + ");");
dictionary codeDict(parentDict, codeSubDict);
codeStream::streamingFunctionType function = codeStream::getFunction
(
parentDict,
codeDict
);
// use function to write stream
OStringStream os(is.format());
(*function)(os, parentDict);
// get the entry from this stream
IStringStream resultStream(os.str());
thisEntry.read(parentDict, resultStream);
return true;
}
// ************************************************************************* //

View File

@ -0,0 +1,111 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2011 OpenCFD Ltd.
\\/ 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 <http://www.gnu.org/licenses/>.
Class
Foam::functionEntries::calcEntry
Description
Uses dynamic compilation to provide calculating functionality
for entering dictionary entries.
E.g.
\verbatim
a 1.0;
b 3;
c #calc "$a/$b";
\endverbatim
Note the explicit trailing 0 ('1.0') to force a to be read (and written)
as a floating point number.
Note
Internally this is just a wrapper around codeStream functionality - the
#calc string gets used to construct a dictionary for codeStream.
SourceFiles
calcEntry.C
\*---------------------------------------------------------------------------*/
#ifndef calcEntry_H
#define calcEntry_H
#include "functionEntry.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
class dlLibraryTable;
namespace functionEntries
{
/*---------------------------------------------------------------------------*\
Class calcEntry Declaration
\*---------------------------------------------------------------------------*/
class calcEntry
:
public functionEntry
{
// Private Member Functions
//- Disallow default bitwise copy construct
calcEntry(const calcEntry&);
//- Disallow default bitwise assignment
void operator=(const calcEntry&);
public:
//- Runtime type information
ClassName("calc");
// Member Functions
//- Execute the functionEntry in a sub-dict context
static bool execute
(
const dictionary& parentDict,
primitiveEntry&,
Istream&
);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace functionEntries
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -92,28 +92,13 @@ Foam::dlLibraryTable& Foam::functionEntries::codeStream::libs
} }
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // Foam::functionEntries::codeStream::streamingFunctionType
Foam::functionEntries::codeStream::getFunction
bool Foam::functionEntries::codeStream::execute
( (
const dictionary& parentDict, const dictionary& parentDict,
primitiveEntry& entry, const dictionary& codeDict
Istream& is
) )
{ {
Info<< "Using #codeStream at line " << is.lineNumber()
<< " in file " << parentDict.name() << endl;
dynamicCode::checkSecurity
(
"functionEntries::codeStream::execute(..)",
parentDict
);
// get code dictionary
// must reference parent for stringOps::expand to work nicely
dictionary codeDict("#codeStream", parentDict, is);
// get code, codeInclude, codeOptions // get code, codeInclude, codeOptions
dynamicCodeContext context(codeDict); dynamicCodeContext context(codeDict);
@ -260,6 +245,34 @@ bool Foam::functionEntries::codeStream::execute
<< " in library " << lib << exit(FatalIOError); << " in library " << lib << exit(FatalIOError);
} }
return function;
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::functionEntries::codeStream::execute
(
const dictionary& parentDict,
primitiveEntry& entry,
Istream& is
)
{
Info<< "Using #codeStream at line " << is.lineNumber()
<< " in file " << parentDict.name() << endl;
dynamicCode::checkSecurity
(
"functionEntries::codeStream::execute(..)",
parentDict
);
// get code dictionary
// must reference parent for stringOps::expand to work nicely
dictionary codeDict("#codeStream", parentDict, is);
streamingFunctionType function = getFunction(parentDict, codeDict);
// use function to write stream // use function to write stream
OStringStream os(is.format()); OStringStream os(is.format());
(*function)(os, parentDict); (*function)(os, parentDict);
@ -268,6 +281,7 @@ bool Foam::functionEntries::codeStream::execute
IStringStream resultStream(os.str()); IStringStream resultStream(os.str());
entry.read(parentDict, resultStream); entry.read(parentDict, resultStream);
return true; return true;
} }

View File

@ -103,6 +103,9 @@ class dlLibraryTable;
namespace functionEntries namespace functionEntries
{ {
// Forward declaration of friend classes
class calcEntry;
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class codeStream Declaration Class codeStream Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -123,6 +126,14 @@ class codeStream
//- Helper function: access to dlLibraryTable of Time //- Helper function: access to dlLibraryTable of Time
static dlLibraryTable& libs(const dictionary& dict); static dlLibraryTable& libs(const dictionary& dict);
//- Construct, compile, load and return streaming function
static streamingFunctionType getFunction
(
const dictionary& parentDict,
const dictionary& codeDict
);
//- Disallow default bitwise copy construct //- Disallow default bitwise copy construct
codeStream(const codeStream&); codeStream(const codeStream&);
@ -137,6 +148,11 @@ public:
//- Name of the C code template to be used //- Name of the C code template to be used
static const word codeTemplateC; static const word codeTemplateC;
// Related types
//- Declare friendship with the calcEntry class
friend class calcEntry;
//- Runtime type information //- Runtime type information
ClassName("codeStream"); ClassName("codeStream");