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 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 NamedEnum
71 {
72  //- The nEnum must be positive (non-zero)
73  static_assert(nEnum > 0, "nEnum must be positive (non-zero)");
74 
75  // Private Member Data
76 
77  //- The values for the enum
78  HashTable<int> lookup_;
79 
80 
81  // Private Member Functions
82 
83  //- No copy construct
84  NamedEnum(const NamedEnum&) = delete;
85 
86  //- No copy assignment
87  void operator=(const NamedEnum&) = delete;
88 
89 
90 public:
91 
92  //- The type of enumeration wrapped by NamedEnum
93  typedef EnumType value_type;
94 
95 
96  // Static data members
97 
98  //- The set of names corresponding to the enumeration EnumType
99  static const char* names[nEnum];
100 
101 
102  // Constructors
103 
104  //- Construct from names
105  NamedEnum()
106  FOAM_DEPRECATED_FOR(2017-05, "Enum instead of using 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::NamedEnum::values
List< int > values() const
The list of enum values, in construction order.
Definition: NamedEnum.C:89
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:51
Foam::NamedEnum::size
label size() const
The number of lookup names for the enumeration.
Definition: NamedEnumI.H:31
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
HashTable.H
FOAM_DEPRECATED_FOR
#define FOAM_DEPRECATED_FOR(since, replacement)
Definition: stdFoam.H:65
Foam::NamedEnum::hasName
bool hasName(const EnumType e) const
Test if there is a name corresponding to the given enumeration.
Definition: NamedEnum.C:113
Foam::NamedEnum::operator
friend Ostream & operator(Ostream &os, const NamedEnum< EnumType, nEnum > &wrapped)
Write names to Ostream, as per writeKeys() with shortListLen=10.
wordList.H
Foam::NamedEnum::found
bool found(const word &enumName) const
Test if there is an enumeration corresponding to the given name.
Definition: NamedEnumI.H:53
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::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
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:121
Foam::NamedEnum::write
void write(const EnumType e, Ostream &os) const
Write the name representation of the enumeration to an Ostream.
Definition: NamedEnum.C:187
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::NamedEnum::lookupOrDefault
EnumType lookupOrDefault(const word &key, const dictionary &dict, const EnumType deflt) const
Find the key in the dictionary and return the corresponding.
Definition: NamedEnum.C:152
Foam::NamedEnum::NamedEnum
NamedEnum() FOAM_DEPRECATED_FOR(2017-05
Construct from names.
Definition: NamedEnum.C:36
Foam::HashTable< int >
Foam::NamedEnum::hasEnum
bool hasEnum(const word &enumName) const
Test if there is an enumeration corresponding to the given name.
Definition: NamedEnumI.H:63
Foam::NamedEnum::words
wordList words() const
The list of enum names, in construction order.
Definition: NamedEnum.C:70
Foam::NamedEnum::value_type
EnumType value_type
The type of enumeration wrapped by NamedEnum.
Definition: NamedEnum.H:92
Foam::List< word >
Foam::NamedEnum::sortedToc
wordList sortedToc() const
The sorted list of enum names.
Definition: NamedEnumI.H:45
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
Foam::NamedEnum::toc
wordList toc() const
The list of enum names.
Definition: NamedEnumI.H:38
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::NamedEnum::names
static const char * names[nEnum]
The set of names corresponding to the enumeration EnumType.
Definition: NamedEnum.H:98
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102
Foam::NamedEnum
A NamedEnum is a wrapper around a list of names that represent particular enumeration values.
Definition: NamedEnum.H:58
Foam::NamedEnum::read
EnumType read(Istream &is) const
Read a word from Istream and return the corresponding enumeration.
Definition: NamedEnum.C:168