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 
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 
97 bool Foam::dynamicCodeContext::valid() const noexcept
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 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
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::stringOps::inplaceTrim
void inplaceTrim(std::string &s)
Trim leading and trailing whitespace inplace.
Definition: stringOps.C:1067
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::FatalIOError
IOerror FatalIOError
Foam::dynamicCodeContext::dict
const dictionary & dict() const noexcept
Return the parent dictionary context.
Definition: dynamicCodeContext.H:128
dynamicCodeContext.H
Foam::dynamicCodeContext::dynamicCodeContext
dynamicCodeContext()
Default construct.
Definition: dynamicCodeContext.C:81
Foam::dictionary::name
const fileName & name() const noexcept
The dictionary name.
Definition: dictionaryI.H:48
Foam::dictionary::null
static const dictionary null
An empty dictionary, which is also the parent for all dictionaries.
Definition: dictionary.H:392
Foam::stringOps::inplaceExpand
void inplaceExpand(std::string &s, const HashTable< string > &mapping, const char sigil='$')
Definition: stringOps.C:731
Foam::dynamicCodeContext::findEntry
const entry * findEntry(const word &key) const
Locate literal dictionary entry, nullptr if not found.
Definition: dynamicCodeContext.C:103
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::entry::startLineNumber
virtual label startLineNumber() const =0
Return line number of first token in dictionary.
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::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::dynamicCodeContext::inplaceExpand
static void inplaceExpand(string &str, const dictionary &dict)
Cleanup string and expand with dictionary parameters.
Definition: dynamicCodeContext.C:36
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::dynamicCodeContext::addLineDirective
static unsigned addLineDirective(string &code, label lineNum, const string &file)
Prefix a #line directive to code.
Definition: dynamicCodeContext.C:47
Foam::dictionary::findEntry
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
OSHA1stream.H
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::entry::readEntry
void readEntry(T &val) const
Definition: entry.H:281
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::keyType::LITERAL
String literal.
Definition: keyType.H:81
stringOps.H