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 ) const
60 {
61  IFstream optionsFile(dynamicCode::resolveTemplate(templateName));
62  if (!optionsFile.good())
63  {
65  << "Failed to open dictionary file " << templateName
66  << exit(FatalError);
67  }
68 
69  return dictionary(optionsFile);
70 }
71 
72 
73 void Foam::compileTemplate::setSubstitution
74 (
75  const Pair<word>& substitution
76 )
77 {
78  const word& name(substitution.first());
79  word type(substitution.second());
80  const word typeRenameMapName(name + "Renamed");
81 
82  if (dict_.found(name))
83  {
84  const HashSet<word> types(dict_.lookup(name));
85  if (!types.found(type))
86  {
88  << "Unknown " << name << " type " << type << nl
89  << "Supported " << name << " types: " << types
90  << exit(FatalIOError);
91  }
92  }
93 
94  if (dict_.found(typeRenameMapName))
95  {
96  const HashTable<word> renameMap
97  (
98  dict_.lookup(typeRenameMapName)
99  );
100 
101  if (renameMap.found(type))
102  {
103  type = renameMap[type];
104  }
105  }
106 
107  varSubstitutions().set(name, type);
108 }
109 
110 
111 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
112 
114 (
115  const word& templateName,
116  const word& instantiatedName,
117  const List<Pair<word>>& substitutions
118 )
119 :
120  codedBase
121  (
122  name(instantiatedName),
123  optionsDict(templateName),
124  codeKeys,
125  codeDictVars,
126  word::null,
127  compileFiles_,
128  wordList::null(),
129  false
130  ),
131  substitutions_(substitutions),
132  dict_(optionsDict(templateName))
133 {
134  compileFiles_ = {templateName + "Template.C"};
135 
136  forAll(substitutions_, i)
137  {
138  setSubstitution(substitutions_[i]);
139  }
140 
141  // Make verbose if debugging
142  varSubstitutions().set("verbose", Foam::name(bool(debug)));
143 
144  this->updateLibrary(dict_);
145 }
146 
147 
148 // ************************************************************************* //
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