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-2019 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 segements
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 Ostream;
60 class dynamicCode;
61 class dlLibraryTable;
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  // Data Types
79 
80  //- Global loader/unloader function type
81  // Called with true on load, false on unload.
82  typedef void (*loaderType)(bool);
83 
84 
85  // Private Member Functions
86 
87  //- Load specified library and execute funcName(true)
88  void* loadLibrary
89  (
90  const fileName& libPath,
91  const std::string& funcName,
92  const dynamicCodeContext& context
93  ) const;
94 
95  //- Execute funcName(false) and unload specified library
96  void unloadLibrary
97  (
98  const fileName& libPath,
99  const std::string& funcName,
100  const dynamicCodeContext& context
101  ) const;
102 
103  //- Create library based on the dynamicCodeContext
104  void createLibrary
105  (
106  dynamicCode& dynCode,
107  const dynamicCodeContext& context
108  ) const;
109 
110  //- No copy construct
111  codedBase(const codedBase&) = delete;
112 
113  //- No copy assignment
114  void operator=(const codedBase&) = delete;
115 
116 
117 protected:
118 
119  //- Write code-dictionary contents
120  static void writeCodeDict(Ostream& os, const dictionary& dict);
121 
122 
123  // Protected Member Functions
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  //- Get the loaded dynamic libraries
152  virtual dlLibraryTable& libs() const = 0;
153 
154  //- Adapt the context for the current object
155  virtual void prepare
156  (
157  dynamicCode& dynCode,
158  const dynamicCodeContext& context
159  ) const = 0;
160 
161  // Return a description (type + name) for the output
162  virtual string description() const = 0;
163 
164  // Clear any redirected objects
165  virtual void clearRedirect() const = 0;
166 
167  // Get the dictionary to initialize the codeContext
168  virtual const dictionary& codeDict() const = 0;
169 
170 
171 public:
172 
173  //- Runtime type information
174  ClassName("codedBase");
175 
176 
177  // Constructors
178 
179  //- Construct null
180  codedBase() = default;
181 
182 
183  //- Destructor
184  virtual ~codedBase() = default;
185 };
186 
187 
188 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
189 
190 } // End namespace Foam
191 
192 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
193 
194 #endif
195 
196 // ************************************************************************* //
Foam::codedBase::append
void append(const std::string &str)
Add content to SHA1 hashing.
Definition: codedBase.C:344
Foam::dlLibraryTable
A table of dynamically loaded libraries.
Definition: dlLibraryTable.H:51
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:66
dynamicCodeContext.H
Foam::codedBase::setCodeContext
void setCodeContext(const dictionary &dict)
Set code context from a dictionary.
Definition: codedBase.C:338
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
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
Get the loaded dynamic libraries.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::codedBase::codedBase
codedBase()=default
Construct null.
Foam::codedBase::description
virtual string description() const =0
dictionary.H
bool
bool
Definition: EEqn.H:20
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::updateLibrary
void updateLibrary(const word &name, const dynamicCodeContext &context) const
Update library as required, using the given context.
Definition: codedBase.C:351