compileTemplate.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) 2021-2026 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 "compileTemplate.H"
27 #include "Time.H"
28 #include "IFstream.H"
29 #include "OSspecific.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 const Foam::wordList Foam::compileTemplate::codeKeys(wordList::null());
34 
35 const Foam::wordList Foam::compileTemplate::codeDictVars(wordList::null());
36 
37 Foam::wordList Foam::compileTemplate::compileFiles_(wordList::null());
38 
39 
40 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
41 
42 Foam::fileName Foam::compileTemplate::name
43 (
44  const word& instantiatedName
45 ) const
46 {
47  fileName templateFileName(instantiatedName);
48  templateFileName.replaceAll(',', '_');
49  templateFileName.replaceAll('<', '_');
50  templateFileName.replaceAll('>', '_');
51 
52  return templateFileName;
53 }
54 
55 
56 Foam::dictionary Foam::compileTemplate::optionsDict
57 (
58  const word& templateName
59 )
60 {
61  IFstream optionsFile(dynamicCode::resolveTemplate(templateName));
62 
63  if (!optionsFile.good())
64  {
66  << "Failed to open dictionary file " << templateName
67  << exit(FatalError);
68  }
69 
70  return dictionary(optionsFile);
71 }
72 
73 
74 void Foam::compileTemplate::setSubstitution
75 (
76  const Pair<word>& substitution
77 )
78 {
79  const word& name(substitution.first());
80  word type(substitution.second());
81  const word typeRenameMapName(name + "Renamed");
82 
83  if (dict_.found(name))
84  {
85  const HashSet<word> types(dict_.lookup(name));
86  if (!types.found(type))
87  {
89  << "Unknown " << name << " type " << type << nl
90  << "Supported " << name << " types: " << types
91  << exit(FatalIOError);
92  }
93  }
94 
95  if (dict_.found(typeRenameMapName))
96  {
97  const HashTable<word> renameMap
98  (
99  dict_.lookup(typeRenameMapName)
100  );
101 
102  if (renameMap.found(type))
103  {
104  type = renameMap[type];
105  }
106  }
107 
108  varSubstitutions().set(name, type);
109 }
110 
111 
112 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
113 
115 (
116  const word& templateName,
117  const word& instantiatedName,
118  const List<Pair<word>>& substitutions
119 )
120 :
121  codedBase
122  (
123  name(instantiatedName),
124  optionsDict(templateName),
125  codeKeys,
126  codeDictVars,
127  word::null,
128  compileFiles_,
129  wordList::null(),
130  false
131  ),
132  substitutions_(substitutions),
133  dict_(optionsDict(templateName))
134 {
135  compileFiles_ = {templateName + "Template.C"};
136 
137  forAll(substitutions_, i)
138  {
139  setSubstitution(substitutions_[i]);
140  }
141 
142  // Make verbose if debugging
143  varSubstitutions().set("verbose", Foam::name(bool(debug)));
144 
145  this->updateLibrary(dict_);
146 }
147 
148 
149 // ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
Base class for coded functionObjects, fvModels, Function1, Function2.
Definition: codedBase.H:53
bool updateLibrary(const dictionary &dict) const
Update library from given updated dictionary as required.
Definition: codedBase.C:230
compileTemplate(const word &templateName, const word &instantiatedName, const List< Pair< word >> &substitutions)
Construct from name and dictionary.
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
HashTable< string > & varSubstitutions()
Definition: dynamicCode.H:252
static fileName resolveTemplate(const fileName &templateName)
Resolve code-template via Foam::findConfigFile.
Definition: dynamicCode.C:481
A class for handling file names.
Definition: fileName.H:82
A class for handling words, derived from string.
Definition: word.H:63
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
IOerror FatalIOError
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
error FatalError
static const char nl
Definition: Ostream.H:297
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488