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 | Copyright (C) 2011-2016 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 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
33 :
34  dict_(dict),
35  code_(),
36  localCode_(),
37  include_(),
38  options_(),
39  libs_()
40 {
41  // Expand dictionary entries
42 
43  // Note: removes any leading/trailing whitespace
44  // - necessary for compilation options, convenient for includes
45  // and body.
46 
47  const entry* codePtr = dict.lookupEntryPtr
48  (
49  "code",
50  false,
51  false
52  );
53  if (codePtr)
54  {
55  code_ = stringOps::trim(codePtr->stream());
56  stringOps::inplaceExpand(code_, dict);
57  }
58 
59  const entry* includePtr = dict.lookupEntryPtr
60  (
61  "codeInclude",
62  false,
63  false
64  );
65  if (includePtr)
66  {
67  include_ = stringOps::trim(includePtr->stream());
68  stringOps::inplaceExpand(include_, dict);
69  }
70 
71  const entry* optionsPtr = dict.lookupEntryPtr
72  (
73  "codeOptions",
74  false,
75  false
76  );
77  if (optionsPtr)
78  {
79  options_ = stringOps::trim(optionsPtr->stream());
80  stringOps::inplaceExpand(options_, dict);
81  }
82 
83  const entry* libsPtr = dict.lookupEntryPtr("codeLibs", false, false);
84  if (libsPtr)
85  {
86  libs_ = stringOps::trim(libsPtr->stream());
87  stringOps::inplaceExpand(libs_, dict);
88  }
89 
90  const entry* localPtr = dict.lookupEntryPtr("localCode", false, false);
91  if (localPtr)
92  {
93  localCode_ = stringOps::trim(localPtr->stream());
94  stringOps::inplaceExpand(localCode_, dict);
95  }
96 
97  // Calculate SHA1 digest from include, options, localCode, code
98  OSHA1stream os;
99  os << include_ << options_ << libs_ << localCode_ << code_;
100  sha1_ = os.digest();
101 
102 
103  // Add line number after calculating sha1 since includes processorDDD
104  // in path which differs between processors.
105 
106  if (codePtr)
107  {
108  addLineDirective(code_, codePtr->startLineNumber(), dict.name());
109  }
110 
111  if (includePtr)
112  {
113  addLineDirective(include_, includePtr->startLineNumber(), dict.name());
114  }
115 
116  // Do not add line directive to options_ (Make/options) and libs since
117  // they are preprocessed as a single line at this point. Can be fixed.
118  if (localPtr)
119  {
120  addLineDirective(localCode_, localPtr->startLineNumber(), dict.name());
121  }
122 }
123 
124 
125 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
126 
128 (
129  string& code,
130  const label lineNum,
131  const fileName& name
132 )
133 {
134  code = "#line " + Foam::name(lineNum + 1) + " \"" + name + "\"\n" + code;
135 }
136 
137 
138 // ************************************************************************* //
dictionary dict
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
A class for handling file names.
Definition: fileName.H:69
const entry * lookupEntryPtr(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream pointer if present.
Definition: dictionary.C:470
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
static void addLineDirective(string &, const label lineNum, const fileName &name)
Helper: add #line directive.
virtual label startLineNumber() const =0
Return line number of first token in dictionary.
The output stream for calculating SHA1 digests.
Definition: OSHA1stream.H:133
dynamicCodeContext(const dictionary &)
Construct from a dictionary.
string trim(const string &)
Return string trimmed of leading and trailing whitespace.
Definition: stringOps.C:923
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:103
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
string & inplaceExpand(string &, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Inplace expand occurences of variables according to the mapping.
Definition: stringOps.C:87
virtual ITstream & stream() const =0
Return token stream if this entry is a primitive entry.
const string & code() const
Return the code.
Foam::SHA1Digest digest()
Return SHA1::Digest for the data processed until now.
Definition: OSHA1stream.H:185
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:65