EnumI.H
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) 2017-2023 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
29 
30 template<class EnumType>
32 {
33  return keys_.empty();
34 }
35 
36 
37 template<class EnumType>
38 inline Foam::label Foam::Enum<EnumType>::size() const noexcept
39 {
40  return keys_.size();
41 }
42 
43 
44 template<class EnumType>
45 inline const Foam::List<Foam::word>&
47 {
48  return keys_;
49 }
50 
51 
52 template<class EnumType>
53 inline const Foam::List<int>&
55 {
56  return vals_;
57 }
58 
59 
60 template<class EnumType>
61 inline const Foam::List<Foam::word>&
63 {
64  return keys_;
65 }
66 
67 
68 template<class EnumType>
71 {
72  List<word> list(keys_);
73 
74  Foam::sort(list);
75 
76  return list;
77 }
78 
79 
80 template<class EnumType>
81 inline void Foam::Enum<EnumType>::clear()
82 {
83  keys_.clear();
84  vals_.clear();
85 }
86 
87 
88 template<class EnumType>
89 inline bool Foam::Enum<EnumType>::contains(const word& enumName) const
90 {
91  return keys_.contains(enumName);
92 }
93 
94 
95 template<class EnumType>
96 inline bool Foam::Enum<EnumType>::contains(const EnumType e) const
97 {
98  return vals_.contains(int(e));
99 }
100 
101 
102 template<class EnumType>
103 inline const Foam::word& Foam::Enum<EnumType>::get(const EnumType e) const
104 {
105  const label idx = vals_.find(int(e));
106 
107  if (idx < 0)
108  {
109  return word::null;
110  }
112  return keys_[idx];
113 }
114 
115 
116 template<class EnumType>
118 (
119  const word& key,
120  const dictionary& dict,
121  EnumType& val,
122  const bool warnOnly
123 ) const
124 {
125  // Reading is non-mandatory
126  return readEntry(key, dict, val, false, warnOnly);
127 }
128 
129 
130 template<class EnumType>
131 inline void Foam::Enum<EnumType>::write(const EnumType e, Ostream& os) const
132 {
133  const label idx = vals_.find(int(e));
134 
135  if (idx >= 0)
136  {
137  os << keys_[idx];
138  }
139 }
140 
141 
142 template<class EnumType>
143 template<class OS>
144 inline OS& Foam::Enum<EnumType>::writeList(OS& os, const label) const
145 {
146  unsigned i = 0;
147 
148  os << '(';
149  for (const word& k : keys_)
150  {
151  if (i++) os << ' ';
152  os << k;
153  }
154  os << ')';
155 
156  return os;
157 }
158 
159 
160 // * * * * * * * * * * * * * * * * Iterators * * * * * * * * * * * * * * * * //
161 
162 template<class EnumType>
164 (
165  const Enum* eptr,
166  const label idx
167 ) noexcept
168 :
169  ptr_(eptr),
170  idx_(idx)
171 {}
172 
173 
174 template<class EnumType>
175 inline const Foam::word&
177 {
178  return ptr_->names()[idx_];
179 }
180 
181 
182 template<class EnumType>
184 {
185  return EnumType(ptr_->values()[idx_]);
186 }
187 
188 
189 template<class EnumType>
191 {
192  return (ptr_ && idx_ >= 0 && idx_ < ptr_->size());
193 }
194 
195 
196 template<class EnumType>
199 {
200  ++idx_;
201  return *this;
202 }
203 
204 
205 template<class EnumType>
207 (
208  const const_iterator& iter
209 ) const noexcept
210 {
211  return idx_ == iter.idx_;
212 }
213 
214 
215 template<class EnumType>
217 (
218  const const_iterator& iter
219 ) const noexcept
220 {
221  return idx_ != iter.idx_;
222 }
223 
224 
225 template<class EnumType>
228 {
229  return typename Enum<EnumType>::const_iterator(this);
230 }
231 
232 
233 template<class EnumType>
236 {
237  return typename Enum<EnumType>::const_iterator(this, this->size());
238 }
239 
240 
241 template<class EnumType>
244 {
245  const label idx = keys_.find(key);
246 
247  return typename Enum<EnumType>::const_iterator
248  (
249  this,
250  (idx >= 0 ? idx : this->size())
251  );
252 }
253 
254 
255 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
256 
257 template<class EnumType>
258 inline EnumType Foam::Enum<EnumType>::operator[]
259 (
260  const word& enumName
261 ) const
262 {
263  return get(enumName);
264 }
265 
266 
267 template<class EnumType>
269 (
270  const EnumType e
271 ) const
272 {
273  return get(e);
274 }
275 
276 
277 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
278 
279 template<class EnumType>
280 inline Foam::Ostream& Foam::operator<<
281 (
282  Ostream& os,
283  const Enum<EnumType>& list
284 )
285 {
286  return list.writeList(os);
287 }
288 
289 
290 template<class EnumType>
291 inline std::ostream& Foam::operator<<
292 (
293  std::ostream& os,
294  const Enum<EnumType>& list
295 )
296 {
297  return list.writeList(os);
298 }
299 
300 
301 // ************************************************************************* //
EnumType get(const word &enumName) const
The enumeration corresponding to the given name.
Definition: Enum.C:68
void write(const EnumType e, Ostream &os) const
Write the name representation of the enumeration to an Ostream.
Definition: EnumI.H:124
const_iterator(const Enum *eptr=nullptr, const label idx=0) noexcept
Default construct, construct at given position.
Definition: EnumI.H:157
dictionary dict
OS & writeList(OS &os, const label ununsed=0) const
Write enumeration names as a list without line-breaks to an output stream.
Definition: EnumI.H:137
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
bool good() const noexcept
True if iterator points to an entry.
Definition: EnumI.H:183
bool readIfPresent(const word &key, const dictionary &dict, EnumType &val, const bool warnOnly=false) const
Find an entry if present, and assign to T val.
Definition: EnumI.H:111
const_iterator cfind(const word &key) const
Find the enumeration by name.
Definition: EnumI.H:236
A const_iterator for iterating an Enum list.
Definition: Enum.H:326
EnumType val() const
Enumeration value at the current index.
Definition: EnumI.H:176
label k
Boltzmann constant.
bool contains(const word &enumName) const
True if there is an enumeration corresponding to the given name.
Definition: EnumI.H:82
const List< word > & toc() const noexcept
The list of enum names, in construction order. Same as names()
Definition: EnumI.H:55
void clear()
Clear all entries.
Definition: EnumI.H:74
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
A class for handling words, derived from Foam::string.
Definition: word.H:63
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:296
const List< int > & values() const noexcept
The list of enum values, in construction order.
Definition: EnumI.H:47
label find(const T &val) const
Find index of the first occurrence of the value.
Definition: UList.C:173
const_iterator & operator++() noexcept
Move to the next index.
Definition: EnumI.H:191
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
const List< word > & names() const noexcept
The list of enum names, in construction order. Same as toc()
Definition: EnumI.H:39
OBJstream os(runTime.globalPath()/outputName)
const_iterator cend() const noexcept
Definition: EnumI.H:228
const word & key() const
The name at the current index.
Definition: EnumI.H:169
label size() const noexcept
The number of name/value pairs for the enumeration.
Definition: EnumI.H:31
List< word > sortedToc() const
The sorted list of enum names.
Definition: EnumI.H:63
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: error.H:64
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
const_iterator cbegin() const noexcept
Definition: EnumI.H:220
bool empty() const noexcept
True if the enumeration list is empty.
Definition: EnumI.H:24