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 -------------------------------------------------------------------------------
11 License
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 
27 Class
28  Foam::NamedEnum
29 
30 Description
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 
38 See Also
39  Foam::Enum
40 
41 SourceFiles
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 
55 namespace Foam
56 {
57 // Forward declarations
58 class dictionary;
59 template<class EnumType, int nEnum> class NamedEnum;
60 
61 template<class EnumType, int nEnum>
63 
64 
65 /*---------------------------------------------------------------------------*\
66  Class NamedEnum Declaration
67 \*---------------------------------------------------------------------------*/
68 
69 template<class EnumType, int nEnum>
70 class 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 
91 public:
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 // ************************************************************************* //
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:65
HashTable.H
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
wordList.H
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
NamedEnumI.H
NamedEnum.C
Foam::radiation::lookup
Lookup type of boundary radiation properties.
Definition: lookup.H:63
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:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::HashTable< int >
found
bool found
Definition: TABSMDCalcMethod2.H:32
Foam::List< word >
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::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:36
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::PtrListOps::names
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Foam::NamedEnum
A NamedEnum is a wrapper around a list of names that represent particular enumeration values.
Definition: NamedEnum.H:58