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 -------------------------------------------------------------------------------
11 License
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 
35 template<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 
69 template<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 
88 template<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 
112 template<class EnumType, int nEnum>
113 bool 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 
128 template<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 
150 template<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 
167 template<class EnumType, int nEnum>
168 EnumType 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 
185 template<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 
202 template<class EnumType, int nEnum>
203 Foam::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 // ************************************************************************* //
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
NamedEnum.H
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::blockMeshTools::read
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:57
Foam::List::setSize
void setSize(const label n)
Alias for resize()
Definition: List.H:222
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
os
OBJstream os(runTime.globalPath()/outputName)
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
forAllConstIters
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::List< word >
stdFoam.H
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
dictionary.H
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:36
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::PtrListOps::names
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Foam::NamedEnum
A NamedEnum is a wrapper around a list of names that represent particular enumeration values.
Definition: NamedEnum.H:58