dynamicCodeContext.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) 2019-2021 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::dynamicCodeContext
29 
30 Description
31  Encapsulation of dynamic code dictionaries
32 
33 SourceFiles
34  dynamicCodeContext.C
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef Foam_dynamicCodeContext_H
39 #define Foam_dynamicCodeContext_H
40 
41 #include "dictionary.H"
42 #include "SHA1.H"
43 #include <functional>
44 
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 
47 namespace Foam
48 {
49 
50 /*---------------------------------------------------------------------------*\
51  Class dynamicCodeContext Declaration
52 \*---------------------------------------------------------------------------*/
53 
55 {
56  // Private Data
57 
58  //- The parent dictionary context
59  std::reference_wrapper<const dictionary> dict_;
60 
61  //- The SHA1 of the contents
62  SHA1 sha1_;
63 
64  //- The "codeOptions" entry (optional)
65  string codeOptions_;
66 
67  //- The "codeLibs" entry (optional)
68  string codeLibs_;
69 
70  //- The "codeInclude" entry (optional)
71  string codeInclude_;
72 
73  //- The "localCode" entry (optional)
74  string localCode_;
75 
76  //- The "code" entry (optional)
77  string code_;
78 
79 
80 public:
81 
82  // Constructors
83 
84  //- Default construct
86 
87  //- Construct from a dictionary
88  explicit dynamicCodeContext(const dictionary& dict);
89 
90 
91  // Static Member Functions
92 
93  //- Cleanup string and expand with dictionary parameters
94  static void inplaceExpand(string& str, const dictionary& dict);
95 
96  //- Prefix a \#line directive to code.
97  // The input lineNum is 0-based.
98  // Is a no-op if any of the arguments are invalid
99  // (lineNum is negative, code or file are empty)
100  //
101  // \return The change in string length caused by the directive.
102  // This can potentially be used to recover the substring portions.
103  static unsigned addLineDirective
104  (
105  string& code,
106  label lineNum,
107  const string& file
108  );
109 
110  //- Prefix a \#line directive to code.
111  // The name of the dictionary is used for the 'file' name.
112  static unsigned addLineDirective
113  (
114  string& code,
115  label lineNum,
116  const dictionary& dict
117  );
118 
119 
120  // Member Functions
121 
122  //- Not using dummy code context (dictionary::null)
123  bool good() const noexcept;
124 
125  //- Same as good()
126  bool valid() const noexcept { return good(); }
127 
128  //- Set code context from a dictionary
129  void setCodeContext(const dictionary& dict);
130 
131  //- Return the parent dictionary context
132  const dictionary& dict() const noexcept
133  {
134  return dict_.get();
135  }
136 
137  //- The code options (Make/options)
138  const string& options() const noexcept
139  {
140  return codeOptions_;
141  }
142 
143  //- The code libs (LIB_LIBS)
144  const string& libs() const noexcept
145  {
146  return codeLibs_;
147  }
148 
149  //- The code includes
150  const string& include() const noexcept
151  {
152  return codeInclude_;
153  }
154 
155  //- The local (file-scope) code
156  const string& localCode() const noexcept
157  {
158  return localCode_;
159  }
160 
161  //- The code
162  const string& code() const noexcept
163  {
164  return code_;
165  }
166 
167  //- The SHA1 calculated from options, libs, include, code, etc.
168  const SHA1& sha1() const noexcept
169  {
170  return sha1_;
171  }
172 
173  //- Add content to SHA1 hashing
174  void append(const std::string& str)
175  {
176  sha1_.append(str);
177  }
178 
179 
180  // Reading
181 
182  //- Locate literal dictionary entry, nullptr if not found
183  const entry* findEntry(const word& key) const;
184 
185  //- Read string entry from context dictionary
186  //- append content to SHA1 hashing and add line number etc.
187  //
188  // The string is cleared before reading.
189  bool readEntry
190  (
191  const word& key,
192  string& str,
193  bool mandatory = true,
194  bool withLineNum = true
195  );
196 
197  //- Read optional string entry from context dictionary,
198  //- append content to SHA1 hashing and add line number etc.
199  //
200  // The string is cleared before reading.
202  (
203  const word& key,
204  string& str,
205  bool withLineNum = true
206  );
207 
208 
209  // Member Operators
210 
211  //- Cast to dictionary
212  operator const dictionary&() const noexcept
213  {
214  return dict_.get();
215  }
216 };
218 
219 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
220 
221 } // End namespace Foam
222 
223 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
224 
225 #endif
226 
227 // ************************************************************************* //
bool good() const noexcept
Not using dummy code context (dictionary::null)
dynamicCodeContext()
Default construct.
const string & include() const noexcept
The code includes.
const string & localCode() const noexcept
The local (file-scope) code.
void setCodeContext(const dictionary &dict)
Set code context from a dictionary.
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
void append(const std::string &str)
Add content to SHA1 hashing.
const entry * findEntry(const word &key) const
Locate literal dictionary entry, nullptr if not found.
const dictionary & dict() const noexcept
Return the parent dictionary context.
const string & options() const noexcept
The code options (Make/options)
A class for handling words, derived from Foam::string.
Definition: word.H:63
const direction noexcept
Definition: Scalar.H:258
static unsigned addLineDirective(string &code, label lineNum, const string &file)
Prefix a #line directive to code.
const string & code() const noexcept
The code.
Encapsulation of dynamic code dictionaries.
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
bool valid() const noexcept
Same as good()
void append(char c)
Append single character.
Definition: SHA1I.H:46
const string & libs() const noexcept
The code libs (LIB_LIBS)
Functions to compute SHA1 message digest according to the NIST specification FIPS-180-1.
Definition: SHA1.H:56
bool readEntry(const word &key, string &str, bool mandatory=true, bool withLineNum=true)
Read string entry from context dictionary append content to SHA1 hashing and add line number etc...
static void inplaceExpand(string &str, const dictionary &dict)
Cleanup string and expand with dictionary parameters.
Namespace for OpenFOAM.
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:63
const SHA1 & sha1() const noexcept
The SHA1 calculated from options, libs, include, code, etc.
bool readIfPresent(const word &key, string &str, bool withLineNum=true)
Read optional string entry from context dictionary, append content to SHA1 hashing and add line numbe...