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 #include "fileName.H"
53 
54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
55 
56 namespace Foam
57 {
58 
59 // Forward Declarations
60 class Ostream;
61 class dynamicCode;
62 class dlLibraryTable;
63 
64 /*---------------------------------------------------------------------------*\
65  Class codedBase Declaration
66 \*---------------------------------------------------------------------------*/
67 
68 class codedBase
69 {
70  // Private Data
71 
72  //- The code context
73  dynamicCodeContext context_;
74 
75  //- Previously loaded library
76  mutable fileName oldLibPath_;
77 
78 
79  // Private Member Functions
80 
81  //- Load specified library and execute funcName(true)
82  void* loadLibrary
83  (
84  const fileName& libPath,
85  const std::string& funcName,
86  const dynamicCodeContext& context
87  ) const;
88 
89  //- Execute funcName(false) and unload specified library
90  void unloadLibrary
91  (
92  const fileName& libPath,
93  const std::string& funcName,
94  const dynamicCodeContext& context
95  ) const;
96 
97  //- Create library based on the dynamicCodeContext
98  void createLibrary
99  (
100  dynamicCode& dynCode,
101  const dynamicCodeContext& context
102  ) const;
103 
104 
105 protected:
106 
107  //- Write code-dictionary contents
108  static void writeCodeDict(Ostream& os, const dictionary& dict);
109 
110 
111  // Protected Member Functions
112 
113  //- Set code context from a dictionary
114  void setCodeContext(const dictionary& dict);
115 
116  //- Add content to SHA1 hashing
117  void append(const std::string& str);
118 
119 
120  //- Update library as required, using the given context
121  void updateLibrary
122  (
123  const word& name,
124  const dynamicCodeContext& context
125  ) const;
126 
127  //- Update library as required, using the given code dictionary
128  //- to use for the context
129  void updateLibrary
130  (
131  const word& name,
132  const dictionary& dict
133  ) const;
134 
135  //- Update library as required, using the predefined context
136  //- or use the codeDict() to generate one
137  void updateLibrary(const word& name) const;
138 
139  //- Mutable access to the loaded dynamic libraries
140  virtual dlLibraryTable& libs() const = 0;
141 
142  //- Adapt the context for the current object
143  virtual void prepare
144  (
145  dynamicCode& dynCode,
146  const dynamicCodeContext& context
147  ) const = 0;
148 
149  // Return a description (type + name) for the output
150  virtual string description() const = 0;
151 
152  // Clear any redirected objects
153  virtual void clearRedirect() const = 0;
154 
155  // Get the dictionary to initialize the codeContext
156  virtual const dictionary& codeDict() const = 0;
157 
158 
159  //- No copy construct
160  codedBase(const codedBase&) = delete;
161 
162  //- No copy assignment
163  void operator=(const codedBase&) = delete;
164 
165 
166 public:
167 
168  //- Runtime type information
169  ClassName("codedBase");
170 
171 
172  // Constructors
173 
174  //- Default construct
175  codedBase() = default;
176 
177 
178  //- Destructor
179  virtual ~codedBase() = default;
180 };
181 
182 
183 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
184 
185 } // End namespace Foam
186 
187 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
188 
189 #endif
190 
191 // ************************************************************************* //
Foam::codedBase::append
void append(const std::string &str)
Add content to SHA1 hashing.
Definition: codedBase.C:308
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:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::codedBase::writeCodeDict
static void writeCodeDict(Ostream &os, const dictionary &dict)
Write code-dictionary contents.
Definition: codedBase.C:80
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
Foam::codedBase
Base class for function objects and boundary conditions using dynamic code that provides methods for ...
Definition: codedBase.H:67
dynamicCodeContext.H
Foam::codedBase::setCodeContext
void setCodeContext(const dictionary &dict)
Set code context from a dictionary.
Definition: codedBase.C:302
Foam::codedBase::prepare
virtual void prepare(dynamicCode &dynCode, const dynamicCodeContext &context) const =0
Adapt the context for the current object.
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
fileName.H
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:121
Foam::codedBase::ClassName
ClassName("codedBase")
Runtime type information.
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::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:315