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