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-2012 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 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
34 :
35  dict_(dict),
36  code_(),
37  localCode_(),
38  include_(),
39  options_(),
40  libs_()
41 {
42  // expand dictionary entries
43 
44  {
45  const entry& codeEntry = dict.lookupEntry("code", false, false);
46  code_ = stringOps::trim(codeEntry.stream());
47  stringOps::inplaceExpand(code_, dict);
48  }
49 
50  // note: removes any leading/trailing whitespace
51  // - necessary for compilation options, convenient for includes
52  // and body.
53 
54  // optional
55  const entry* includePtr = dict.lookupEntryPtr
56  (
57  "codeInclude",
58  false,
59  false
60  );
61  if (includePtr)
62  {
63  include_ = stringOps::trim(includePtr->stream());
64  stringOps::inplaceExpand(include_, dict);
65  }
66 
67  // optional
68  const entry* optionsPtr = dict.lookupEntryPtr
69  (
70  "codeOptions",
71  false,
72  false
73  );
74  if (optionsPtr)
75  {
76  options_ = stringOps::trim(optionsPtr->stream());
77  stringOps::inplaceExpand(options_, dict);
78  }
79 
80  // optional
81  const entry* libsPtr = dict.lookupEntryPtr("codeLibs", false, false);
82  if (libsPtr)
83  {
84  libs_ = stringOps::trim(libsPtr->stream());
85  stringOps::inplaceExpand(libs_, dict);
86  }
87 
88  // optional
89  const entry* localPtr = dict.lookupEntryPtr("localCode", false, false);
90  if (localPtr)
91  {
92  localCode_ = stringOps::trim(localPtr->stream());
93  stringOps::inplaceExpand(localCode_, dict);
94  }
95 
96  // calculate SHA1 digest from include, options, localCode, code
97  OSHA1stream os;
98  os << include_ << options_ << libs_ << localCode_ << code_;
99  sha1_ = os.digest();
100 
101 
102 
103  // Add line number after calculating sha1 since includes processorDDD
104  // in path which differs between processors.
105 
106  {
107  const entry& codeEntry = dict.lookupEntry("code", false, false);
108  addLineDirective(code_, codeEntry.startLineNumber(), dict.name());
109  }
110  if (includePtr)
111  {
112  addLineDirective(include_, includePtr->startLineNumber(), dict.name());
113  }
114 
115  // Do not add line directive to options_ (Make/options) and libs since
116  // they are preprocessed as a single line at this point. Can be fixed.
117 
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 // ************************************************************************* //
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
The output stream for calculating SHA1 digests.
Definition: OSHA1stream.H:133
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
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:103
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
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:86
const entry * lookupEntryPtr(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream pointer if present.
Definition: dictionary.C:345
dictionary dict
virtual ITstream & stream() const =0
Return token stream if this entry is a primitive entry.
dynamicCodeContext(const dictionary &)
Construct from a dictionary.
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
string trim(const string &)
Return string trimmed of leading and trailing whitespace.
Definition: stringOps.C:938
A class for handling file names.
Definition: fileName.H:69
static void addLineDirective(string &, const label lineNum, const fileName &name)
Helper: add #line directive.
const string & code() const
Return the code.
const entry & lookupEntry(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream if present otherwise error.
Definition: dictionary.C:428
virtual label startLineNumber() const =0
Return line number of first token in dictionary.