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 
43 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
44 
46 (
47  const dictionary& dict,
48  const wordList& codeKeys,
49  const wordList& codeDictVars
50 )
51 :
52  dict_(dict),
53  code_(),
54  options_(),
55  libs_()
56 {
57  // Expand all dictionary entries. Note that this removes any leading or
58  // trailing whitespace, which is necessary for compilation options, and
59  // doesn't hurt for everything else
60  List<const entry*> codePtrs(codeKeys.size(), nullptr);
61  forAll(codeKeys, i)
62  {
63  const word& key = codeKeys[i];
64  codePtrs[i] = dict.lookupEntryPtr(key, false, false);
65  if (codePtrs[i])
66  {
67  string s(stringOps::trim(verbatimString(codePtrs[i]->stream())));
68  stringOps::inplaceExpandCodeString(s, dict, codeDictVars[i]);
69  code_.insert(key, s);
70  }
71  else
72  {
73  code_.insert(key, "");
74  }
75  }
76 
77  // Options
78  const entry* optionsPtr = dict.lookupEntryPtr("codeOptions", false, false);
79  if (optionsPtr)
80  {
81  options_ = stringOps::trim(verbatimString(optionsPtr->stream()));
83  }
84 
85  // Libs
86  const entry* libsPtr = dict.lookupEntryPtr("codeLibs", false, false);
87  if (libsPtr)
88  {
89  libs_ = stringOps::trim(verbatimString(libsPtr->stream()));
91  }
92 
93  // Calculate SHA1 digest from all entries
94  OSHA1stream os;
96  {
97  os << iter();
98  }
99  os << options_ << libs_;
100  sha1_ = os.digest();
101 
102  // Add line directive after calculating SHA1 since this includes
103  // "processor..." in the path which differs between cores
104  forAll(codeKeys, i)
105  {
106  if (codePtrs[i])
107  {
108  const word& key = codeKeys[i];
109  addLineDirective
110  (
111  code_[key],
112  codePtrs[i]->startLineNumber(),
113  dict.name()
114  );
115  }
116  }
117 }
118 
119 
120 // ************************************************************************* //
#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:111
A list of keyword definitions, which are a keyword followed by any number of values (e....
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:548
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, const wordList &codeDictVars)
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
static const word null
An empty word.
Definition: word.H:77
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
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
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 bool)
Return a word representation of a bool.
Definition: boolIO.C:39
dictionary dict