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 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& code,
38  const dictionary& dict
39 )
40 {
43 }
44 
45 
47 (
48  string& code,
49  label lineNum,
50  const fileName& 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  dict_ = std::cref<dictionary>(dict);
106  sha1_.clear();
107 
108  // Expand dictionary entries.
109  // Removing any leading/trailing whitespace is necessary for compilation
110  // options, but is also convenient for includes and code body.
111 
112  const entry* eptr;
113 
114  options_.clear();
115  sha1_.append("<codeOptions>");
116  if ((eptr = dict.findEntry("codeOptions", keyType::LITERAL)) != nullptr)
117  {
118  eptr->readEntry(options_);
120  sha1_.append(options_);
121  // No #line for options (Make/options)
122  }
123 
124  libs_.clear();
125  sha1_.append("<codeLibs>");
126  if ((eptr = dict.findEntry("codeLibs", keyType::LITERAL)) != nullptr)
127  {
128  eptr->readEntry(libs_);
130  sha1_.append(libs_);
131  // No #line for libs (LIB_LIBS)
132  }
133 
134  include_.clear();
135  sha1_.append("<codeInclude>");
136  if ((eptr = dict.findEntry("codeInclude", keyType::LITERAL)) != nullptr)
137  {
138  eptr->readEntry(include_);
140  sha1_.append(include_);
141  addLineDirective(include_, eptr->startLineNumber(), dict);
142  }
143 
144  code_.clear();
145  sha1_.append("<code>");
146  if ((eptr = dict.findEntry("code", keyType::LITERAL)) != nullptr)
147  {
148  eptr->readEntry(code_);
150  sha1_.append(code_);
151  addLineDirective(code_, eptr->startLineNumber(), dict);
152  }
153 
154  localCode_.clear();
155  sha1_.append("<localCode>");
156  if ((eptr = dict.findEntry("localCode", keyType::LITERAL)) != nullptr)
157  {
158  eptr->readEntry(localCode_);
160  sha1_.append(localCode_);
161  addLineDirective(localCode_, eptr->startLineNumber(), dict);
162  }
163 }
164 
165 
166 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
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::stringOps::inplaceTrim
void inplaceTrim(std::string &s)
Trim leading and trailing whitespace inplace.
Definition: stringOps.C:1069
Foam::dynamicCodeContext
Encapsulation of dynamic code dictionaries.
Definition: dynamicCodeContext.H:53
dynamicCodeContext.H
Foam::dynamicCodeContext::dynamicCodeContext
dynamicCodeContext()
Construct null.
Definition: dynamicCodeContext.C:81
Foam::dictionary::null
static const dictionary null
An empty dictionary, which is also the parent for all dictionaries.
Definition: dictionary.H:385
Foam::dynamicCodeContext::inplaceExpand
static void inplaceExpand(string &code, const dictionary &dict)
Cleanup string and expand with dictionary parameters.
Definition: dynamicCodeContext.C:36
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::stringOps::inplaceExpand
void inplaceExpand(std::string &s, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Definition: stringOps.C:733
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:121
Foam::dynamicCodeContext::setCodeContext
void setCodeContext(const dictionary &dict)
Set code context from a dictionary.
Definition: dynamicCodeContext.C:103
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: dictionary.C:374
OSHA1stream.H
Foam::entry::readEntry
void readEntry(T &val) const
Definition: entry.H:270
Foam::keyType::LITERAL
String literal.
Definition: keyType.H:73
stringOps.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