Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Class
Foam::Enum
Description
A Enum is a wrapper around a list of names/values that represent
particular enumeration values.
SourceFiles
Enum.C
\*---------------------------------------------------------------------------*/
#ifndef Enum_H
#define Enum_H
#include "wordList.H"
#include <initializer_list>
#include <utility>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// Forward declarations
class dictionary;
template<class EnumType> class Enum;
template<class EnumType>
Ostream& operator<<(Ostream& os, const Enum<EnumType>& wrapped);
/*---------------------------------------------------------------------------*\
Class Enum Declaration
\*---------------------------------------------------------------------------*/
template<class EnumType>
class Enum
{
// Private Member Data
//- The names for the enum
List<word> names_;
//- The values for the enum
List<int> values_;
// Private Member Functions
//- The index for the given name. -1 if not found.
label getIndex(const word& enumName) const;
//- The index for the given enumeration. -1 if not found.
label getIndex(const EnumType e) const;
//- Lookup enumeration corresponding to the given name.
// FatalError if not found.
EnumType getEnum(const word& enumName) const;
//- Lookup name corresponding to the given enumeration.
// Return empty word if not found.
const word& getName(const EnumType e) const;
//- Disallow default bitwise copy construct
Enum(const Enum&) = delete;
//- Disallow default bitwise assignment
void operator=(const Enum&) = delete;
public:
//- The type of enumeration wrapped by Enum
typedef EnumType value_type;
// Constructors
//- Construct from a values/names list.
// Duplicate values are permitted (eg, for aliases).
// Duplicate names are permitted, but won't make much sense.
explicit Enum(std::initializer_list<std::pair<EnumType, word>> lst);
//- Construct from a list of names with values incremented from the
//- specified start value.
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
Enum(const EnumType start, std::initializer_list<word> lst);
// Member Functions
// Access
//- The number of lookup names for the enumeration
inline label size() const;
//- The list of enum names, in construction order
inline const List<word>& names() const;
//- The list of enum names, in construction order
inline const List<word>& toc() const;
//- The sorted list of enum names
List<word> sortedToc() const;
//- The list of enum values, in construction order
inline const List<int>& values() const;
// Query
//- Test if there is an enumeration corresponding to the given name.
inline bool hasEnum(const word& enumName) const;
//- Test if there is a name corresponding to the given enumeration.
inline bool hasName(const EnumType e) const;
// Lookup
//- Lookup the key in the dictionary and return the corresponding
//- enumeration element based on its name.
// Fatal if anything is incorrect.
EnumType lookup
(
const word& key,
const dictionary& dict
) const;
//- Find the key in the dictionary and return the corresponding
//- enumeration element based on its name.
// Return the default value if the key was not found in the dictionary.
// Fatal if the enumerated name was incorrect.
EnumType lookupOrDefault
(
const word& key,
const dictionary& dict,
const EnumType deflt
) const;
//- Find the key in the dictionary and return the corresponding
//- enumeration element based on its name.
// Return the default value if the key was not found in the dictionary
// or if the enumerated name was incorrect (emit warning)
EnumType lookupOrFailsafe
(
const word& key,
const dictionary& dict,
const EnumType deflt
) const;
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// IO
//- Read a word from Istream and return the corresponding enumeration
EnumType read(Istream& is) const;
//- Write the name representation of the enumeration to an Ostream
// A noop if the enumeration wasn't found.
void write(const EnumType e, Ostream& os) const;
//- Write the names as a list to an Ostream
inline Ostream& writeList
(
Ostream& os,
const label shortListLen=0
) const;
// Member Operators
//- Return the enumeration corresponding to the given name
// Fatal if the name cannot be found.
inline const EnumType operator[](const word& name) const;
//- Return the first name corresponding to the given enumeration.
// Returns an empty word on failure.
inline const word& operator[](const EnumType e) const;
// IOstream operators
//- Write names to Ostream, as per writeList() with shortListLen=10
friend Ostream& operator<< <EnumType>
(
Ostream& os,
const Enum<EnumType>& wrapped
);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "EnumI.H"
#ifdef NoRepository
#include "Enum.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //