NamedEnum.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) 2011-2017 OpenFOAM Foundation
9 Copyright (C) 2017-2020 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27Class
28 Foam::NamedEnum
29
30Description
31 A NamedEnum is a wrapper around a list of names that represent
32 particular enumeration values.
33
34 \deprecated(2017-05) This class is retained for compatibility only and
35 should be used for any new code.
36 The Foam::Enum class is robuster, more flexible, easier to use.
37
38See Also
39 Foam::Enum
40
41SourceFiles
42 NamedEnum.C
43
44\*---------------------------------------------------------------------------*/
45
46#ifndef NamedEnum_H
47#define NamedEnum_H
48
49#include "HashTable.H"
50#include "wordList.H"
51#include <type_traits>
52
53// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54
55namespace Foam
56{
57// Forward declarations
59template<class EnumType, int nEnum> class NamedEnum;
60
61template<class EnumType, int nEnum>
63
64
65/*---------------------------------------------------------------------------*\
66 Class NamedEnum Declaration
67\*---------------------------------------------------------------------------*/
68
69template<class EnumType, int nEnum>
70class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
71{
72 //- The nEnum must be positive (non-zero)
73 static_assert(nEnum > 0, "nEnum must be positive (non-zero)");
74
75
76 // Private Member Data
77
78 //- The values for the enum
79 HashTable<int> lookup_;
80
81
82 // Private Member Functions
83
84 //- No copy construct
85 NamedEnum(const NamedEnum&) = delete;
86
87 //- No copy assignment
88 void operator=(const NamedEnum&) = delete;
89
90
91public:
92
93 //- The type of enumeration wrapped by NamedEnum
94 typedef EnumType value_type;
95
96
97 // Static data members
98
99 //- The set of names corresponding to the enumeration EnumType
100 static const char* names[nEnum];
101
102
103 // Constructors
104
105 //- Construct from names
106 NamedEnum();
107
108
109 // Member Functions
110
111 // Access
112
113 //- The number of lookup names for the enumeration
114 inline label size() const;
115
116 //- The list of enum names
117 inline wordList toc() const;
118
119 //- The sorted list of enum names
120 inline wordList sortedToc() const;
121
122 //- The list of enum names, in construction order
123 wordList words() const;
124
125 //- The list of enum values, in construction order
126 List<int> values() const;
127
128
129 // Query
130
131 //- Test if there is an enumeration corresponding to the given name.
132 inline bool found(const word& enumName) const;
133
134 //- Test if there is an enumeration corresponding to the given name.
135 inline bool hasEnum(const word& enumName) const;
136
137 //- Test if there is a name corresponding to the given enumeration.
138 bool hasName(const EnumType e) const;
139
140
141 // Lookup
142
143 //- Lookup the key in the dictionary and return the corresponding
144 // enumeration element based on its name.
145 // Fatal if anything is incorrect.
146 EnumType lookup
147 (
148 const word& key,
149 const dictionary& dict
150 ) const;
151
152 //- Find the key in the dictionary and return the corresponding
153 // enumeration element based on its name.
154 // Return the default value if the key was not found in the dictionary.
155 // Fatal if enumerated name was incorrect.
156 EnumType lookupOrDefault
157 (
158 const word& key,
159 const dictionary& dict,
160 const EnumType deflt
161 ) const;
162
163
164 // IO
165
166 //- Read a word from Istream and return the corresponding enumeration
167 EnumType read(Istream& is) const;
168
169 //- Write the name representation of the enumeration to an Ostream
170 // A noop if the enumeration wasn't found.
171 void write(const EnumType e, Ostream& os) const;
172
173
174 // Member Operators
175
176 //- Return the enumeration element corresponding to the given name
177 inline const EnumType operator[](const word& name) const;
178
179 //- Return the name of the given enumeration element
180 inline const char* operator[](const EnumType e) const;
181
182
183 // IOstream operators
184
185 //- Write names to Ostream, as per writeKeys() with shortListLen=10
186 friend Ostream& operator<< <EnumType, nEnum>
187 (
188 Ostream& os,
189 const NamedEnum<EnumType, nEnum>& wrapped
190 );
191
192};
193
194
195// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
196
197} // End namespace Foam
198
199// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
200
201#include "NamedEnumI.H"
202
203#ifdef NoRepository
204 #include "NamedEnum.C"
205#endif
206
207// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
208
209#endif
210
211// ************************************************************************* //
bool found
A HashTable similar to std::unordered_map.
Definition: HashTable.H:123
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
A NamedEnum is a wrapper around a list of names that represent particular enumeration values.
Definition: NamedEnum.H:58
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
Lookup type of boundary radiation properties.
Definition: lookup.H:66
A class for handling words, derived from Foam::string.
Definition: word.H:68
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
runTime write()
dictionary dict
volScalarField & e
Definition: createFields.H:11
#define FOAM_DEPRECATED_FOR(since, replacement)
Definition: stdFoam.H:52