dynamicCodeContext.C
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 \*---------------------------------------------------------------------------*/
28 
30 #include "stringOps.H"
31 #include "OSHA1stream.H"
32 
33 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
34 
36 (
37  string& str,
38  const dictionary& dict
39 )
40 {
43 }
44 
45 
47 (
48  string& code,
49  label lineNum,
50  const string& file
51 )
52 {
53  ++lineNum; // Change from 0-based to 1-based
54 
55  const auto len = code.length();
56 
57  if (lineNum > 0 && len && !file.empty())
58  {
59  code = "#line " + Foam::name(lineNum) + " \"" + file + "\"\n" + code;
60 
61  return (code.length() - len);
62  }
63 
64  return 0;
65 }
66 
67 
69 (
70  string& code,
71  label lineNum,
72  const dictionary& dict
73 )
74 {
75  return addLineDirective(code, lineNum, dict.name());
76 }
77 
78 
79 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
80 
82 :
83  dict_(std::cref<dictionary>(dictionary::null))
84 {}
85 
86 
88 :
90 {
91  setCodeContext(dict);
92 }
93 
94 
95 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
96 
98 {
99  return &(dict_.get()) != &(dictionary::null);
100 }
101 
102 
104 {
105  return this->dict().findEntry(key, keyType::LITERAL);
106 }
107 
108 
110 (
111  const word& key,
112  string& str,
113  bool mandatory,
114  bool withLineNum
115 )
116 {
117  str.clear();
118  sha1_.append("<" + key + ">");
119 
120  const dictionary& dict = this->dict();
121  const entry* eptr = dict.findEntry(key, keyType::LITERAL);
122 
123  if (!eptr)
124  {
125  if (mandatory)
126  {
128  << "Entry '" << key << "' not found in dictionary "
129  << dict.name() << nl
130  << exit(FatalIOError);
131  }
132 
133  return false;
134  }
135 
136  // Expand dictionary entries.
137  // Removing any leading/trailing whitespace is necessary for compilation
138  // options, but is also convenient for includes and code body.
139 
140  eptr->readEntry(str);
142  sha1_.append(str);
143 
144  if (withLineNum)
145  {
146  addLineDirective(str, eptr->startLineNumber(), dict);
147  }
148 
149  return true;
150 }
151 
152 
154 (
155  const word& key,
156  string& str,
157  bool withLineNum
158 )
159 {
160  return readEntry(key, str, false, withLineNum);
161 }
162 
163 
165 {
166  dict_ = std::cref<dictionary>(dict);
167  sha1_.clear();
168 
169  // No #line for options (Make/options)
170  readIfPresent("codeOptions", codeOptions_, false);
171 
172  // No #line for libs (LIB_LIBS)
173  readIfPresent("codeLibs", codeLibs_, false);
174 
175  readIfPresent("codeInclude", codeInclude_);
176  readIfPresent("localCode", localCode_);
177  readIfPresent("code", code_);
178 }
179 
180 
181 // ************************************************************************* //
void readEntry(T &val) const
Assign to T val, FatalIOError if the number of tokens is incorrect.
Definition: entry.H:353
dynamicCodeContext()
Default construct.
dictionary dict
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
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:120
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
virtual label startLineNumber() const =0
Return line number of first token in dictionary.
void inplaceTrim(std::string &s)
Trim leading and trailing whitespace inplace.
Definition: stringOps.C:1054
const fileName & name() const noexcept
The dictionary name.
Definition: dictionaryI.H:41
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.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
A class for handling words, derived from Foam::string.
Definition: word.H:63
static const dictionary null
An empty dictionary, which is also the parent for all dictionaries.
Definition: dictionary.H:465
propsDict readIfPresent("fields", acceptFields)
String literal.
Definition: keyType.H:82
const direction noexcept
Definition: Scalar.H:258
void inplaceExpand(std::string &s, const HashTable< string > &mapping, const char sigil='$')
Inplace expand occurrences of variables according to the mapping. Does not use environment values...
Definition: stringOps.C:718
static unsigned addLineDirective(string &code, label lineNum, const string &file)
Prefix a #line directive to code.
Encapsulation of dynamic code dictionaries.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:607
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
Considered valid if not using dictionary::null as the context.
const entry * findEntry(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find an entry (const access) with the given keyword.
Definition: dictionaryI.H:80
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.
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:63
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...
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...