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-2021 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>
31 inline bool Foam::Enum<EnumType>::empty() const noexcept
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>&
62 Foam::Enum<EnumType>::toc() const noexcept
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>
82 {
83  keys_.clear();
84  vals_.clear();
85 }
86 
87 
88 template<class EnumType>
89 inline Foam::label Foam::Enum<EnumType>::find(const word& enumName) const
90 {
91  return keys_.find(enumName);
92 }
93 
94 
95 template<class EnumType>
96 inline Foam::label Foam::Enum<EnumType>::find(const EnumType e) const
97 {
98  return vals_.find(int(e));
99 }
100 
101 
102 template<class EnumType>
103 inline bool Foam::Enum<EnumType>::found(const word& enumName) const
104 {
105  return keys_.found(enumName);
106 }
107 
108 
109 template<class EnumType>
110 inline bool Foam::Enum<EnumType>::found(const EnumType e) const
111 {
112  return vals_.found(int(e));
113 }
114 
115 
116 template<class EnumType>
117 inline const Foam::word& Foam::Enum<EnumType>::get(const EnumType e) const
118 {
119  const label idx = find(e);
120 
121  if (idx < 0)
122  {
123  return word::null;
124  }
125 
126  return keys_[idx];
127 }
128 
129 
130 template<class EnumType>
132 (
133  const word& key,
134  const dictionary& dict,
135  EnumType& val
136 ) const
137 {
138  // Reading is non-mandatory
139  return readEntry(key, dict, val, false);
140 }
141 
142 
143 template<class EnumType>
144 inline void Foam::Enum<EnumType>::write(const EnumType e, Ostream& os) const
145 {
146  const label idx = find(e);
147 
148  if (idx >= 0)
149  {
150  os << keys_[idx];
151  }
152 }
153 
154 
155 template<class EnumType>
156 template<class OS>
157 inline OS& Foam::Enum<EnumType>::writeList(OS& os, const label) const
158 {
159  unsigned i = 0;
160 
161  os << '(';
162  for (const word& k : keys_)
163  {
164  if (i++) os << ' ';
165  os << k;
166  }
167  os << ')';
168 
169  return os;
170 }
171 
172 
173 // * * * * * * * * * * * * * * * * Iterators * * * * * * * * * * * * * * * * //
174 
175 template<class EnumType>
177 (
178  const Enum* eptr,
179  const label idx
180 ) noexcept
181 :
182  ptr_(eptr),
183  idx_(idx)
184 {}
185 
186 
187 template<class EnumType>
188 inline const Foam::word&
190 {
191  return ptr_->names()[idx_];
192 }
193 
194 
195 template<class EnumType>
197 {
198  return EnumType(ptr_->values()[idx_]);
199 }
200 
201 
202 template<class EnumType>
205 {
206  ++idx_;
207  return *this;
208 }
209 
210 
211 template<class EnumType>
213 (
214  const const_iterator& iter
215 ) const noexcept
216 {
217  return idx_ == iter.idx_;
218 }
219 
220 
221 template<class EnumType>
223 (
224  const const_iterator& iter
225 ) const noexcept
226 {
227  return idx_ != iter.idx_;
228 }
229 
230 
231 template<class EnumType>
234 {
235  return typename Enum<EnumType>::const_iterator(this);
236 }
237 
238 
239 template<class EnumType>
242 {
243  return typename Enum<EnumType>::const_iterator(this, this->size());
244 }
245 
246 
247 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
248 
249 template<class EnumType>
250 inline EnumType Foam::Enum<EnumType>::operator[]
251 (
252  const word& enumName
253 ) const
254 {
255  return get(enumName);
256 }
257 
258 
259 template<class EnumType>
261 (
262  const EnumType e
263 ) const
264 {
265  return get(e);
266 }
267 
268 
269 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
270 
271 template<class EnumType>
272 inline Foam::Ostream& Foam::operator<<
273 (
274  Ostream& os,
275  const Enum<EnumType>& list
276 )
277 {
278  return list.writeList(os);
279 }
280 
281 
282 template<class EnumType>
283 inline std::ostream& Foam::operator<<
284 (
285  std::ostream& os,
286  const Enum<EnumType>& list
287 )
288 {
289  return list.writeList(os);
290 }
291 
292 
293 // ************************************************************************* //
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:57
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::Enum::found
bool found(const word &enumName) const
True if there is an enumeration corresponding to the given name.
Definition: EnumI.H:103
Foam::Enum::const_iterator::val
EnumType val() const
Enumeration value at the current index.
Definition: EnumI.H:196
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::Enum::empty
bool empty() const noexcept
True if the enumeration list is empty.
Definition: EnumI.H:31
Foam::Enum::toc
const List< word > & toc() const noexcept
The list of enum names, in construction order. Same as names()
Definition: EnumI.H:62
Foam::Enum::const_iterator
A const_iterator for iterating an Enum list.
Definition: Enum.H:261
Foam::Enum::get
EnumType get(const word &enumName) const
The enumeration corresponding to the given name.
Definition: Enum.C:75
Foam::Enum::write
void write(const EnumType e, Ostream &os) const
Write the name representation of the enumeration to an Ostream.
Definition: EnumI.H:144
Foam::sort
void sort(UList< T > &a)
Definition: UList.C:261
Foam::Enum::const_iterator::operator++
const_iterator & operator++() noexcept
Move to the next index.
Definition: EnumI.H:204
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam::Enum::const_iterator::key
const word & key() const
The name at the current index.
Definition: EnumI.H:189
Foam::ListOps::find
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Find index of the first occurrence that satisfies the predicate.
Foam::Enum::const_iterator::const_iterator
const_iterator(const Enum *eptr=nullptr, const label idx=0) noexcept
Default construct, construct at given position.
Definition: EnumI.H:177
Foam::List< Foam::word >
Foam::Enum::size
label size() const noexcept
The number of name/value pairs for the enumeration.
Definition: EnumI.H:38
Foam::Enum::clear
void clear()
Clear all entries.
Definition: EnumI.H:81
Foam::Enum::writeList
OS & writeList(OS &os, const label ununsed=0) const
Definition: EnumI.H:157
k
label k
Boltzmann constant.
Definition: LISASMDCalcMethod2.H:41
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
Foam::Enum::readIfPresent
bool readIfPresent(const word &key, const dictionary &dict, EnumType &val) const
Find an entry if present, and assign to T val.
Definition: EnumI.H:132
Foam::Enum::cbegin
const_iterator cbegin() const noexcept
Definition: EnumI.H:233
Foam::Enum::sortedToc
List< word > sortedToc() const
The sorted list of enum names.
Definition: EnumI.H:70
Foam::Enum::find
label find(const word &enumName) const
Find the index of the given name.
Definition: EnumI.H:89
Foam::Enum::cend
const_iterator cend() const noexcept
Definition: EnumI.H:241
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Enum::values
const List< int > & values() const noexcept
The list of enum values, in construction order.
Definition: EnumI.H:54
Foam::Enum::names
const List< word > & names() const noexcept
The list of enum names, in construction order. Same as toc()
Definition: EnumI.H:46