Loading...
Searching...
No Matches
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-2025 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>
32{
33 return keys_.empty();
34}
35
36
37template<class EnumType>
38inline Foam::label Foam::Enum<EnumType>::size() const noexcept
40 return keys_.size();
41}
42
43
44template<class EnumType>
45inline const Foam::List<Foam::word>&
48 return keys_;
49}
50
51
52template<class EnumType>
53inline const Foam::List<int>&
56 return vals_;
57}
58
59
60template<class EnumType>
61inline const Foam::List<Foam::word>&
64 return keys_;
65}
66
67
68template<class EnumType>
71{
72 List<word> list(keys_);
73
75
76 return list;
77}
78
79
80template<class EnumType>
83 keys_.clear();
84 vals_.clear();
85}
86
87
88template<class EnumType>
89inline bool Foam::Enum<EnumType>::contains(const word& key) const
90{
91 return (!key.empty() && keys_.contains(key));
92}
93
94
95template<class EnumType>
96inline bool Foam::Enum<EnumType>::contains(const EnumType e) const
97{
98 return vals_.contains(int(e));
99}
100
101
102template<class EnumType>
103inline 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;
111
112 return keys_[idx];
113}
114
115
116template<class EnumType>
118(
119 const word& key,
120 const dictionary& dict,
121 EnumType& val,
122 const bool warnOnly
123) const
125 // Reading is non-mandatory
126 return readEntry(key, dict, val, false, warnOnly);
127}
128
130template<class EnumType>
131inline void Foam::Enum<EnumType>::write(const EnumType e, Ostream& os) const
132{
133 const label idx = vals_.find(int(e));
135 if (idx >= 0)
136 {
137 os << keys_[idx];
138 }
140
141
142template<class EnumType>
143template<class OS>
144inline 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
162template<class EnumType>
164(
165 const Enum* eptr,
166 const label idx
167) noexcept
168:
169 ptr_(eptr),
170 idx_(idx)
171{}
172
173
174template<class EnumType>
175inline const Foam::word&
177{
178 return ptr_->names()[idx_];
179}
180
181
182template<class EnumType>
184{
185 return EnumType(ptr_->values()[idx_]);
186}
187
188
189template<class EnumType>
192 return (ptr_ && idx_ >= 0 && idx_ < ptr_->size());
193}
194
195
196template<class EnumType>
200 ++idx_;
201 return *this;
202}
203
204
205template<class EnumType>
207(
208 const const_iterator& iter
209) const noexcept
210{
211 return idx_ == iter.idx_;
212}
213
214
215template<class EnumType>
217(
218 const const_iterator& iter
219) const noexcept
221 return idx_ != iter.idx_;
222}
223
224
225template<class EnumType>
229 return typename Enum<EnumType>::const_iterator(this);
230}
231
232
233template<class EnumType>
237 return typename Enum<EnumType>::const_iterator(this, this->size());
238}
239
240
241template<class EnumType>
243Foam::Enum<EnumType>::cfind(const word& key) const
244{
245 const label idx = (key.empty() ? -1 : keys_.find(key));
246
247 return typename Enum<EnumType>::const_iterator
248 (
249 this,
250 (idx >= 0 ? idx : this->size())
251 );
252}
253
254
255template<class EnumType>
257Foam::Enum<EnumType>::cfind(const EnumType e) const
258{
259 const label idx = vals_.find(int(e));
260
261 return typename Enum<EnumType>::const_iterator
262 (
263 this,
264 (idx >= 0 ? idx : this->size())
265 );
266}
267
268
269// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
270
271template<class EnumType>
272inline Foam::Ostream& Foam::operator<<
273(
274 Ostream& os,
275 const Enum<EnumType>& list
277{
278 return list.writeList(os);
279}
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
292
293// ************************************************************************* //
label k
A const_iterator for iterating an Enum list.
Definition Enum.H:327
const_iterator & operator++() noexcept
Move to the next index.
Definition EnumI.H:191
EnumType val() const
Enumeration value at the current index.
Definition EnumI.H:176
bool good() const noexcept
True if iterator points to an entry.
Definition EnumI.H:183
const word & key() const
The name at the current index.
Definition EnumI.H:169
const_iterator(const Enum *eptr=nullptr, const label idx=0) noexcept
Default construct, construct at given position.
Definition EnumI.H:157
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition Enum.H:57
const_iterator cbegin() const noexcept
Definition EnumI.H:220
const List< int > & values() const noexcept
The list of enum values, in construction order.
Definition EnumI.H:47
bool empty() const noexcept
True if the enumeration list is empty.
Definition EnumI.H:24
bool readEntry(const word &key, const dictionary &dict, injectionMethod &val, const bool mandatory=true, const bool warnOnly=false) const
Definition Enum.C:218
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
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
Enum() noexcept=default
Default construct, an empty list.
const_iterator cend() const noexcept
Definition EnumI.H:228
void write(const EnumType e, Ostream &os) const
Write the name representation of the enumeration to an Ostream.
Definition EnumI.H:124
const List< word > & names() const noexcept
The list of enum names, in construction order. Same as toc().
Definition EnumI.H:39
label size() const noexcept
The number of name/value pairs for the enumeration.
Definition EnumI.H:31
const_iterator cfind(const word &key) const
Find key/value pair by enumeration name.
Definition EnumI.H:236
const List< word > & toc() const noexcept
The list of enum names, in construction order. Same as names().
Definition EnumI.H:55
List< word > sortedToc() const
The sorted list of enum names.
Definition EnumI.H:63
void clear()
Clear all entries.
Definition EnumI.H:74
EnumType get(const word &enumName) const
The enumeration corresponding to the given name.
Definition Enum.C:68
bool contains(const word &enumName) const
True if there is an enumeration corresponding to the given name.
Definition EnumI.H:82
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
void clear()
Clear the list, i.e. set size to zero.
Definition ListI.H:133
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
A class for handling words, derived from Foam::string.
Definition word.H:66
static const word null
An empty word.
Definition word.H:84
OBJstream os(runTime.globalPath()/outputName)
void sort(UList< T > &list)
Sort the list.
Definition UList.C:283
const direction noexcept
Definition scalarImpl.H:265
dictionary dict
volScalarField & e