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-2022 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  //- Find the index of the given name.
143  // \return position in list or -1 if not found.
144  inline label find(const word& enumName) const;
145 
146  //- Find the first index of given enumeration.
147  // \return position in list or -1 if not found.
148  inline label find(const EnumType e) const;
149 
150  //- True if there is an enumeration corresponding to the given name.
151  inline bool found(const word& enumName) const;
152 
153  //- True if there is a name corresponding to the given enumeration.
154  inline bool found(const EnumType e) const;
155 
156  //- The enumeration corresponding to the given name.
157  // FatalError if not found.
158  EnumType get(const word& enumName) const;
159 
160  //- The name corresponding to the given enumeration.
161  // Return an empty word if there is no corresponding name for it.
162  inline const word& get(const EnumType e) const;
163 
164  //- The enumeration corresponding to the given name.
165  // \return The enumeration or default if not found.
166  // \note Method name compatibility with HashTable
167  EnumType lookup(const word& enumName, const EnumType deflt) const;
168 
169 
170  // Read
171 
172  //- Get the key in the dictionary and return the corresponding
173  //- enumeration element based on its name.
174  // FatalIOError if anything is incorrect.
175  EnumType get
176  (
177  const word& key,
178  const dictionary& dict
179  ) const;
180 
181  //- Find the key in the dictionary and return the corresponding
182  //- enumeration element based on its name.
183  //
184  // \return The value found or default if not found in dictionary.
185  // FatalIOError if the enumeration is incorrect.
186  // Specifying failsafe downgrades the FatalIOError to an IOWarning.
187  EnumType getOrDefault
188  (
189  const word& key,
190  const dictionary& dict,
191  const EnumType deflt,
192  const bool failsafe = false
193  ) const;
194 
195  //- Find entry and assign to T val.
196  // FatalIOError if the enumeration is incorrect,
197  // or when it is mandatory but was not found.
198  //
199  // \return true if the entry was found.
200  bool readEntry
201  (
202  const word& key,
203  const dictionary& dict,
204  EnumType& val,
205  const bool mandatory = true
206  ) const;
207 
208  //- Find an entry if present, and assign to T val.
209  // FatalIOError if the enumeration is incorrect.
210  // Default search: non-recursive with patterns.
211  //
212  // \return true if the entry was found.
213  inline bool readIfPresent
214  (
215  const word& key,
216  const dictionary& dict,
217  EnumType& val
218  ) const;
219 
220 
221  // IO
222 
223  //- Read a word from Istream and return the corresponding enumeration
224  EnumType read(Istream& is) const;
225 
226  //- Read a word from Istream, lookup named enumeration.
227  // \return true on success. Fatal if mandatory and not found.
228  bool read
229  (
230  Istream& is,
231  EnumType& val,
232  const bool mandatory = true
233  ) const;
234 
235  //- Write the name representation of the enumeration to an Ostream
236  // A noop if the enumeration wasn't found.
237  inline void write(const EnumType e, Ostream& os) const;
238 
239  //- Write enumeration names as a list without line-breaks
240  //- to an output stream.
241  template<class OS>
242  inline OS& writeList(OS& os, const label ununsed=0) const;
243 
244 
245  // Member Operators
246 
247  //- Return the enumeration corresponding to the given name
248  // FatalError if the name is not found.
249  // Identical to get()
250  inline EnumType operator[](const word& enumName) const;
251 
252  //- Return the first name corresponding to the given enumeration,
253  //- or an empty word on failure.
254  // Identical to get()
255  inline const word& operator[](const EnumType e) const;
256 
257 
258  // Iteration
259 
260  //- A const_iterator for iterating an Enum list
261  // \note The iterator dereference returns the \b key
262  class const_iterator
263  {
264  //- The list being iterated
265  const Enum* ptr_;
266 
267  //- Index in the list
268  label idx_;
269 
270  public:
271 
272  //- Default construct, construct at given position
273  inline explicit const_iterator
274  (
275  const Enum* eptr = nullptr,
276  const label idx = 0
277  ) noexcept;
278 
279  //- The name at the current index
280  inline const word& key() const;
281 
282  //- Enumeration value at the current index
283  inline EnumType val() const;
284 
285  //- De-referencing returns the name (key)
286  // This is similar to HashSet (not HashTable!) and allows
287  // convenient output and traversing of the names
288  const word& operator*() const { return key(); }
289 
290  //- Move to the next index
291  inline const_iterator& operator++() noexcept;
292 
293  inline bool operator==(const const_iterator& iter) const noexcept;
294  inline bool operator!=(const const_iterator& iter) const noexcept;
295  };
296 
297  inline const_iterator cbegin() const noexcept;
298  inline const_iterator cend() const noexcept;
299 
300  const_iterator begin() const noexcept { return cbegin(); }
301  const_iterator end() const noexcept { return cend(); }
302 
303 
304  // Housekeeping
305 
306  //- Find the key in the dictionary and return the corresponding
307  //- enumeration element based on its name.
308  //
309  // \return The value found or default if not found in dictionary.
310  // FatalError (or Warning) if the enumeration was incorrect.
311  EnumType lookupOrDefault
312  (
313  const word& key,
314  const dictionary& dict,
315  const EnumType deflt,
316  const bool failsafe = false
317  ) const
318  {
319  return getOrDefault(key, dict, deflt, failsafe);
320  }
321 
322 
323  //- Deprecated(2020-11) use get() method
324  // \deprecated(2020-11) - use get() method
325  FOAM_DEPRECATED_FOR(2020-11, "get() method")
326  const word& operator()(const EnumType e) const
327  {
328  return get(e);
329  }
330 
331  //- Deprecated(2020-11) use two-parameter lookup() method
332  // \deprecated(2020-11) - use two-parameter lookup() method
333  FOAM_DEPRECATED_FOR(2020-11, "lookup() method")
334  EnumType operator()(const word& key, const EnumType deflt) const
335  {
336  return lookup(key, deflt);
337  }
338 
339  //- Deprecated(2020-11) use two-parameter lookup() method
340  // \deprecated(2020-11) - use two-parameter lookup() method
341  FOAM_DEPRECATED_FOR(2020-11, "lookup() method")
342  EnumType get(const word& key, const EnumType deflt) const
343  {
344  return lookup(key, deflt);
345  }
346 
347  //- Deprecated(2018-10) same as two-parameter get() method
348  // \deprecated(2018-10) - use two-parameter get() method
349  FOAM_DEPRECATED_FOR(2018-10, "get() method")
350  EnumType lookup(const word& key, const dictionary& dict) const
351  {
352  return get(key, dict);
353  }
354 
355  //- Append value/key pairs to the lists of known enumerations
356  // Does not check for duplicate entries
357  void append
358  (
359  std::initializer_list<std::pair<EnumType, const char*>> list
360  )
361  {
362  push_back(list);
363  }
364 };
365 
366 
367 // Ostream Operator
368 
369 //- Write enumeration names, without line-breaks (ie, FlatOutput)
370 template<class EnumType>
371 inline Ostream& operator<<(Ostream& os, const Enum<EnumType>& list);
372 
373 //- Write enumeration names, without line-breaks (ie, FlatOutput)
374 template<class EnumType>
375 inline std::ostream& operator<<(std::ostream& os, const Enum<EnumType>& list);
377 
378 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
379 
380 } // End namespace Foam
381 
382 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
383 
384 #include "EnumI.H"
385 
386 #ifdef NoRepository
387  #include "Enum.C"
388 #endif
389 
390 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
392 #endif
393 
394 // ************************************************************************* //
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:137
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:150
label find(const word &enumName) const
Find the index of the given name.
Definition: EnumI.H:82
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:120
EnumType lookupOrDefault(const word &key, const dictionary &dict, const EnumType deflt, const bool failsafe=false) const
Find the key in the dictionary and return the corresponding enumeration element based on its name...
Definition: Enum.H:405
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
bool found(const word &enumName) const
True if there is an enumeration corresponding to the given name.
Definition: EnumI.H:96
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
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 end() const noexcept
Definition: Enum.H:391
bool readEntry(const word &key, const dictionary &dict, EnumType &val, const bool mandatory=true) const
Find entry and assign to T val.
Definition: Enum.C:215
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
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:466
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:55
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:234
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:390
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
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
bool readIfPresent(const word &key, const dictionary &dict, EnumType &val) const
Find an entry if present, and assign to T val.
Definition: EnumI.H:125
const_iterator cbegin() const noexcept
Definition: EnumI.H:226
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:299
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
EnumType getOrDefault(const word &key, const dictionary &dict, const EnumType deflt, const bool failsafe=false) const
Find the key in the dictionary and return the corresponding enumeration element based on its name...
Definition: Enum.C:172
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