/*---------------------------------------------------------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org \\ / A nd | Copyright (C) 2011-2026 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 . \*---------------------------------------------------------------------------*/ #include "codeStream.H" #include "dynamicCode.H" #include "Time.H" #include "OSspecific.H" #include "PstreamReduceOps.H" #include "addToRunTimeSelectionTable.H" #include "addToMemberFunctionSelectionTable.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // namespace Foam { namespace functionEntries { defineFunctionTypeNameAndDebug(codeStream, 0); addToRunTimeSelectionTable(functionEntry, codeStream, dictionary); addToMemberFunctionSelectionTable ( functionEntry, codeStream, execute, primitiveEntryIstream ); } } const Foam::wordList Foam::functionEntries::codeStream::codeKeys ( {"code", "codeInclude", "localCode"} ); const Foam::wordList Foam::functionEntries::codeStream::codeDictVars ( {"dict", word::null, word::null} ); const Foam::word Foam::functionEntries::codeStream::codeOptions ( "codeStreamOptions" ); const Foam::wordList Foam::functionEntries::codeStream::compileFiles { "codeStreamTemplate.C" }; // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // bool Foam::functionEntries::codeStream::masterOnlyRead ( const word& typeName, const dictionary& dict ) { const dictionary& topDict = dict.topDict(); if (debug) { Pout<< typeName << " : dictionary:" << dict.name() << " master-only-reading:" << topDict.global() << endl; } return topDict.global(); } Foam::string Foam::functionEntries::codeStream::codeString ( const word& typeName, const word& templateFunctionName, const label index, const dictionary& contextDict, Istream& is ) { // Construct code string for codeStream using the context dictionary for // string expansion and variable substitution const dictionary codeDict(typeName, contextDict, is); if (codeDict.found("codeInclude")) { IOWarningInFunction(is) << "codeInclude entry not supported within #codeBlock, " "use #codeInclude instead." << endl; } return ( templateFunctionName + '(' + Foam::name(index) + ")\n" "{\n" " #line " + Foam::name(codeDict.lookup("code").lineNumber()) + " \"" + codeDict.name() + "\"\n" + codeDict.lookup("code") + "\n}\n\n" ); } Foam::string Foam::functionEntries::codeStream::codeString ( const label index, const dictionary& contextDict, Istream& is ) { return codeString ( typeName, "CODE_BLOCK_STREAM_FUNCTION", index, contextDict, is ); } void* Foam::functionEntries::codeStream::compile ( const word& typeName, const dictionary& contextDict, const dictionary& codeDict, const word& codeOptions, const wordList& compileFiles, word& codeName ) { // Get code, codeInclude, ... // codeName: codeStream + _ // codeDir : _ dynamicCode dynCode ( contextDict, codeDict, typeName.remove('#'), word::null, codeKeys, codeDictVars, codeOptions, compileFiles, wordList::null() ); // Load library if not already loaded // Version information is encoded in the libPath (encoded with the SHA1) const fileName libPath = dynCode.libPath(); // See if library is loaded void* lib = libs.findLibrary(libPath); if (debug && !lib) { Info<< "Using " << typeName << " with " << libPath << endl; } // Nothing loaded // avoid compilation if possible by loading an existing library if (!lib) { lib = dynCode.loadLibrary(libPath); } // Create library if required and load if (!lib) { dynCode.createLibrary ( contextDict, masterOnlyRead(typeName, contextDict) ); lib = dynCode.loadLibrary(libPath); } if (!lib) { FatalIOErrorInFunction(contextDict) << "Failed loading library " << libPath << nl << "Did you add all libraries to the 'libs' entry" << " in system/controlDict?" << exit(FatalIOError); } bool allHaveLib = lib; if (!masterOnlyRead(typeName, contextDict)) { reduce(allHaveLib, andOp()); } if (!allHaveLib) { FatalIOErrorInFunction(contextDict) << "Failed loading library " << libPath << " on some processors." << exit(FatalIOError); } codeName = dynCode.codeSha1Name(); return lib; } Foam::functionEntries::codeStream::streamingFunctionType Foam::functionEntries::codeStream::getFunction ( const dictionary& contextDict, const dictionary& codeDict ) { word codeName; void* lib = compile ( typeName, contextDict, codeDict, codeOptions, compileFiles, codeName ); // Find the function handle in the library const streamingFunctionType function = reinterpret_cast ( dlSym(lib, codeName) ); if (!function) { FatalIOErrorInFunction(contextDict) << "Failed looking up symbol " << codeName << " in library " << lib << exit(FatalIOError); } return function; } Foam::OTstream Foam::functionEntries::codeStream::resultStream ( const dictionary& contextDict, Istream& is ) { if (debug) { Info<< "Using " << typeName << " at line " << is.lineNumber() << " in file " << contextDict.name() << endl; } // Construct codeDict for codeStream using the context dictionary // for string expansion and variable substitution const dictionary codeDict(typeName, contextDict, is); // Compile and link the code library and get the function pointer const streamingFunctionType function = getFunction(contextDict, codeDict); // Use function to write stream OTstream ots(is.name(), is.format()); ots.lineNumber() = is.lineNumber(); (*function)(ots, contextDict); // Return the OTstream containing the results of the code execution return ots; } // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::functionEntries::codeStream::codeStream ( const functionName& functionType, const label lineNumber, const dictionary& dict ) : functionEntry(functionType, lineNumber, dict) {} Foam::functionEntries::codeStream::codeStream ( const label lineNumber, const dictionary& parentDict, Istream& is ) : functionEntry(typeName, lineNumber, parentDict) {} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // bool Foam::functionEntries::codeStream::execute ( dictionary& contextDict, Istream& is ) { return insert(contextDict, resultStream(contextDict, is)); } bool Foam::functionEntries::codeStream::execute ( const dictionary& contextDict, primitiveEntry& contextEntry, Istream& is ) { return insert(contextDict, contextEntry, resultStream(contextDict, is)); } // ************************************************************************* //