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-2023 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  string s(stringOps::trim(verbatimString(codePtrs[i]->stream())));
68  code_.insert(key, s);
69  }
70  else
71  {
72  code_.insert(key, "");
73  }
74  }
75 
76  // Options
77  const entry* optionsPtr = dict.lookupEntryPtr("codeOptions", false, false);
78  if (optionsPtr)
79  {
80  options_ = stringOps::trim(verbatimString(optionsPtr->stream()));
82  }
83 
84  // Libs
85  const entry* libsPtr = dict.lookupEntryPtr("codeLibs", false, false);
86  if (libsPtr)
87  {
88  libs_ = stringOps::trim(verbatimString(libsPtr->stream()));
90  }
91 
92  // Calculate SHA1 digest from all entries
93  OSHA1stream os;
95  {
96  os << iter();
97  }
98  os << options_ << libs_;
99  sha1_ = os.digest();
100 
101  // Add line directive after calculating SHA1 since this includes
102  // "processor..." in the path which differs between cores
103  forAll(codeKeys, i)
104  {
105  if (codePtrs[i])
106  {
107  const word& key = codeKeys[i];
108  addLineDirective
109  (
110  code_[key],
111  codePtrs[i]->startLineNumber(),
112  dict.name()
113  );
114  }
115  }
116 }
117 
118 
119 // ************************************************************************* //
#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
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:940
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