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 dynamicCodeContext_H
39 #define 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  //- Considered valid if not using dictionary::null as the context
123  bool valid() const noexcept;
124 
125  //- Set code context from a dictionary
126  void setCodeContext(const dictionary& dict);
127 
128  //- Return the parent dictionary context
129  const dictionary& dict() const noexcept
130  {
131  return dict_.get();
132  }
133 
134  //- The code options (Make/options)
135  const string& options() const noexcept
136  {
137  return codeOptions_;
138  }
139 
140  //- The code libs (LIB_LIBS)
141  const string& libs() const noexcept
142  {
143  return codeLibs_;
144  }
145 
146  //- The code includes
147  const string& include() const noexcept
148  {
149  return codeInclude_;
150  }
151 
152  //- The local (file-scope) code
153  const string& localCode() const noexcept
154  {
155  return localCode_;
156  }
157 
158  //- The code
159  const string& code() const noexcept
160  {
161  return code_;
162  }
163 
164  //- The SHA1 calculated from options, libs, include, code, etc.
165  const SHA1& sha1() const noexcept
166  {
167  return sha1_;
168  }
169 
170  //- Add content to SHA1 hashing
171  void append(const std::string& str)
172  {
173  sha1_.append(str);
174  }
175 
176 
177  // Reading
178 
179  //- Locate literal dictionary entry, nullptr if not found
180  const entry* findEntry(const word& key) const;
181 
182  //- Read string entry from context dictionary
183  //- append content to SHA1 hashing and add line number etc.
184  //
185  // The string is cleared before reading.
186  bool readEntry
187  (
188  const word& key,
189  string& str,
190  bool mandatory = true,
191  bool withLineNum = true
192  );
193 
194  //- Read optional string entry from context dictionary,
195  //- append content to SHA1 hashing and add line number etc.
196  //
197  // The string is cleared before reading.
198  bool readIfPresent
199  (
200  const word& key,
201  string& str,
202  bool withLineNum = true
203  );
204 
205 
206  // Member Operators
207 
208  //- Cast to dictionary
209  operator const dictionary&() const noexcept
210  {
211  return dict_.get();
212  }
213 };
214 
215 
216 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
217 
218 } // End namespace Foam
219 
220 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
221 
222 #endif
223 
224 // ************************************************************************* //
Foam::dynamicCodeContext::localCode
const string & localCode() const noexcept
The local (file-scope) code.
Definition: dynamicCodeContext.H:152
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::SHA1::append
SHA1 & append(const char *str)
Append data for processing.
Definition: SHA1I.H:62
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::dynamicCodeContext::readEntry
bool readEntry(const word &key, string &str, bool mandatory=true, bool withLineNum=true)
Definition: dynamicCodeContext.C:110
Foam::SHA1
Functions to compute SHA1 message digest according to the NIST specification FIPS-180-1.
Definition: SHA1.H:60
Foam::dynamicCodeContext::valid
bool valid() const noexcept
Considered valid if not using dictionary::null as the context.
Definition: dynamicCodeContext.C:97
Foam::dynamicCodeContext
Encapsulation of dynamic code dictionaries.
Definition: dynamicCodeContext.H:53
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Foam::dynamicCodeContext::readIfPresent
bool readIfPresent(const word &key, string &str, bool withLineNum=true)
Definition: dynamicCodeContext.C:154
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:107
Foam::dynamicCodeContext::dict
const dictionary & dict() const noexcept
Return the parent dictionary context.
Definition: dynamicCodeContext.H:128
Foam::dynamicCodeContext::dynamicCodeContext
dynamicCodeContext()
Default construct.
Definition: dynamicCodeContext.C:81
Foam::dynamicCodeContext::append
void append(const std::string &str)
Add content to SHA1 hashing.
Definition: dynamicCodeContext.H:170
Foam::dynamicCodeContext::sha1
const SHA1 & sha1() const noexcept
The SHA1 calculated from options, libs, include, code, etc.
Definition: dynamicCodeContext.H:164
Foam::dynamicCodeContext::findEntry
const entry * findEntry(const word &key) const
Locate literal dictionary entry, nullptr if not found.
Definition: dynamicCodeContext.C:103
Foam::dynamicCodeContext::libs
const string & libs() const noexcept
The code libs (LIB_LIBS)
Definition: dynamicCodeContext.H:140
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::dynamicCodeContext::setCodeContext
void setCodeContext(const dictionary &dict)
Set code context from a dictionary.
Definition: dynamicCodeContext.C:164
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::dynamicCodeContext::include
const string & include() const noexcept
The code includes.
Definition: dynamicCodeContext.H:146
Foam::dynamicCodeContext::inplaceExpand
static void inplaceExpand(string &str, const dictionary &dict)
Cleanup string and expand with dictionary parameters.
Definition: dynamicCodeContext.C:36
Foam::dynamicCodeContext::addLineDirective
static unsigned addLineDirective(string &code, label lineNum, const string &file)
Prefix a #line directive to code.
Definition: dynamicCodeContext.C:47
dictionary.H
Foam::dynamicCodeContext::code
const string & code() const noexcept
The code.
Definition: dynamicCodeContext.H:158
SHA1.H
Foam::dynamicCodeContext::options
const string & options() const noexcept
The code options (Make/options)
Definition: dynamicCodeContext.H:134