Loading...
Searching...
No Matches
Enum.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
26Class
27 Foam::Enum
28
29Description
30 Enum is a wrapper around a list of names/values that represent particular
31 enumeration (or int) values.
32 All dictionary searches use a literal (not regex).
33
34SourceFiles
35 Enum.C
36 EnumI.H
37
38\*---------------------------------------------------------------------------*/
39
40#ifndef Foam_Enum_H
41#define Foam_Enum_H
42
43#include "wordList.H"
44#include <ostream>
45#include <utility>
46
47// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48
49namespace Foam
50{
51
52// Forward Declarations
53class dictionary;
54template<class EnumType> class Enum;
55
56/*---------------------------------------------------------------------------*\
57 Class Enum Declaration
58\*---------------------------------------------------------------------------*/
59
60template<class EnumType>
61class Enum
62{
63 // Private Member Data
64
65 //- The names for the enum
66 List<word> keys_;
67
68 //- The values for the enum, stored as int
69 List<int> vals_;
70
71
72 // Allow enums and integrals (should fit within an int)
73 static_assert
74 (
75 std::is_enum_v<EnumType> || std::is_integral_v<EnumType>,
76 "Enum must be enum or an integral type"
77 );
78
79public:
80
81 // Typedefs
82
83 //- The type of keys used
84 typedef word key_type;
86 //- The type of enumeration represented by the Enum
87 typedef EnumType value_type;
88
89
90 // Constructors
91
92 //- Default construct, an empty list
93 Enum() noexcept = default;
94
95 //- Construct from a values/names list.
96 // Duplicate values are permitted (eg, for aliases).
97 // Duplicate names are permitted, but won't make much sense.
98 explicit Enum
99 (
100 std::initializer_list<std::pair<EnumType, const char*>> list
101 );
102
103
104 // Member Functions
105
106 // Access
107
108 //- True if the enumeration list is empty.
109 inline bool empty() const noexcept;
110
111 //- The number of name/value pairs for the enumeration.
112 inline label size() const noexcept;
113
114 //- The list of enum names, in construction order. Same as toc()
115 inline const List<word>& names() const noexcept;
116
117 //- The list of enum values, in construction order.
118 inline const List<int>& values() const noexcept;
119
120 //- The list of enum names, in construction order. Same as names()
121 inline const List<word>& toc() const noexcept;
122
123 //- The sorted list of enum names.
124 inline List<word> sortedToc() const;
125
126
127 // Modify
128
129 //- Clear all entries
130 inline void clear();
131
132 //- Append value/key pairs to the lists of known enumerations
133 // Does not check for duplicate entries
134 void push_back
135 (
136 std::initializer_list<std::pair<EnumType, const char*>> list
137 );
138
139
140 // Query
141
142 //- True if there is an enumeration corresponding to the given name.
143 inline bool contains(const word& enumName) const;
144
145 //- True if there is a name corresponding to the given enumeration.
146 inline bool contains(const EnumType e) const;
147
148 //- The enumeration corresponding to the given name.
149 // FatalError if not found.
150 EnumType get(const word& enumName) const;
151
152 //- The name corresponding to the given enumeration.
153 // Return an empty word if there is no corresponding name for it.
154 inline const word& get(const EnumType e) const;
155
156 //- The enumeration corresponding to the given name.
157 // \return The enumeration or default if not found.
158 // \note Method name compatibility with HashTable
159 EnumType lookup(const word& enumName, const EnumType deflt) const;
160
161
162 // Read
163
164 //- Get the key in the dictionary and return the corresponding
165 //- enumeration element based on its name.
166 // FatalIOError if anything is incorrect.
167 EnumType get
168 (
169 const word& key,
170 const dictionary& dict
171 ) const;
172
173 //- Find the key in the dictionary and return the corresponding
174 //- enumeration element based on its name.
175 //
176 // \return The value found or default if not found in dictionary.
177 // FatalIOError if the enumeration is incorrect.
178 // Specifying warnOnly downgrades the FatalIOError to an IOWarning.
179 EnumType getOrDefault
180 (
181 const word& key,
182 const dictionary& dict,
183 const EnumType deflt,
184 const bool warnOnly = false
185 ) const;
186
187 //- Find entry and assign to T val.
188 // FatalIOError if the enumeration is incorrect,
189 // or when it is mandatory but was not found.
190 //
191 // (TDB: handle IOobjectOption::readOption instead)
192 // \return true if the entry was found.
193 bool readEntry
194 (
195 const word& key,
196 const dictionary& dict,
197 EnumType& val,
198 const bool mandatory = true,
199 const bool warnOnly = false
200 ) const;
201
202 //- Find an entry if present, and assign to T val.
203 // FatalIOError if the enumeration is incorrect.
204 // Default search: non-recursive with patterns.
205 //
206 // \return true if the entry was found.
207 inline bool readIfPresent
208 (
209 const word& key,
210 const dictionary& dict,
211 EnumType& val,
212 const bool warnOnly = false
213 ) const;
214
215
216 // IO
217
218 //- Read a word from Istream and return the corresponding enumeration
219 EnumType read(Istream& is) const;
220
221 //- Read a word from Istream, lookup named enumeration.
222 // \return true on success. Fatal if mandatory and not found.
223 bool read
224 (
225 Istream& is,
226 EnumType& val,
227 const bool mandatory = true
228 ) const;
229
230 //- Write the name representation of the enumeration to an Ostream
231 // A no-op if the enumeration does not have a corresponding name.
232 inline void write(const EnumType e, Ostream& os) const;
233
234 //- Write enumeration names as a list without line-breaks
235 //- to an output stream.
236 template<class OS>
237 inline OS& writeList(OS& os, const label ununsed=0) const;
238
239
240 // Member Operators
241
242 //- Return the enumeration corresponding to the given name
243 // FatalError if the name is not found.
244 // Identical to get()
245 EnumType operator[](const word& k) const { return get(k); }
246
247 //- Return the first name corresponding to the given enumeration,
248 //- or an empty word if not found.
249 // Identical to get()
250 const word& operator[](EnumType e) const { return get(e); }
251
252
253 // Iteration
254
255 //- A const_iterator for iterating an Enum list
256 // \note The iterator dereference returns the \b key
257 class const_iterator
258 {
259 const Enum* ptr_;
260 label idx_;
261
262 public:
263
264 //- Default construct, construct at given position
265 inline explicit const_iterator
266 (
267 const Enum* eptr = nullptr,
268 const label idx = 0
269 ) noexcept;
270
271 //- True if iterator points to an entry
272 inline bool good() const noexcept;
273
274 //- The name at the current index
275 inline const word& key() const;
276
277 //- Enumeration value at the current index
278 inline EnumType val() const;
279
280 //- True if iterator points to an entry
281 explicit operator bool() const noexcept { return good(); }
282
283 //- De-referencing returns the name (key)
284 // This is similar to HashSet (not HashTable!) and allows
285 // convenient output and traversing of the names
286 const word& operator*() const { return key(); }
287
288 //- Move to the next index
290
291 inline bool operator==(const const_iterator& iter) const noexcept;
292 inline bool operator!=(const const_iterator& iter) const noexcept;
293 };
294
295 inline const_iterator cbegin() const noexcept;
296 inline const_iterator cend() const noexcept;
297
298 const_iterator begin() const noexcept { return cbegin(); }
299 const_iterator end() const noexcept { return cend(); }
300
301 //- Find key/value pair by enumeration name.
302 // Equal to cend() if not found, or test if good()
303 inline const_iterator cfind(const word& key) const;
304 const_iterator find(const word& key) const { return cfind(key); }
305
306 //- Find key/value pair by enumeration value.
307 // Equal to cend() if not found, or test if good()
308 inline const_iterator cfind(const EnumType e) const;
309 const_iterator find(const EnumType e) const { return cfind(e); }
310
311
312 // Housekeeping
313
314 //- Same as getOrDefault()
315 FOAM_DEPRECATED_STRICT(2019-06, "getOrDefault() method")
317 (
318 const word& key,
319 const dictionary& dict,
320 const EnumType deflt,
321 const bool warnOnly = false
322 ) const
323 {
324 return getOrDefault(key, dict, deflt, warnOnly);
325 }
327 //- Deprecated(2020-11) use get() method
328 // \deprecated(2020-11) - use get() method
329 FOAM_DEPRECATED_FOR(2020-11, "get() method")
330 const word& operator()(const EnumType e) const
331 {
332 return get(e);
333 }
334
335 //- Deprecated(2020-11) use two-parameter lookup() method
336 // \deprecated(2020-11) - use two-parameter lookup() method
337 FOAM_DEPRECATED_FOR(2020-11, "lookup() method")
338 EnumType operator()(const word& key, const EnumType deflt) const
339 {
340 return lookup(key, deflt);
341 }
342
343 //- Deprecated(2020-11) use two-parameter lookup() method
344 // \deprecated(2020-11) - use two-parameter lookup() method
345 FOAM_DEPRECATED_FOR(2020-11, "lookup() method")
346 EnumType get(const word& key, const EnumType deflt) const
347 {
348 return lookup(key, deflt);
349 }
350
351 //- Deprecated(2018-10) same as two-parameter get() method
352 // \deprecated(2018-10) - use two-parameter get() method
353 FOAM_DEPRECATED_FOR(2018-10, "get() method")
354 EnumType lookup(const word& key, const dictionary& dict) const
355 {
356 return get(key, dict);
357 }
358
359 //- Append value/key pairs to the lists of known enumerations
360 // Does not check for duplicate entries
361 FOAM_DEPRECATED_STRICT(2023-06, "push_back() method")
362 void append
363 (
364 std::initializer_list<std::pair<EnumType, const char*>> list
365 )
366 {
367 push_back(list);
369
370 //- Same as contains()
371 bool found(const word& key) const { return contains(key); }
372
373 //- Same as contains()
374 bool found(const EnumType e) const { return contains(e); }
375};
376
377
378// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
379
380// Ostream Operator
381
382//- Write enumeration names, without line-breaks (ie, FlatOutput)
383template<class EnumType>
384inline Ostream& operator<<(Ostream& os, const Enum<EnumType>& list);
385
386//- Write enumeration names, without line-breaks (ie, FlatOutput)
387template<class EnumType>
388inline std::ostream& operator<<(std::ostream& os, const Enum<EnumType>& list);
389
390
391// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
392
393} // End namespace Foam
394
395// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
396
397#include "EnumI.H"
398
399#ifdef NoRepository
400 #include "Enum.C"
401#endif
402
403// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
404
405#endif
406
407// ************************************************************************* //
label k
bool found
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 word & operator*() const
De-referencing returns the name (key).
Definition Enum.H:368
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
EnumType lookupOrDefault(const word &key, const dictionary &dict, const EnumType deflt, const bool warnOnly=false) const
Same as getOrDefault().
Definition Enum.H:409
const_iterator begin() const noexcept
Definition Enum.H:382
const List< int > & values() const noexcept
The list of enum values, in construction order.
Definition EnumI.H:47
EnumType operator[](const word &k) const
Return the enumeration corresponding to the given name.
Definition Enum.H:308
bool empty() const noexcept
True if the enumeration list is empty.
Definition EnumI.H:24
bool readEntry(const word &key, const dictionary &dict, EnumType &val, const bool mandatory=true, const bool warnOnly=false) const
Find entry and assign to T val.
Definition Enum.C:218
void push_back(std::initializer_list< std::pair< EnumType, const char * > > list)
Append value/key pairs to the lists of known enumerations.
Definition Enum.C:49
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
const_iterator end() const noexcept
Definition Enum.H:383
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.
EnumType value_type
The type of enumeration represented by the Enum.
Definition Enum.H:90
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 find(const EnumType e) const
Definition Enum.H:399
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
bool found(const EnumType e) const
Same as contains().
Definition Enum.H:485
List< word > sortedToc() const
The sorted list of enum names.
Definition EnumI.H:63
EnumType read(Istream &is) const
Read a word from Istream and return the corresponding enumeration.
Definition Enum.C:102
const_iterator find(const word &key) const
Definition Enum.H:391
void clear()
Clear all entries.
Definition EnumI.H:74
const word & operator[](EnumType e) const
Return the first name corresponding to the given enumeration, or an empty word if not found.
Definition Enum.H:316
word key_type
The type of keys used.
Definition Enum.H:85
bool found(const word &key) const
Same as contains().
Definition Enum.H:480
EnumType getOrDefault(const word &key, const dictionary &dict, const EnumType deflt, const bool warnOnly=false) const
Find the key in the dictionary and return the corresponding enumeration element based on its name.
Definition Enum.C:173
EnumType lookup(const word &enumName, const EnumType deflt) const
The enumeration corresponding to the given name.
Definition Enum.C:85
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
void append(std::initializer_list< std::pair< EnumType, const char * > > list)
Append value/key pairs to the lists of known enumerations.
Definition Enum.H:470
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
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
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
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
const direction noexcept
Definition scalarImpl.H:265
dictionary dict
volScalarField & e
#define FOAM_DEPRECATED_FOR(since, replacement)
Definition stdFoam.H:43
#define FOAM_DEPRECATED_STRICT(since, replacement)
Definition stdFoam.H:53