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-2019 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
70  List<int> vals_;
71 
72 
73 public:
74 
75  //- The type of enumeration represented by the Enum
76  typedef EnumType value_type;
77 
78  // Allow enums and integrals (should fit within an int)
79  static_assert
80  (
81  std::is_enum<EnumType>::value || std::is_integral<EnumType>::value,
82  "Enum must be enum or an integral type"
83  );
84 
85 
86  // Constructors
87 
88  //- Construct null (empty list)
89  Enum() = default;
90 
91  //- Construct from a values/names list.
92  // Duplicate values are permitted (eg, for aliases).
93  // Duplicate names are permitted, but won't make much sense.
94  explicit Enum
95  (
96  std::initializer_list<std::pair<EnumType, const char*>> list
97  );
98 
99 
100  // Member Functions
101 
102  // Access
103 
104  //- True if the enumeration list is empty.
105  inline bool empty() const noexcept;
106 
107  //- The number of name/value pairs for the enumeration.
108  inline label size() const noexcept;
109 
110  //- The list of enum names, in construction order. Same as toc()
111  inline const List<word>& names() const;
112 
113  //- The list of enum names, in construction order. Same as names()
114  inline const List<word>& toc() const;
115 
116  //- The sorted list of enum names.
117  List<word> sortedToc() const;
118 
119  //- The list of enum values, in construction order.
120  inline const List<int>& values() const;
121 
122 
123  // Modify
124 
125  //- Clear all entries
126  inline void clear();
127 
128  //- Append value/key pairs to the lists of known enumerations
129  // Does not check for duplicate entries
130  void append
131  (
132  std::initializer_list<std::pair<EnumType, const char*>> list
133  );
134 
135 
136  // Query
137 
138  //- Find the index of the given name.
139  // \return position in list or -1 if not found.
140  inline label find(const word& enumName) const;
141 
142  //- Find the first index of given enumeration.
143  // \return position in list or -1 if not found.
144  inline label find(const EnumType e) const;
145 
146  //- Test if there is an enumeration corresponding to the given name.
147  inline bool found(const word& enumName) const;
148 
149  //- Test if there is a name corresponding to the given enumeration.
150  inline bool found(const EnumType e) const;
151 
152  //- The enumeration corresponding to the given name.
153  // FatalError if not found.
154  EnumType get(const word& enumName) const;
155 
156  //- The enumeration corresponding to the given name.
157  // \return The enumeration or default if not found.
158  EnumType get(const word& enumName, const EnumType deflt) const;
159 
160  //- The name corresponding to the given enumeration.
161  // Return an empty word if not found.
162  inline const word& get(const EnumType e) const;
163 
164 
165  // Read
166 
167  //- Get the key in the dictionary and return the corresponding
168  //- enumeration element based on its name.
169  // FatalIOError if anything is incorrect.
170  EnumType get
171  (
172  const word& key,
173  const dictionary& dict
174  ) const;
175 
176  //- Find the key in the dictionary and return the corresponding
177  //- enumeration element based on its name.
178  //
179  // \return The value found or default if not found in dictionary.
180  // FatalIOError if the enumeration is incorrect.
181  // Specifying failsafe downgrades the FatalIOError to an IOWarning.
182  EnumType getOrDefault
183  (
184  const word& key,
185  const dictionary& dict,
186  const EnumType deflt,
187  const bool failsafe = false
188  ) const;
189 
190  //- Find entry and assign to T val.
191  // FatalIOError if the enumeration is incorrect,
192  // or when it is mandatory but was not found.
193  //
194  // \return true if the entry was found.
195  bool readEntry
196  (
197  const word& key,
198  const dictionary& dict,
199  EnumType& val,
200  const bool mandatory = true
201  ) const;
202 
203  //- Find an entry if present, and assign to T val.
204  // FatalIOError if the enumeration is incorrect.
205  // Default search: non-recursive with patterns.
206  //
207  // \return true if the entry was found.
208  bool readIfPresent
209  (
210  const word& key,
211  const dictionary& dict,
212  EnumType& val
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 noop if the enumeration wasn't found.
232  inline void write(const EnumType e, Ostream& os) const;
233 
234  //- Write the names as a list to an Ostream.
235  // Default is without line-breaks.
236  inline Ostream& writeList(Ostream& os, const label shortLen=0) const;
237 
238 
239  // Member Operators
240 
241  //- Return the enumeration corresponding to the given name
242  // FatalError if the name is not found.
243  // Identical to single parameter get()
244  inline EnumType operator[](const word& enumName) const;
245 
246  //- Return the first name corresponding to the given enumeration,
247  //- or an empty word on failure.
248  // Identical to single parameter get()
249  inline const word& operator[](const EnumType e) const;
250 
251  //- Return the first name corresponding to the given enumeration,
252  //- or an empty word on failure.
253  // Identical to single parameter get()
254  inline const word& operator()(const EnumType e) const;
255 
256  //- Return the enumeration corresponding to the given name,
257  //- or deflt if the name is not found.
258  inline EnumType operator()
259  (
260  const word& enumName,
261  const EnumType deflt
262  ) const;
263 
264 
265  // Housekeeping
266 
267  //- Find the key in the dictionary and return the corresponding
268  //- enumeration element based on its name.
269  //
270  // \return The value found or default if not found in dictionary.
271  // FatalError (or Warning) if the enumeration was incorrect.
273  (
274  const word& key,
275  const dictionary& dict,
276  const EnumType deflt,
277  const bool failsafe = false
278  ) const
279  {
280  return getOrDefault(key, dict, deflt, failsafe);
281  }
282 
283  //- Deprecated(2018-10) same as two-parameter get()
284  // \deprecated(2018-10) - use two-parameter get() method
285  EnumType FOAM_DEPRECATED_FOR(2018-10, "get() method")
286  lookup(const word& key, const dictionary& dict) const
287  {
288  return get(key, dict);
289  }
290 };
291 
292 
293 // Ostream Operator
294 
295 //- Write enumeration names, without line-breaks (ie, FlatOutput)
296 template<class EnumType>
297 inline Ostream& operator<<(Ostream& os, const Enum<EnumType>& list);
298 
299 //- Write enumeration names, without line-breaks (ie, FlatOutput)
300 template<class EnumType>
301 inline std::ostream& operator<<(std::ostream& os, const Enum<EnumType>& list);
302 
303 
304 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
305 
306 } // End namespace Foam
307 
308 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
309 
310 #include "EnumI.H"
311 
312 #ifdef NoRepository
313  #include "Enum.C"
314 #endif
315 
316 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
317 
318 #endif
319 
320 // ************************************************************************* //
Foam::Enum::lookupOrDefault
EnumType lookupOrDefault(const word &key, const dictionary &dict, const EnumType deflt, const bool failsafe=false) const
Definition: Enum.H:272
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::Enum::dict
EnumType const dictionary &const dict
Definition: Enum.H:286
Foam::Enum::found
bool found(const word &enumName) const
Test if there is an enumeration corresponding to the given name.
Definition: EnumI.H:88
Foam::Enum::names
const List< word > & names() const
The list of enum names, in construction order. Same as toc()
Definition: EnumI.H:45
Foam::Enum::empty
bool empty() const noexcept
True if the enumeration list is empty.
Definition: EnumI.H:31
Foam::Enum::Enum
Enum()=default
Construct null (empty list)
Foam::Enum::writeList
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write the names as a list to an Ostream.
Definition: EnumI.H:117
Foam::Enum::get
EnumType get(const word &enumName) const
The enumeration corresponding to the given name.
Definition: Enum.C:86
wordList.H
Foam::Enum::value_type
EnumType value_type
The type of enumeration represented by the Enum.
Definition: Enum.H:75
Enum.C
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::Enum::write
void write(const EnumType e, Ostream &os) const
Write the name representation of the enumeration to an Ostream.
Definition: EnumI.H:127
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::Enum::values
const List< int > & values() const
The list of enum values, in construction order.
Definition: EnumI.H:59
Foam::Enum::getOrDefault
EnumType getOrDefault(const word &key, const dictionary &dict, const EnumType deflt, const bool failsafe=false) const
Definition: Enum.C:190
Foam::Enum::FOAM_DEPRECATED_FOR
EnumType FOAM_DEPRECATED_FOR(2018-10, "get() method") lookup(const word &key
Deprecated(2018-10) same as two-parameter get()
Foam::radiation::lookup
Lookup type of boundary radiation properties.
Definition: lookup.H:63
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
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:67
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:102
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:66
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
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: Enum.C:271
EnumI.H
Foam::Enum::sortedToc
List< word > sortedToc() const
The sorted list of enum names.
Definition: Enum.C:55
Foam::Enum::find
label find(const word &enumName) const
Find the index of the given name.
Definition: EnumI.H:74
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:120
Foam::Enum::toc
const List< word > & toc() const
The list of enum names, in construction order. Same as names()
Definition: EnumI.H:52
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102
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:233