dynamicCodeContext.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "dynamicCodeContext.H"
27 #include "stringOps.H"
28 #include "OSHA1stream.H"
29 
30 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
31 
32 void Foam::dynamicCodeContext::addLineDirective
33 (
34  string& code,
35  const label lineNum,
36  const fileName& name
37 )
38 {
39  code = "#line " + Foam::name(lineNum + 1) + " \"" + name + "\"\n" + code;
40 }
41 
42 
44 (
45  const dictionary& contextDict,
46  const dictionary& codeDict
47 )
48 {
49  // Expand all dictionary entries. Note that this removes any leading or
50  // trailing whitespace, which is necessary for compilation options, and
51  // doesn't hurt for everything else
52  List<const entry*> codePtrs(codeKeys_.size(), nullptr);
53  code_.clear();
54  forAll(codeKeys_, i)
55  {
56  const word& key = codeKeys_[i];
57  codePtrs[i] = codeDict.lookupEntryPtr(key, false, false);
58  if (codePtrs[i])
59  {
60  codeStrings_[i] = verbatimString(codePtrs[i]->stream());
62  (
63  codeStrings_[i],
64  contextDict, // Lookup variables from the context dictionary
65  codeDictVars_[i]
66  );
67  code_.insert(key, codeStrings_[i]);
68  }
69  else
70  {
71  code_.insert(key, "");
72  }
73  }
74 
75  // Options
76  const entry* optionsPtr =
77  codeDict.lookupEntryPtr("codeOptions", false, false);
78  if (optionsPtr)
79  {
80  optionsString_ = verbatimString(optionsPtr->stream());
82  (
83  optionsString_,
84  contextDict,
86  );
87  options_ = stringOps::trim(optionsString_);
88  }
89 
90  // Libs
91  const entry* libsPtr = codeDict.lookupEntryPtr("codeLibs", false, false);
92  if (libsPtr)
93  {
94  libsString_ = verbatimString(libsPtr->stream());
96  (
97  libsString_,
98  contextDict,
100  );
101  libs_ = stringOps::trim(libsString_);
102  }
103 
104  // Calculate SHA1 digest from all entries
105  OSHA1stream os;
106  forAllConstIter(HashTable<string>, code_, iter)
107  {
108  os << iter();
109  }
110  os << options_ << libs_;
111  sha1_ = os.digest();
112 
113  // Add line directives after calculating SHA1
114  forAll(codeKeys_, i)
115  {
116  if (codePtrs[i])
117  {
118  const word& key = codeKeys_[i];
119  addLineDirective
120  (
121  code_[key],
122  codePtrs[i]->startLineNumber(),
123  codeDict.name()
124  );
125  }
126  }
127 }
128 
129 
131 {
132  read(contextDict, contextDict);
133 }
134 
135 
136 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
137 
139 (
140  const dictionary& contextDict,
141  const dictionary& codeDict,
142  const wordList& codeKeys,
143  const wordList& codeDictVars
144 )
145 :
146  codeKeys_(codeKeys),
147  codeDictVars_(codeDictVars),
148  codeStrings_(codeKeys.size())
149 {
150  read(contextDict, codeDict);
151 }
152 
153 
155 (
156  const dictionary& contextDict,
157  const wordList& codeKeys,
158  const wordList& codeDictVars
159 )
160 :
161  dynamicCodeContext(contextDict, contextDict, codeKeys, codeDictVars)
162 {}
163 
164 
165 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
166 
168 {
169  forAll(codeStrings_, i)
170  {
171  if (codeStrings_[i] != verbatimString::null)
172  {
173  writeEntry(os, codeKeys_[i], codeStrings_[i]);
174  }
175  }
176 
177  if (optionsString_ != verbatimString::null)
178  {
179  writeEntry(os, "codeOptions", optionsString_);
180  }
181 
182  if (libsString_ != verbatimString::null)
183  {
184  writeEntry(os, "codeLibs", libsString_);
185  }
186 }
187 
188 
189 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:476
An STL-conforming hash table.
Definition: HashTable.H:127
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:91
A Foam::OSstream for calculating SHA-1 digests.
Definition: OSHA1stream.H:152
SHA1Digest digest()
Return the SHA-1 digest for the data processed until now.
Definition: OSHA1stream.H:194
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:111
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
const entry * lookupEntryPtr(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream pointer if present.
Definition: dictionary.C:578
Encapsulation of dynamic code dictionaries.
const HashTable< string > & code() const
Return the code table.
dynamicCodeContext(const dictionary &contextDict, const dictionary &codeDict, const wordList &codeKeys, const wordList &codeDictVars)
Construct from the context and code dictionaries.
void write(Ostream &os) const
Write the code for restart.
void read(const dictionary &contextDict, const dictionary &codeDict)
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:68
virtual ITstream & stream() const =0
Return token stream if this entry is a primitive entry.
A class for handling verbatimStrings, derived from string.
static const verbatimString null
An empty verbatimString.
A class for handling words, derived from string.
Definition: word.H:62
static const word null
An empty word.
Definition: word.H:77
string & inplaceExpandCodeString(string &, const dictionary &dict, const word &dictVar="dict", const char sigil='$')
Inplace expand occurrences of variables according to the dictionary.
Definition: stringOps.C:410
string trim(const string &)
Return string trimmed of leading and trailing whitespace.
Definition: stringOps.C:947
bool read(const char *, int32_t &)
Definition: int32IO.C:85
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.