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-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 Class
27  Foam::Enum
28 
29 Description
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 
34 SourceFiles
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 
49 namespace Foam
50 {
51 
52 // Forward Declarations
53 class dictionary;
54 template<class EnumType> class Enum;
55 
56 /*---------------------------------------------------------------------------*\
57  Class Enum Declaration
58 \*---------------------------------------------------------------------------*/
59 
60 template<class EnumType>
61 class 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<EnumType>::value || std::is_integral<EnumType>::value,
76  "Enum must be enum or an integral type"
77  );
78 
79 public:
80 
81  // Typedefs
82 
83  //- The type of keys used
84  typedef word key_type;
85 
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  inline EnumType operator[](const word& enumName) const;
246 
247  //- Return the first name corresponding to the given enumeration,
248  //- or an empty word on failure.
249  // Identical to get()
250  inline const word& operator[](const EnumType e) const;
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  //- The list being iterated
260  const Enum* ptr_;
261 
262  //- Index in the list
263  label idx_;
264 
265  public:
266 
267  //- Default construct, construct at given position
268  inline explicit const_iterator
269  (
270  const Enum* eptr = nullptr,
271  const label idx = 0
272  ) noexcept;
273 
274  //- True if iterator points to an entry
275  inline bool good() const noexcept;
276 
277  //- The name at the current index
278  inline const word& key() const;
279 
280  //- Enumeration value at the current index
281  inline EnumType val() const;
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
289  inline const_iterator& operator++() noexcept;
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 the enumeration by 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 
307  // Housekeeping
308 
309  //- Same as getOrDefault()
310  FOAM_DEPRECATED_STRICT(2019-06, "getOrDefault() method")
311  EnumType lookupOrDefault
312  (
313  const word& key,
314  const dictionary& dict,
315  const EnumType deflt,
316  const bool warnOnly = false
317  ) const
318  {
319  return getOrDefault(key, dict, deflt, warnOnly);
320  }
321 
322  //- Deprecated(2020-11) use get() method
323  // \deprecated(2020-11) - use get() method
324  FOAM_DEPRECATED_FOR(2020-11, "get() method")
325  const word& operator()(const EnumType e) const
326  {
327  return get(e);
328  }
329 
330  //- Deprecated(2020-11) use two-parameter lookup() method
331  // \deprecated(2020-11) - use two-parameter lookup() method
332  FOAM_DEPRECATED_FOR(2020-11, "lookup() method")
333  EnumType operator()(const word& key, const EnumType deflt) const
334  {
335  return lookup(key, deflt);
336  }
337 
338  //- Deprecated(2020-11) use two-parameter lookup() method
339  // \deprecated(2020-11) - use two-parameter lookup() method
340  FOAM_DEPRECATED_FOR(2020-11, "lookup() method")
341  EnumType get(const word& key, const EnumType deflt) const
342  {
343  return lookup(key, deflt);
344  }
345 
346  //- Deprecated(2018-10) same as two-parameter get() method
347  // \deprecated(2018-10) - use two-parameter get() method
348  FOAM_DEPRECATED_FOR(2018-10, "get() method")
349  EnumType lookup(const word& key, const dictionary& dict) const
350  {
351  return get(key, dict);
352  }
353 
354  //- Append value/key pairs to the lists of known enumerations
355  // Does not check for duplicate entries
356  FOAM_DEPRECATED_STRICT(2023-06, "push_back() method")
357  void append
358  (
359  std::initializer_list<std::pair<EnumType, const char*>> list
360  )
361  {
362  push_back(list);
363  }
364 
365  //- Same as contains()
366  bool found(const word& key) const { return contains(key); }
367 
368  //- Same as contains()
369  bool found(const EnumType e) const { return contains(e); }
370 };
371 
372 
373 // Ostream Operator
374 
375 //- Write enumeration names, without line-breaks (ie, FlatOutput)
376 template<class EnumType>
377 inline Ostream& operator<<(Ostream& os, const Enum<EnumType>& list);
378 
379 //- Write enumeration names, without line-breaks (ie, FlatOutput)
380 template<class EnumType>
381 inline std::ostream& operator<<(std::ostream& os, const Enum<EnumType>& list);
382 
383 
384 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
386 } // End namespace Foam
387 
388 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
389 
390 #include "EnumI.H"
391 
392 #ifdef NoRepository
393  #include "Enum.C"
394 #endif
395 
396 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
397 
398 #endif
399 
400 // ************************************************************************* //
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
EnumType read(Istream &is) const
Read a word from Istream and return the corresponding enumeration.
Definition: Enum.C:102
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
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 found(const word &key) const
Same as contains()
Definition: Enum.H:474
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
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
const_iterator cfind(const word &key) const
Find the enumeration by name.
Definition: EnumI.H:236
bool contains(const word &enumName) const
True if there is an enumeration corresponding to the given name.
Definition: EnumI.H:82
Lookup type of boundary radiation properties.
Definition: lookup.H:57
EnumType value_type
The type of enumeration represented by the Enum.
Definition: Enum.H:90
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
const List< word > & toc() const noexcept
The list of enum names, in construction order. Same as names()
Definition: EnumI.H:55
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:65
void clear()
Clear all entries.
Definition: EnumI.H:74
const_iterator find(const word &key) const
Definition: Enum.H:393
const_iterator end() const noexcept
Definition: Enum.H:385
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
tmp< faMatrix< Type > > operator*(const areaScalarField::Internal &, const faMatrix< Type > &)
A class for handling words, derived from Foam::string.
Definition: word.H:63
const List< int > & values() const noexcept
The list of enum values, in construction order.
Definition: EnumI.H:47
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
#define FOAM_DEPRECATED_STRICT(since, replacement)
Definition: stdFoam.H:55
word key_type
The type of keys used.
Definition: Enum.H:76
void append(std::initializer_list< std::pair< EnumType, const char *>> list)
Append value/key pairs to the lists of known enumerations.
Definition: Enum.H:464
Enum() noexcept=default
Default construct, an empty list.
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
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
const_iterator begin() const noexcept
Definition: Enum.H:384
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
EnumType lookupOrDefault(const word &key, const dictionary &dict, const EnumType deflt, const bool warnOnly=false) const
Same as getOrDefault()
Definition: Enum.H:403
const_iterator cbegin() const noexcept
Definition: EnumI.H:220
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:297
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
bool empty() const noexcept
True if the enumeration list is empty.
Definition: EnumI.H:24
Namespace for OpenFOAM.
EnumType lookup(const word &enumName, const EnumType deflt) const
The enumeration corresponding to the given name.
Definition: Enum.C:85