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-------------------------------------------------------------------------------
11License
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
29#include "dynamicCodeContext.H"
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{
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// ************************************************************************* //
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
const fileName & name() const noexcept
The dictionary name.
Definition: dictionaryI.H:48
entry * findEntry(const word &keyword, enum keyType::option matchOpt=keyType::REGEX)
Find for an entry (non-const access) with the given keyword.
Definition: dictionaryI.H:97
Encapsulation of dynamic code dictionaries.
bool readIfPresent(const word &key, string &str, bool withLineNum=true)
static unsigned addLineDirective(string &code, label lineNum, const string &file)
Prefix a #line directive to code.
void setCodeContext(const dictionary &dict)
Set code context from a dictionary.
bool readEntry(const word &key, string &str, bool mandatory=true, bool withLineNum=true)
static void inplaceExpand(string &str, const dictionary &dict)
Cleanup string and expand with dictionary parameters.
bool valid() const noexcept
Considered valid if not using dictionary::null as the context.
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.
dynamicCodeContext()
Default construct.
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:70
void readEntry(T &val) const
Definition: entry.H:281
virtual label startLineNumber() const =0
Return line number of first token in dictionary.
@ LITERAL
String literal.
Definition: keyType.H:81
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
void inplaceExpand(std::string &s, const HashTable< string > &mapping, const char sigil='$')
Definition: stringOps.C:731
void inplaceTrim(std::string &s)
Trim leading and trailing whitespace inplace.
Definition: stringOps.C:1067
IOerror FatalIOError
const direction noexcept
Definition: Scalar.H:223
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict
propsDict readIfPresent("fields", acceptFields)