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-2023 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 Foam_codedBase_H
48 #define Foam_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 
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 public:
173 
174  //- Runtime type information
175  ClassName("codedBase");
176 
177 
178  // Generated Methods
179 
180  //- No copy construct
181  codedBase(const codedBase&) = delete;
182 
183  //- No copy assignment
184  void operator=(const codedBase&) = delete;
185 
186 
187  // Constructors
188 
189  //- Default construct
190  codedBase() = default;
191 
192 
193  //- Destructor
194  virtual ~codedBase() = default;
195 };
196 
197 
198 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
199 
200 } // End namespace Foam
201 
202 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
203 
204 #endif
205 
206 // ************************************************************************* //
dictionary dict
A class for handling file names.
Definition: fileName.H:72
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
virtual ~codedBase()=default
Destructor.
void operator=(const codedBase &)=delete
No copy assignment.
const word dictName("faMeshDefinition")
virtual void prepare(dynamicCode &dynCode, const dynamicCodeContext &context) const =0
Adapt the context for the current object.
void append(const std::string &str)
Add content to SHA1 hashing.
Definition: codedBase.C:276
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
Base class for function objects and boundary conditions using dynamic code that provides methods for ...
Definition: codedBase.H:62
A table of dynamically loaded libraries.
void setCodeContext(const dictionary &dict)
Set code context from a dictionary.
Definition: codedBase.C:270
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
Tools for handling dynamic code compilation.
Definition: dynamicCode.H:56
Encapsulation of dynamic code dictionaries.
void updateLibrary(const word &name, const dynamicCodeContext &context) const
Update library as required, using the given context.
Definition: codedBase.C:283
virtual string description() const =0
ClassName("codedBase")
Runtime type information.
dynamicCodeContext & codeContext()
Access to the dynamic code context.
Definition: codedBase.H:131
virtual dlLibraryTable & libs() const =0
Mutable access to the loaded dynamic libraries.
Registry of regIOobjects.
virtual void clearRedirect() const =0
codedBase()=default
Default construct.
static void writeCodeDict(Ostream &os, const dictionary &dict)
Write code-dictionary contents.
Definition: codedBase.C:81
virtual const dictionary & codeDict() const =0
Namespace for OpenFOAM.