NamedEnum.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 | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2015 OpenFOAM Foundation
9 Copyright (C) 2017-2019 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "NamedEnum.H"
30#include "dictionary.H"
31#include "stdFoam.H"
32
33// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
34
35template<class EnumType, int nEnum>
37:
38 lookup_(2*nEnum)
39{
40 for (int enumi=0; enumi < nEnum; ++enumi)
41 {
42 if (names[enumi] && names[enumi][0])
43 {
44 lookup_.insert(names[enumi], enumi);
45 }
46 else
47 {
48 // Bad name - generate error message
49 List<string> goodNames(enumi);
50
51 for (int i = 0; i < enumi; ++i)
52 {
53 goodNames[i] = names[i];
54 }
55
57 << "Illegal enumeration name at position " << enumi << nl
58 << "after entries " << goodNames << nl
59 << "Possibly your NamedEnum<EnumType, nEnum>::names array"
60 << " is not of size " << nEnum << endl
61 << abort(FatalError);
62 }
63 }
64}
65
66
67// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
68
69template<class EnumType, int nEnum>
71{
72 List<word> lst(nEnum);
73
74 label count = 0;
75 for (int enumi=0; enumi < nEnum; ++enumi)
76 {
77 if (names[enumi] && names[enumi][0])
78 {
79 lst[count++] = names[enumi];
80 }
81 }
82
83 lst.setSize(count);
84 return lst;
85}
86
87
88template<class EnumType, int nEnum>
90{
91 List<int> lst(nEnum);
92
93 label count = 0;
94 for (int enumi=0; enumi < nEnum; ++enumi)
95 {
96 if (names[enumi] && names[enumi][0])
97 {
98 auto iter = lookup_.cfind(names[enumi]);
99
100 if (iter.found())
101 {
102 lst[count++] = iter.val();
103 }
104 }
105 }
106
107 lst.setSize(count);
108 return lst;
109}
110
111
112template<class EnumType, int nEnum>
113bool Foam::NamedEnum<EnumType, nEnum>::hasName(const EnumType e) const
114{
115 const int enumValue(e);
116
117 forAllConstIters(lookup_, iter)
118 {
119 if (iter.val() == enumValue)
120 {
121 return true;
122 }
123 }
124 return false;
125}
126
127
128template<class EnumType, int nEnum>
130(
131 const word& key,
132 const dictionary& dict
133) const
134{
135 const word enumName(dict.lookup(key));
136 auto iter = lookup_.cfind(enumName);
137
138 if (!iter.found())
139 {
141 << enumName << " is not in enumeration: "
142 << lookup_.sortedToc() << nl
143 << exit(FatalIOError);
144 }
145
146 return EnumType(iter.val());
147}
148
149
150template<class EnumType, int nEnum>
152(
153 const word& key,
154 const dictionary& dict,
155 const EnumType deflt
156) const
157{
158 if (dict.found(key))
159 {
160 return lookup(key, dict);
161 }
162
163 return deflt;
164}
165
166
167template<class EnumType, int nEnum>
168EnumType Foam::NamedEnum<EnumType, nEnum>::read(Istream& is) const
169{
170 const word enumName(is);
171 auto iter = lookup_.cfind(enumName);
172
173 if (!iter.found())
174 {
176 << enumName << " is not in enumeration: "
177 << lookup_.sortedToc() << nl
178 << exit(FatalIOError);
179 }
180
181 return EnumType(iter.val());
182}
183
184
185template<class EnumType, int nEnum>
187(
188 const EnumType e,
189 Ostream& os
190) const
191{
192 const int idx = int(e);
193 if (idx >= 0 && idx < nEnum)
194 {
195 os << names[idx];
196 }
197}
198
199
200// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
201
202template<class EnumType, int nEnum>
203Foam::Ostream& Foam::operator<<
204(
205 Ostream& os,
206 const NamedEnum<EnumType, nEnum>& wrapped
207)
208{
209 return wrapped.lookup_.writeKeys(os, 10);
210}
211
212
213// ************************************************************************* //
void setSize(const label n)
Alias for resize()
Definition: List.H:218
A NamedEnum is a wrapper around a list of names that represent particular enumeration values.
Definition: NamedEnum.H:58
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
virtual bool read()
Re-read model coefficients if they have changed.
T lookupOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionary.H:1290
virtual bool write()
Write the output fields.
Lookup type of boundary radiation properties.
Definition: lookup.H:66
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:78
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
errorManip< error > abort(error &err)
Definition: errorManip.H:144
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict
volScalarField & e
Definition: createFields.H:11
Includes some standard C++ headers, defines global macros and templates used in multiple places by Op...
#define forAllConstIters(container, iter)
Iterate across all elements of the container object with const access.
Definition: stdFoam.H:278
const UList< T > & values
Definition: UList.H:189