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-2019 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 
43 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
44 
46 (
47  const dictionary& dict,
48  const wordList& codeKeys
49 )
50 :
51  dict_(dict),
52  code_(),
53  options_(),
54  libs_()
55 {
56  // Expand all dictionary entries. Note that this removes any leading or
57  // trailing whitespace, which is necessary for compilation options, and
58  // doesn't hurt for everything else
59  List<const entry*> codePtrs(codeKeys.size(), nullptr);
60  forAll(codeKeys, i)
61  {
62  const word& key = codeKeys[i];
63  codePtrs[i] = dict.lookupEntryPtr(key, false, false);
64  if (codePtrs[i])
65  {
66  code_.insert
67  (
68  key,
70  (
71  stringOps::trim(verbatimString(codePtrs[i]->stream())),
72  dict
73  )
74  );
75  }
76  else
77  {
78  code_.insert(key, "");
79  }
80  }
81 
82  // Options
83  const entry* optionsPtr = dict.lookupEntryPtr("codeOptions", false, false);
84  if (optionsPtr)
85  {
86  options_ =
88  (
89  stringOps::trim(verbatimString(optionsPtr->stream())),
90  dict
91  );
92  }
93 
94  // Libs
95  const entry* libsPtr = dict.lookupEntryPtr("codeLibs", false, false);
96  if (libsPtr)
97  {
98  libs_ =
100  (
102  dict
103  );
104  }
105 
106  // Calculate SHA1 digest from all entries
107  OSHA1stream os;
108  forAllConstIter(HashTable<string>, code_, iter)
109  {
110  os << iter();
111  }
112  os << options_ << libs_;
113  sha1_ = os.digest();
114 
115  // Add line directive after calculating SHA1 since this includes
116  // "processor..." in the path which differs between cores
117  forAll(codeKeys, i)
118  {
119  if (codePtrs[i])
120  {
121  const word& key = codeKeys[i];
122  addLineDirective
123  (
124  code_[key],
125  codePtrs[i]->startLineNumber(),
126  dict.name()
127  );
128  }
129  }
130 }
131 
132 
133 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:477
An STL-conforming hash table.
Definition: HashTable.H:127
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
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
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:109
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
const entry * lookupEntryPtr(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream pointer if present.
Definition: dictionary.C:698
const HashTable< string > & code() const
Return the code table.
const dictionary & dict() const
Return the parent dictionary context.
dynamicCodeContext(const dictionary &dict, const wordList &codeKeys)
Construct from a dictionary and lists of which entries correspond.
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.
A class for handling words, derived from string.
Definition: word.H:62
string trim(const string &)
Return string trimmed of leading and trailing whitespace.
Definition: stringOps.C:934
string expand(const string &, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Expand occurrences of variables according to the mapping.
Definition: stringOps.C:69
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
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
dictionary dict