diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files
index 3e52499e25..6e169d64bc 100644
--- a/src/OpenFOAM/Make/files
+++ b/src/OpenFOAM/Make/files
@@ -193,6 +193,7 @@ dll = db/dynamicLibrary
$(dll)/dlLibraryTable/dlLibraryTable.C
$(dll)/dynamicCode/dynamicCode.C
$(dll)/dynamicCode/dynamicCodeContext.C
+$(dll)/codedBase/codedBase.C
db/functionObjects/functionObject/functionObject.C
db/functionObjects/functionObjectList/functionObjectList.C
diff --git a/src/OpenFOAM/db/dynamicLibrary/codedBase/codedBase.C b/src/OpenFOAM/db/dynamicLibrary/codedBase/codedBase.C
new file mode 100644
index 0000000000..c899b97b59
--- /dev/null
+++ b/src/OpenFOAM/db/dynamicLibrary/codedBase/codedBase.C
@@ -0,0 +1,288 @@
+/*---------------------------------------------------------------------------*\
+ ========= |
+ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
+ \\ / O peration |
+ \\ / A nd | Copyright (C) 2011 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 "codedBase.H"
+#include "SHA1Digest.H"
+#include "dynamicCode.H"
+#include "dynamicCodeContext.H"
+#include "dlLibraryTable.H"
+#include "PstreamReduceOps.H"
+#include "OSspecific.H"
+
+// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
+
+void* Foam::codedBase::loadLibrary
+(
+ const fileName& libPath,
+ const string& globalFuncName,
+ const dictionary& contextDict
+) const
+{
+ void* lib = 0;
+
+ // avoid compilation by loading an existing library
+ if (!libPath.empty())
+ {
+ if (libs().open(libPath, false))
+ {
+ lib = libs().findLibrary(libPath);
+
+ // verify the loaded version and unload if needed
+ if (lib)
+ {
+ // provision for manual execution of code after loading
+ if (dlSymFound(lib, globalFuncName))
+ {
+ loaderFunctionType function =
+ reinterpret_cast
+ (
+ dlSym(lib, globalFuncName)
+ );
+
+ if (function)
+ {
+ (*function)(true); // force load
+ }
+ else
+ {
+ FatalIOErrorIn
+ (
+ "codedBase::updateLibrary()",
+ contextDict
+ ) << "Failed looking up symbol " << globalFuncName
+ << nl << "from " << libPath << exit(FatalIOError);
+ }
+ }
+ else
+ {
+ FatalIOErrorIn
+ (
+ "codedBase::loadLibrary()",
+ contextDict
+ ) << "Failed looking up symbol " << globalFuncName << nl
+ << "from " << libPath << exit(FatalIOError);
+
+ lib = 0;
+ if (!libs().close(libPath, false))
+ {
+ FatalIOErrorIn
+ (
+ "codedBase::loadLibrary()",
+ contextDict
+ ) << "Failed unloading library "
+ << libPath
+ << exit(FatalIOError);
+ }
+ }
+ }
+ }
+ }
+
+ return lib;
+}
+
+
+void Foam::codedBase::unloadLibrary
+(
+ const fileName& libPath,
+ const string& globalFuncName,
+ const dictionary& contextDict
+) const
+{
+ void* lib = 0;
+
+ if (libPath.empty())
+ {
+ return;
+ }
+
+ lib = libs().findLibrary(libPath);
+
+ if (!lib)
+ {
+ return;
+ }
+
+ // provision for manual execution of code before unloading
+ if (dlSymFound(lib, globalFuncName))
+ {
+ loaderFunctionType function =
+ reinterpret_cast
+ (
+ dlSym(lib, globalFuncName)
+ );
+
+ if (function)
+ {
+ (*function)(false); // force unload
+ }
+ else
+ {
+ FatalIOErrorIn
+ (
+ "codedBase::unloadLibrary()",
+ contextDict
+ ) << "Failed looking up symbol " << globalFuncName << nl
+ << "from " << libPath << exit(FatalIOError);
+ }
+ }
+
+ if (!libs().close(libPath, false))
+ {
+ FatalIOErrorIn
+ (
+ "codedBase::updateLibrary()",
+ contextDict
+ ) << "Failed unloading library " << libPath
+ << exit(FatalIOError);
+ }
+}
+
+
+// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
+
+void Foam::codedBase::createLibrary
+(
+ dynamicCode& dynCode,
+ const dynamicCodeContext& context
+) const
+{
+ bool create = Pstream::master();
+
+ if (create)
+ {
+ // Write files for new library
+ if (!dynCode.upToDate(context))
+ {
+ // filter with this context
+ dynCode.reset(context);
+
+ this->prepare(dynCode, context);
+
+ if (!dynCode.copyOrCreateFiles(true))
+ {
+ FatalIOErrorIn
+ (
+ "codedBase::createLibrary(..)",
+ context.dict()
+ ) << "Failed writing files for" << nl
+ << dynCode.libRelPath() << nl
+ << exit(FatalIOError);
+ }
+ }
+
+ if (!dynCode.wmakeLibso())
+ {
+ FatalIOErrorIn
+ (
+ "codedBase::createLibrary(..)",
+ context.dict()
+ ) << "Failed wmake " << dynCode.libRelPath() << nl
+ << exit(FatalIOError);
+ }
+ }
+
+
+ // all processes must wait for compile to finish
+ reduce(create, orOp());
+}
+
+
+void Foam::codedBase::updateLibrary
+(
+ const word& redirectType
+) const
+{
+ const dictionary& dict = this->codeDict();
+
+ dynamicCode::checkSecurity
+ (
+ "codedBase::updateLibrary()",
+ dict
+ );
+
+ dynamicCodeContext context(dict);
+
+ // codeName: redirectType + _
+ // codeDir : redirectType
+ dynamicCode dynCode
+ (
+ redirectType + context.sha1().str(true),
+ redirectType
+ );
+ const fileName libPath = dynCode.libPath();
+
+
+ // the correct library was already loaded => we are done
+ if (libs().findLibrary(libPath))
+ {
+ return;
+ }
+
+ Info<< "Using dynamicCode for " << this->description().c_str()
+ << " at line " << dict.startLineNumber()
+ << " in " << dict.name() << endl;
+
+
+ // remove instantiation of fvPatchField provided by library
+ this->clearRedirect();
+
+ // may need to unload old library
+ unloadLibrary
+ (
+ oldLibPath_,
+ dynamicCode::libraryBaseName(oldLibPath_),
+ context.dict()
+ );
+
+ // try loading an existing library (avoid compilation when possible)
+ if (!loadLibrary(libPath, dynCode.codeName(), context.dict()))
+ {
+ createLibrary(dynCode, context);
+
+ loadLibrary(libPath, dynCode.codeName(), context.dict());
+ }
+
+ // retain for future reference
+ oldLibPath_ = libPath;
+}
+
+
+// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
+
+Foam::codedBase::codedBase()
+{}
+
+
+// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
+
+Foam::codedBase::~codedBase()
+{}
+
+
+// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
+
+
+// ************************************************************************* //
diff --git a/src/OpenFOAM/db/dynamicLibrary/codedBase/codedBase.H b/src/OpenFOAM/db/dynamicLibrary/codedBase/codedBase.H
new file mode 100644
index 0000000000..391c5267c0
--- /dev/null
+++ b/src/OpenFOAM/db/dynamicLibrary/codedBase/codedBase.H
@@ -0,0 +1,142 @@
+/*---------------------------------------------------------------------------*\
+ ========= |
+ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
+ \\ / O peration |
+ \\ / A nd | Copyright (C) 2011 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 .
+
+Class
+ Foam::codedBase
+
+Description
+ Base class for function objects and boundary conditions using dynamic code
+
+SourceFiles
+ codedBase.C
+
+\*---------------------------------------------------------------------------*/
+
+#ifndef codedBase_H
+#define codedBase_H
+
+#include "dictionary.H"
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+namespace Foam
+{
+
+// Forward declaration of classes
+class dynamicCode;
+class dynamicCodeContext;
+class dlLibraryTable;
+
+/*---------------------------------------------------------------------------*\
+ Class codedBase Declaration
+\*---------------------------------------------------------------------------*/
+
+class codedBase
+{
+ // Private data
+
+ //- Previously loaded library
+ mutable fileName oldLibPath_;
+
+ // Private Member Functions
+
+ //- Global loader/unloader function type
+ typedef void (*loaderFunctionType)(bool);
+
+ //- Load specified library and execute globalFuncName(true)
+ void* loadLibrary
+ (
+ const fileName& libPath,
+ const string& globalFuncName,
+ const dictionary& contextDict
+ ) const;
+
+ //- Execute globalFuncName(false) and unload specified library
+ void unloadLibrary
+ (
+ const fileName& libPath,
+ const string& globalFuncName,
+ const dictionary& contextDict
+ ) const;
+
+ //- Create library based on the dynamicCodeContext
+ void createLibrary(dynamicCode&, const dynamicCodeContext&) const;
+
+ //- Disallow default bitwise copy construct
+ codedBase(const codedBase&);
+
+ //- Disallow default bitwise assignment
+ void operator=(const codedBase&);
+
+
+protected:
+
+ //- Update library as required
+ void updateLibrary
+ (
+ const word& redirectType
+ ) const;
+
+ //- get the loaded dynamic libraries
+ virtual dlLibraryTable& libs() const = 0;
+
+ //- adapt the context for the current object
+ virtual void prepare
+ (
+ dynamicCode&,
+ const dynamicCodeContext &
+ ) const = 0;
+
+ // Return a description (type + name) for the output
+ virtual string description() const = 0;
+
+ // Clear any redirected objects
+ virtual void clearRedirect() const = 0;
+
+ // Get the dictionary to initialize the codeContext
+ virtual const dictionary& codeDict() const = 0;
+
+
+public:
+
+ // Constructors
+
+ //- Construct null
+ codedBase();
+
+
+ //- Destructor
+ virtual ~codedBase();
+
+};
+
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+} // End namespace Foam
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+#endif
+
+// ************************************************************************* //
diff --git a/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.C b/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.C
index 4b131118d2..acffe1a9b3 100644
--- a/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.C
+++ b/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.C
@@ -26,19 +26,10 @@ License
#include "codedFixedValueFvPatchField.H"
#include "addToRunTimeSelectionTable.H"
#include "fvPatchFieldMapper.H"
-#include "surfaceFields.H"
#include "volFields.H"
-#include "dlLibraryTable.H"
-#include "IFstream.H"
-#include "OFstream.H"
-#include "SHA1Digest.H"
#include "dynamicCode.H"
#include "dynamicCodeContext.H"
#include "stringOps.H"
-#include "IOdictionary.H"
-
-#include
-#include
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@@ -53,142 +44,6 @@ const Foam::word Foam::codedFixedValueFvPatchField::codeTemplateH
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
-template
-void* Foam::codedFixedValueFvPatchField::loadLibrary
-(
- const fileName& libPath,
- const string& globalFuncName,
- const dictionary& contextDict
-) const
-{
- void* lib = 0;
-
- // avoid compilation by loading an existing library
- if (!libPath.empty())
- {
- dlLibraryTable& libs = const_cast