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 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 <functional>
42 #include "dictionary.H"
43 #include "SHA1.H"
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  //- Optional "codeOptions" entry
65  string options_;
66 
67  //- Optional "codeLibs" entry
68  string libs_;
69 
70  //- Optional "codeInclude" entry
71  string include_;
72 
73  //- Optional "code" entry
74  string code_;
75 
76  //- Optional "localCode" entry
77  string localCode_;
78 
79 
80 public:
81 
82  // Constructors
83 
84  //- Construct null
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& code, 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 fileName& 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;
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
130  {
131  return dict_.get();
132  }
133 
134  //- Return the code-includes
135  const string& include() const
136  {
137  return include_;
138  }
139 
140  //- Return the code-options
141  const string& options() const
142  {
143  return options_;
144  }
145 
146  //- Return the code-libs
147  const string& libs() const
148  {
149  return libs_;
150  }
151 
152  //- Return the code
153  const string& code() const
154  {
155  return code_;
156  }
157 
158  //- Return the local (file-scope) code
159  const string& localCode() const
160  {
161  return localCode_;
162  }
163 
164  //- Return SHA1 calculated from options, libs, include, code
165  const SHA1& sha1() const
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  // Member Operators
178 
179  //- Cast to dictionary
180  operator const dictionary&() const
181  {
182  return dict_.get();
183  }
184 
185 };
186 
187 
188 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
189 
190 } // End namespace Foam
191 
192 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
193 
194 #endif
195 
196 // ************************************************************************* //
Foam::dynamicCodeContext::libs
const string & libs() const
Return the code-libs.
Definition: dynamicCodeContext.H:146
Foam::SHA1::append
SHA1 & append(const char *str)
Append data for processing.
Definition: SHA1I.H:63
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::dynamicCodeContext::dict
const dictionary & dict() const
Return the parent dictionary context.
Definition: dynamicCodeContext.H:128
Foam::SHA1
Functions to compute SHA1 message digest according to the NIST specification FIPS-180-1.
Definition: SHA1.H:60
Foam::dynamicCodeContext
Encapsulation of dynamic code dictionaries.
Definition: dynamicCodeContext.H:53
Foam::dynamicCodeContext::include
const string & include() const
Return the code-includes.
Definition: dynamicCodeContext.H:134
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:81
Foam::dynamicCodeContext::dynamicCodeContext
dynamicCodeContext()
Construct null.
Definition: dynamicCodeContext.C:81
Foam::dynamicCodeContext::append
void append(const std::string &str)
Add content to SHA1 hashing.
Definition: dynamicCodeContext.H:170
Foam::dynamicCodeContext::inplaceExpand
static void inplaceExpand(string &code, const dictionary &dict)
Cleanup string and expand with dictionary parameters.
Definition: dynamicCodeContext.C:36
Foam::dynamicCodeContext::code
const string & code() const
Return the code.
Definition: dynamicCodeContext.H:152
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::dynamicCodeContext::setCodeContext
void setCodeContext(const dictionary &dict)
Set code context from a dictionary.
Definition: dynamicCodeContext.C:103
Foam::dynamicCodeContext::options
const string & options() const
Return the code-options.
Definition: dynamicCodeContext.H:140
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::dynamicCodeContext::localCode
const string & localCode() const
Return the local (file-scope) code.
Definition: dynamicCodeContext.H:158
dictionary.H
Foam::dynamicCodeContext::sha1
const SHA1 & sha1() const
Return SHA1 calculated from options, libs, include, code.
Definition: dynamicCodeContext.H:164
SHA1.H
Foam::dynamicCodeContext::valid
bool valid() const
Considered valid if not using dictionary::null as the context.
Definition: dynamicCodeContext.C:97
Foam::dynamicCodeContext::addLineDirective
static unsigned addLineDirective(string &code, label lineNum, const fileName &file)
Prefix a #line directive to code.
Definition: dynamicCodeContext.C:47