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>
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>
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::NamedEnum::values
List< int > values() const
The list of enum values, in construction order.
Definition: NamedEnum.C:89
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::dictionary::found
bool found(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Search for an entry (const access) with the given keyword.
Definition: dictionary.C:359
Foam::NamedEnum::hasName
bool hasName(const EnumType e) const
Test if there is a name corresponding to the given enumeration.
Definition: NamedEnum.C:113
NamedEnum.H
Foam::HashTable::insert
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:168
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::label
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:62
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::cellModeller::lookup
const cellModel * lookup(const word &modelName)
Deprecated(2017-11) equivalent to cellModel::ptr static method.
Definition: cellModeller.H:46
Foam::dictionary::lookup
ITstream & lookup(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionary.C:419
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::NamedEnum::write
void write(const EnumType e, Ostream &os) const
Write the name representation of the enumeration to an Ostream.
Definition: NamedEnum.C:187
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
Foam::NamedEnum::lookupOrDefault
EnumType lookupOrDefault(const word &key, const dictionary &dict, const EnumType deflt) const
Find the key in the dictionary and return the corresponding.
Definition: NamedEnum.C:152
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::NamedEnum::NamedEnum
NamedEnum() FOAM_DEPRECATED_FOR(2017-05
Construct from names.
Definition: NamedEnum.C:36
Foam::NamedEnum::words
wordList words() const
The list of enum names, in construction order.
Definition: NamedEnum.C:70
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::nl
constexpr char nl
Definition: Ostream.H:372
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:74
Foam::NamedEnum::lookup
EnumType lookup(const word &key, const dictionary &dict) const
Lookup the key in the dictionary and return the corresponding.
Definition: NamedEnum.C:130
Foam::List< string >
stdFoam.H
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
dictionary.H
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:375
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::List::setSize
void setSize(const label newSize)
Alias for resize(const label)
Definition: ListI.H:146
Foam::NamedEnum::names
static const char * names[nEnum]
The set of names corresponding to the enumeration EnumType.
Definition: NamedEnum.H:98
Foam::NamedEnum
A NamedEnum is a wrapper around a list of names that represent particular enumeration values.
Definition: NamedEnum.H:58
Foam::NamedEnum::read
EnumType read(Istream &is) const
Read a word from Istream and return the corresponding enumeration.
Definition: NamedEnum.C:168