codedBase.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2016-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::codedBase
29 
30 Description
31  Base class for function objects and boundary conditions using dynamic code
32  that provides methods for managing loading/unloading/updating
33  of a dynamic library. For these purposes, it uses a dynamicCodeContext
34  object to maintain information about the state.
35 
36  For simple coded objects, the default state management is sufficient.
37  When there are more complicated code segments
38  (eg, functionObjects::codedFunctionObject), the state management
39  must also register these elements as well, starting with an initial
40  setCodeContext() call and followed by append() to register each element.
41 
42 SourceFiles
43  codedBase.C
44 
45 \*---------------------------------------------------------------------------*/
46 
47 #ifndef codedBase_H
48 #define codedBase_H
49 
50 #include "dictionary.H"
51 #include "dynamicCodeContext.H"
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
55 namespace Foam
56 {
57 
58 // Forward Declarations
59 class dynamicCode;
60 class dlLibraryTable;
61 class objectRegistry;
62 
63 /*---------------------------------------------------------------------------*\
64  Class codedBase Declaration
65 \*---------------------------------------------------------------------------*/
66 
67 class codedBase
68 {
69  // Private Data
70 
71  //- The code context
72  dynamicCodeContext context_;
73 
74  //- Previously loaded library
75  mutable fileName oldLibPath_;
76 
77 
78  // Private Member Functions
79 
80  //- Load specified library and execute funcName(true)
81  void* loadLibrary
82  (
83  const fileName& libPath,
84  const std::string& funcName,
85  const dynamicCodeContext& context
86  ) const;
87 
88  //- Execute funcName(false) and unload specified library
89  void unloadLibrary
90  (
91  const fileName& libPath,
92  const std::string& funcName,
93  const dynamicCodeContext& context
94  ) const;
95 
96  //- Create library based on the dynamicCodeContext
97  void createLibrary
98  (
99  dynamicCode& dynCode,
100  const dynamicCodeContext& context
101  ) const;
102 
103 
104 protected:
105 
106  // Protected Member Functions
107 
108  //- Write code-dictionary contents
109  static void writeCodeDict(Ostream& os, const dictionary& dict);
110 
111  //- Return "codeDict" from objectRegistry or read from disk
112  static const dictionary& codeDict
113  (
114  const objectRegistry& obr,
115  const word& dictName = "codeDict"
116  );
117 
118 
119  //- Access to the dynamic code context
121  {
122  return context_;
123  }
124 
125  //- Set code context from a dictionary
126  void setCodeContext(const dictionary& dict);
127 
128  //- Add content to SHA1 hashing
129  void append(const std::string& str);
130 
131 
132  //- Update library as required, using the given context
133  void updateLibrary
134  (
135  const word& name,
136  const dynamicCodeContext& context
137  ) const;
138 
139  //- Update library as required, using the given code dictionary
140  //- to use for the context
141  void updateLibrary
142  (
143  const word& name,
144  const dictionary& dict
145  ) const;
146 
147  //- Update library as required, using the predefined context
148  //- or use the codeDict() to generate one
149  void updateLibrary(const word& name) const;
150 
151 
152  //- Mutable access to the loaded dynamic libraries
153  virtual dlLibraryTable& libs() const = 0;
154 
155  // Return a description (type + name) for the output
156  virtual string description() const = 0;
157 
158  // Clear any redirected objects
159  virtual void clearRedirect() const = 0;
160 
161  // Get the dictionary to initialize the codeContext
162  virtual const dictionary& codeDict() const = 0;
163 
164  //- Adapt the context for the current object
165  virtual void prepare
166  (
167  dynamicCode& dynCode,
168  const dynamicCodeContext& context
169  ) const = 0;
170 
171 
172  //- No copy construct
173  codedBase(const codedBase&) = delete;
174 
175  //- No copy assignment
176  void operator=(const codedBase&) = delete;
177 
178 
179 public:
180 
181  //- Runtime type information
182  ClassName("codedBase");
183 
184 
185  // Constructors
186 
187  //- Default construct
188  codedBase() = default;
189 
190 
191  //- Destructor
192  virtual ~codedBase() = default;
193 };
194 
195 
196 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
197 
198 } // End namespace Foam
199 
200 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
201 
202 #endif
203 
204 // ************************************************************************* //
Foam::codedBase::append
void append(const std::string &str)
Add content to SHA1 hashing.
Definition: codedBase.C:347
Foam::dlLibraryTable
A table of dynamically loaded libraries.
Definition: dlLibraryTable.H:63
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::codedBase::writeCodeDict
static void writeCodeDict(Ostream &os, const dictionary &dict)
Write code-dictionary contents.
Definition: codedBase.C:88
Foam::codedBase::codeDict
virtual const dictionary & codeDict() const =0
Foam::dynamicCode
Tools for handling dynamic code compilation.
Definition: dynamicCode.H:59
Foam::codedBase::clearRedirect
virtual void clearRedirect() const =0
Foam::dynamicCodeContext
Encapsulation of dynamic code dictionaries.
Definition: dynamicCodeContext.H:53
dictName
const word dictName("faMeshDefinition")
Foam::codedBase
Base class for function objects and boundary conditions using dynamic code that provides methods for ...
Definition: codedBase.H:66
dynamicCodeContext.H
Foam::codedBase::setCodeContext
void setCodeContext(const dictionary &dict)
Set code context from a dictionary.
Definition: codedBase.C:341
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::codedBase::prepare
virtual void prepare(dynamicCode &dynCode, const dynamicCodeContext &context) const =0
Adapt the context for the current object.
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::codedBase::codeContext
dynamicCodeContext & codeContext()
Access to the dynamic code context.
Definition: codedBase.H:119
Foam::codedBase::ClassName
ClassName("codedBase")
Runtime type information.
os
OBJstream os(runTime.globalPath()/outputName)
Foam::codedBase::libs
virtual dlLibraryTable & libs() const =0
Mutable access to the loaded dynamic libraries.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::codedBase::codedBase
codedBase()=default
Default construct.
Foam::codedBase::description
virtual string description() const =0
dictionary.H
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::codedBase::~codedBase
virtual ~codedBase()=default
Destructor.
Foam::codedBase::operator=
void operator=(const codedBase &)=delete
No copy assignment.
Foam::codedBase::updateLibrary
void updateLibrary(const word &name, const dynamicCodeContext &context) const
Update library as required, using the given context.
Definition: codedBase.C:354