NamedEnum.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2025 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::NamedEnum
26 
27 Description
28  Initialise the NamedEnum HashTable from the static list of names.
29 
30 SourceFiles
31  NamedEnum.C
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #ifndef NamedEnum_H
36 #define NamedEnum_H
37 
38 #include "FixedList.H"
39 #include "HashTable.H"
40 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
43 namespace Foam
44 {
45 
46 class dictionary;
47 
48 /*---------------------------------------------------------------------------*\
49  Class NamedEnum Declaration
50 \*---------------------------------------------------------------------------*/
51 
52 template<class Enum, unsigned int nEnum>
53 class NamedEnum
54 :
55  public FixedList<word, nEnum>
56 {
57  // Private Member Data
58 
59  //- Map of name -> enumeration
61 
62 
63 public:
64 
66 
67  // Constructors
68 
69  //- Construct from list of names
70  NamedEnum(const FixedList<word, nEnum>& names);
71 
72  //- Construct from list of names
73  NamedEnum(std::initializer_list<word>);
74 
75  //- Disallow default bitwise copy construction
76  NamedEnum(const NamedEnum&) = delete;
77 
78 
79  // Member Functions
80 
81  //- Read a word from Istream and return the corresponding
82  // enumeration element
83  Enum read(Istream&) const;
84 
85  //- Lookup the name from the dictionary if present and return the
86  // corresponding enumeration element, otherwise return the defaultValue
87  Enum lookupOrDefault
88  (
89  const word& name,
90  const dictionary& dict,
91  const Enum defaultValue
92  ) const;
93 
94  //- Select the name from the dictionary and return the
95  // corresponding enumeration element.
96  // If more than one of the options is present in the dictionary
97  // generate a FatalError
98  Enum select(const dictionary& dict) const;
99 
100  //- Write the name representation of the enumeration to an Ostream
101  void write(const Enum e, Ostream&) const;
102 
103 
104  // Member Operators
105 
106  //- Return the enumeration element corresponding to the given name
107  const Enum operator[](const char* name) const
108  {
109  return Enum(table_[name]);
110  }
111 
112  //- Return the enumeration element corresponding to the given name
113  const Enum operator[](const word& name) const
114  {
115  return Enum(table_[name]);
116  }
117 
118  //- Return the name of the given enumeration element
119  const word& operator[](const Enum e) const
120  {
121  return FixedList<word, nEnum>::operator[](unsigned(e));
122  }
123 
124  //- Disallow default bitwise assignment
125  void operator=(const NamedEnum&) = delete;
126 };
127 
128 
129 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
130 
131 } // End namespace Foam
132 
133 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
134 
135 #ifdef NoRepository
136  #include "NamedEnum.C"
137 #endif
138 
139 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
140 
141 #endif
142 
143 // ************************************************************************* //
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:78
T & operator[](const label)
Return element of FixedList.
Definition: FixedListI.H:247
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:55
Enum read(Istream &) const
Read a word from Istream and return the corresponding.
Definition: NamedEnum.C:54
void operator=(const NamedEnum &)=delete
Disallow default bitwise assignment.
Enum select(const dictionary &dict) const
Select the name from the dictionary and return the.
Definition: NamedEnum.C:86
Enum lookupOrDefault(const word &name, const dictionary &dict, const Enum defaultValue) const
Lookup the name from the dictionary if present and return the.
Definition: NamedEnum.C:73
NamedEnum(const FixedList< word, nEnum > &names)
Construct from list of names.
Definition: NamedEnum.C:32
const Enum operator[](const char *name) const
Return the enumeration element corresponding to the given name.
Definition: NamedEnum.H:106
FixedList< word, nEnum > namesType
Definition: NamedEnum.H:64
void write(const Enum e, Ostream &) const
Write the name representation of the enumeration to an Ostream.
Definition: NamedEnum.C:119
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
A class for handling words, derived from string.
Definition: word.H:62
Namespace for OpenFOAM.
const doubleScalar e
Definition: doubleScalar.H:106
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
dictionary dict