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 | Copyright (C) 2011-2016 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 "HashTable.H"
39 #include "stringList.H"
40 #include "wordList.H"
41 #include <type_traits>
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47 
48 /*---------------------------------------------------------------------------*\
49  Class NamedEnum Declaration
50 \*---------------------------------------------------------------------------*/
51 
52 template<class Enum, int nEnum>
53 class NamedEnum
54 :
55  public HashTable<int>
56 {
57  //- nEnum must be positive (non-zero)
58  static_assert(nEnum > 0, "nEnum must be positive (non-zero)");
59 
60  // Private Member Functions
61 
62  //- Disallow default bitwise copy construct
63  NamedEnum(const NamedEnum&);
64 
65  //- Disallow default bitwise assignment
66  void operator=(const NamedEnum&);
67 
68 
69 public:
70 
71  // Static data members
72 
73  //- The set of names corresponding to the enumeration Enum
74  static const char* names[nEnum];
75 
76 
77  // Constructors
78 
79  //- Construct from names
80  NamedEnum();
81 
82 
83  // Member Functions
84 
85  //- Read a word from Istream and return the corresponding
86  // enumeration element
87  Enum read(Istream&) const;
88 
89  //- Write the name representation of the enumeration to an Ostream
90  void write(const Enum e, Ostream&) const;
91 
92  //- The set of names as a list of strings
93  static stringList strings();
94 
95  //- The set of names as a list of words
96  static wordList words();
97 
98 
99  // Member Operators
100 
101  //- Return the enumeration element corresponding to the given name
102  const Enum operator[](const char* name) const
103  {
104  return Enum(HashTable<int>::operator[](name));
105  }
106 
107  //- Return the enumeration element corresponding to the given name
108  const Enum operator[](const word& name) const
109  {
110  return Enum(HashTable<int>::operator[](name));
111  }
112 
113  //- Return the name of the given enumeration element
114  const char* operator[](const Enum e) const
115  {
116  return names[e];
117  }
118 };
119 
120 
121 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
122 
123 } // End namespace Foam
124 
125 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
126 
127 #ifdef NoRepository
128  #include "NamedEnum.C"
129 #endif
130 
131 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
132 
133 #endif
134 
135 // ************************************************************************* //
static wordList words()
The set of names as a list of words.
Definition: NamedEnum.C:105
const double e
Elementary charge.
Definition: doubleFloat.H:78
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:52
static stringList strings()
The set of names as a list of strings.
Definition: NamedEnum.C:86
const char * names[]
Definition: directions.C:46
A class for handling words, derived from string.
Definition: word.H:59
NamedEnum()
Construct from names.
Definition: NamedEnum.C:31
An STL-conforming hash table.
Definition: HashTable.H:61
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
const Enum operator[](const char *name) const
Return the enumeration element corresponding to the given name.
Definition: NamedEnum.H:101
void write(const Enum e, Ostream &) const
Write the name representation of the enumeration to an Ostream.
Definition: NamedEnum.C:79
Enum read(Istream &) const
Read a word from Istream and return the corresponding.
Definition: NamedEnum.C:61
Namespace for OpenFOAM.