From 9fa370f54a9de845fab0f980bbf4f7bb45d82934 Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Wed, 29 Jun 2011 09:56:01 +0100
Subject: [PATCH] ENH: calcEntry: new functionEntry method

---
 src/OpenFOAM/Make/files                       |   1 +
 .../functionEntries/calcEntry/calcEntry.C     |  99 ++++++++++++++++
 .../functionEntries/calcEntry/calcEntry.H     | 111 ++++++++++++++++++
 .../functionEntries/codeStream/codeStream.C   |  50 +++++---
 .../functionEntries/codeStream/codeStream.H   |  16 +++
 5 files changed, 259 insertions(+), 18 deletions(-)
 create mode 100644 src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C
 create mode 100644 src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.H

diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files
index 52de21702b5..1f1e25341e2 100644
--- a/src/OpenFOAM/Make/files
+++ b/src/OpenFOAM/Make/files
@@ -160,6 +160,7 @@ $(dictionaryEntry)/dictionaryEntry.C
 $(dictionaryEntry)/dictionaryEntryIO.C
 
 functionEntries = $(dictionary)/functionEntries
+$(functionEntries)/calcEntry/calcEntry.C
 $(functionEntries)/codeStream/codeStream.C
 $(functionEntries)/functionEntry/functionEntry.C
 $(functionEntries)/includeEntry/includeEntry.C
diff --git a/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C
new file mode 100644
index 00000000000..fceccc4d354
--- /dev/null
+++ b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C
@@ -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;
+}
+
+
+// ************************************************************************* //
diff --git a/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.H b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.H
new file mode 100644
index 00000000000..235210e798d
--- /dev/null
+++ b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.H
@@ -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
+
+// ************************************************************************* //
diff --git a/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C b/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C
index 8eefcb86d92..bab7ac3901b 100644
--- a/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C
+++ b/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C
@@ -92,28 +92,13 @@ Foam::dlLibraryTable& Foam::functionEntries::codeStream::libs
 }
 
 
-// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
-
-bool Foam::functionEntries::codeStream::execute
+Foam::functionEntries::codeStream::streamingFunctionType
+Foam::functionEntries::codeStream::getFunction
 (
     const dictionary& parentDict,
-    primitiveEntry& entry,
-    Istream& is
+    const dictionary& codeDict
 )
 {
-    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
     dynamicCodeContext context(codeDict);
 
@@ -260,6 +245,34 @@ bool Foam::functionEntries::codeStream::execute
             << " 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
     OStringStream os(is.format());
     (*function)(os, parentDict);
@@ -268,6 +281,7 @@ bool Foam::functionEntries::codeStream::execute
     IStringStream resultStream(os.str());
     entry.read(parentDict, resultStream);
 
+
     return true;
 }
 
diff --git a/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.H b/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.H
index 4da11839f13..6c93af86904 100644
--- a/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.H
+++ b/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.H
@@ -103,6 +103,9 @@ class dlLibraryTable;
 namespace functionEntries
 {
 
+// Forward declaration of friend classes
+class calcEntry;
+
 /*---------------------------------------------------------------------------*\
                          Class codeStream Declaration
 \*---------------------------------------------------------------------------*/
@@ -123,6 +126,14 @@ class codeStream
         //- Helper function: access to dlLibraryTable of Time
         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
         codeStream(const codeStream&);
 
@@ -137,6 +148,11 @@ public:
         //- Name of the C code template to be used
         static const word codeTemplateC;
 
+    // Related types
+
+        //- Declare friendship with the calcEntry class
+        friend class calcEntry;
+
 
     //- Runtime type information
     ClassName("codeStream");
-- 
GitLab