Loading...
Searching...
No Matches
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 NOT 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.txx
43 NamedEnumI.H
44
45\*---------------------------------------------------------------------------*/
46
47#ifndef FoamCompat_NamedEnum_H
48#define FoamCompat_NamedEnum_H
49
50#include "HashTable.H"
51#include "wordList.H"
52#include <type_traits>
53
54// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56namespace Foam
57{
58// Forward declarations
59class dictionary;
60template<class EnumType, int nEnum> class NamedEnum;
61
62template<class EnumType, int nEnum>
64
65
66/*---------------------------------------------------------------------------*\
67 Class NamedEnum Declaration
68\*---------------------------------------------------------------------------*/
69
70template<class EnumType, int nEnum>
71class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
72{
73 //- The nEnum must be positive (non-zero)
74 static_assert(nEnum > 0, "nEnum must be positive (non-zero)");
75
76
77 // Private Member Data
78
79 //- The values for the enum
80 HashTable<int> lookup_;
81
82
83 // Private Member Functions
84
85 //- No copy construct
86 NamedEnum(const NamedEnum&) = delete;
87
88 //- No copy assignment
89 void operator=(const NamedEnum&) = delete;
90
91
92public:
93
94 //- The type of enumeration wrapped by NamedEnum
95 typedef EnumType value_type;
96
97
98 // Static data members
99
100 //- The set of names corresponding to the enumeration EnumType
101 static const char* names[nEnum];
102
103
104 // Constructors
105
106 //- Construct from names
107 NamedEnum();
108
109
110 // Member Functions
111
112 // Access
113
114 //- The number of lookup names for the enumeration
115 inline label size() const;
116
117 //- The list of enum names
118 inline wordList toc() const;
119
120 //- The sorted list of enum names
121 inline wordList sortedToc() const;
122
123 //- The list of enum names, in construction order
124 wordList words() const;
125
126 //- The list of enum values, in construction order
127 List<int> values() const;
128
129
130 // Query
131
132 //- Test if there is an enumeration corresponding to the given name.
133 inline bool found(const word& enumName) const;
134
135 //- Test if there is an enumeration corresponding to the given name.
136 inline bool hasEnum(const word& enumName) const;
137
138 //- Test if there is a name corresponding to the given enumeration.
139 bool hasName(const EnumType e) const;
140
141
142 // Lookup
143
144 //- Lookup the key in the dictionary and return the corresponding
145 // enumeration element based on its name.
146 // Fatal if anything is incorrect.
147 EnumType lookup
148 (
149 const word& key,
150 const dictionary& dict
151 ) const;
152
153 //- Find the key in the dictionary and return the corresponding
154 // enumeration element based on its name.
155 // Return the default value if the key was not found in the dictionary.
156 // Fatal if enumerated name was incorrect.
157 EnumType lookupOrDefault
158 (
159 const word& key,
160 const dictionary& dict,
161 const EnumType deflt
162 ) const;
163
164
165 // IO
166
167 //- Read a word from Istream and return the corresponding enumeration
168 EnumType read(Istream& is) const;
169
170 //- Write the name representation of the enumeration to an Ostream
171 // A noop if the enumeration wasn't found.
172 void write(const EnumType e, Ostream& os) const;
173
174
175 // Member Operators
176
177 //- Return the enumeration element corresponding to the given name
178 inline const EnumType operator[](const word& name) const;
179
180 //- Return the name of the given enumeration element
181 inline const char* operator[](const EnumType e) const;
182
183
184 // IOstream operators
185
186 //- Write names to Ostream, as per writeKeys() with shortListLen=10
187 friend Ostream& operator<< <EnumType, nEnum>
188 (
189 Ostream& os,
190 const NamedEnum<EnumType, nEnum>& wrapped
191 );
192
193};
194
195
196// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
197
198} // End namespace Foam
199
200// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
201
202#include "NamedEnumI.H"
203#include "NamedEnum.txx"
204
205// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
206
207#endif
208
209// ************************************************************************* //
bool found
A HashTable similar to std::unordered_map.
Definition HashTable.H:124
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
A NamedEnum is a wrapper around a list of names that represent particular enumeration values.
Definition NamedEnum.H:55
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
Lookup type of boundary radiation properties.
Definition lookup.H:60
A class for handling words, derived from Foam::string.
Definition word.H:66
OBJstream os(runTime.globalPath()/outputName)
auto & names
Namespace for OpenFOAM.
List< word > wordList
List of word.
Definition fileName.H:60
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition int32.H:127
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition NamedEnum.H:66
runTime write()
dictionary dict
volScalarField & e