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-------------------------------------------------------------------------------
10License
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
30template<class EnumType>
31inline bool Foam::Enum<EnumType>::empty() const noexcept
32{
33 return keys_.empty();
34}
35
36
37template<class EnumType>
38inline Foam::label Foam::Enum<EnumType>::size() const noexcept
39{
40 return keys_.size();
41}
42
43
44template<class EnumType>
45inline const Foam::List<Foam::word>&
47{
48 return keys_;
49}
50
51
52template<class EnumType>
53inline const Foam::List<int>&
55{
56 return vals_;
57}
58
59
60template<class EnumType>
61inline const Foam::List<Foam::word>&
63{
64 return keys_;
65}
66
67
68template<class EnumType>
71{
72 List<word> list(keys_);
73
74 Foam::sort(list);
75
76 return list;
77}
78
79
80template<class EnumType>
82{
83 keys_.clear();
84 vals_.clear();
85}
86
87
88template<class EnumType>
89inline Foam::label Foam::Enum<EnumType>::find(const word& enumName) const
90{
91 return keys_.find(enumName);
92}
93
94
95template<class EnumType>
96inline Foam::label Foam::Enum<EnumType>::find(const EnumType e) const
97{
98 return vals_.find(int(e));
99}
100
101
102template<class EnumType>
103inline bool Foam::Enum<EnumType>::found(const word& enumName) const
104{
105 return keys_.found(enumName);
106}
107
109template<class EnumType>
110inline bool Foam::Enum<EnumType>::found(const EnumType e) const
112 return vals_.found(int(e));
113}
115
116template<class EnumType>
117inline const Foam::word& Foam::Enum<EnumType>::get(const EnumType e) const
118{
119 const label idx = find(e);
121 if (idx < 0)
122 {
124 }
125
126 return keys_[idx];
127}
128
130template<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
143template<class EnumType>
144inline void Foam::Enum<EnumType>::write(const EnumType e, Ostream& os) const
145{
146 const label idx = find(e);
148 if (idx >= 0)
149 {
150 os << keys_[idx];
151 }
152}
154
155template<class EnumType>
156template<class OS>
157inline 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
175template<class EnumType>
177(
178 const Enum* eptr,
179 const label idx
180) noexcept
181:
182 ptr_(eptr),
183 idx_(idx)
184{}
185
186
187template<class EnumType>
188inline const Foam::word&
190{
191 return ptr_->names()[idx_];
192}
193
194
195template<class EnumType>
197{
198 return EnumType(ptr_->values()[idx_]);
199}
200
201
202template<class EnumType>
205{
206 ++idx_;
207 return *this;
208}
209
210
211template<class EnumType>
213(
214 const const_iterator& iter
215) const noexcept
216{
217 return idx_ == iter.idx_;
218}
219
220
221template<class EnumType>
223(
224 const const_iterator& iter
225) const noexcept
226{
227 return idx_ != iter.idx_;
228}
229
230
231template<class EnumType>
234{
235 return typename Enum<EnumType>::const_iterator(this);
237
238
239template<class EnumType>
242{
243 return typename Enum<EnumType>::const_iterator(this, this->size());
244}
245
246
247// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
248
249template<class EnumType>
251(
252 const word& enumName
253) const
255 return get(enumName);
256}
257
258
259template<class EnumType>
261(
262 const EnumType e
263) const
264{
265 return get(e);
266}
267
268
269// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
270
271template<class EnumType>
272inline Foam::Ostream& Foam::operator<<
273(
274 Ostream& os,
275 const Enum<EnumType>& list
276)
277{
278 return list.writeList(os);
280
281
282template<class EnumType>
283inline std::ostream& Foam::operator<<
284(
285 std::ostream& os,
286 const Enum<EnumType>& list
287)
288{
289 return list.writeList(os);
291
293// ************************************************************************* //
label k
bool found
A const_iterator for iterating an Enum list.
Definition: Enum.H:262
const_iterator & operator++() noexcept
Move to the next index.
Definition: EnumI.H:204
EnumType val() const
Enumeration value at the current index.
Definition: EnumI.H:196
const word & key() const
The name at the current index.
Definition: EnumI.H:189
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: Enum.H:61
const_iterator cbegin() const noexcept
Definition: EnumI.H:233
const List< int > & values() const noexcept
The list of enum values, in construction order.
Definition: EnumI.H:54
label find(const word &enumName) const
Find the index of the given name.
Definition: EnumI.H:89
bool empty() const noexcept
True if the enumeration list is empty.
Definition: EnumI.H:31
OS & writeList(OS &os, const label ununsed=0) const
Definition: EnumI.H:157
const_iterator cend() const noexcept
Definition: EnumI.H:241
const List< word > & names() const noexcept
The list of enum names, in construction order. Same as toc()
Definition: EnumI.H:46
label size() const noexcept
The number of name/value pairs for the enumeration.
Definition: EnumI.H:38
const List< word > & toc() const noexcept
The list of enum names, in construction order. Same as names()
Definition: EnumI.H:62
List< word > sortedToc() const
The sorted list of enum names.
Definition: EnumI.H:70
void clear()
Clear all entries.
Definition: EnumI.H:81
EnumType get(const word &enumName) const
The enumeration corresponding to the given name.
Definition: Enum.C:75
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
unsigned int get() const
Get value as unsigned, no range-checking.
Definition: PackedListI.H:302
A const_iterator for iterating across on values.
Definition: bitSet.H:505
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
virtual bool write()
Write the output fields.
A class for handling words, derived from Foam::string.
Definition: word.H:68
OBJstream os(runTime.globalPath()/outputName)
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:342
const direction noexcept
Definition: Scalar.H:223
dictionary dict
volScalarField & e
Definition: createFields.H:11