All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NamedEnum.C
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-2018 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 \*---------------------------------------------------------------------------*/
25 
26 #include "NamedEnum.H"
27 
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
30 template<class Enum, unsigned int nEnum>
32 :
33  HashTable<unsigned int>(2*nEnum)
34 {
35  for (unsigned int enumI = 0; enumI < nEnum; ++enumI)
36  {
37  if (!names[enumI] || names[enumI][0] == '\0')
38  {
39  stringList goodNames(enumI);
40 
41  for (unsigned int i = 0; i < enumI; ++i)
42  {
43  goodNames[i] = names[i];
44  }
45 
47  << "Illegal enumeration name at position " << enumI << endl
48  << "after entries " << goodNames << ".\n"
49  << "Possibly your NamedEnum<Enum, nEnum>::names array"
50  << " is not of size " << nEnum << endl
51  << abort(FatalError);
52  }
53  insert(names[enumI], enumI);
54  }
55 }
56 
57 
58 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
59 
60 template<class Enum, unsigned int nEnum>
62 {
63  const word name(is);
64 
66 
67  if (iter == HashTable<unsigned int>::end())
68  {
70  << name << " is not in enumeration: "
71  << sortedToc() << exit(FatalIOError);
72  }
73 
74  return Enum(iter());
75 }
76 
77 
78 template<class Enum, unsigned int nEnum>
79 void Foam::NamedEnum<Enum, nEnum>::write(const Enum e, Ostream& os) const
80 {
81  os << operator[](e);
82 }
83 
84 
85 template<class Enum, unsigned int nEnum>
87 {
88  stringList lst(nEnum);
89 
90  label nElem = 0;
91  for (unsigned int enumI = 0; enumI < nEnum; ++enumI)
92  {
93  if (names[enumI] && names[enumI][0])
94  {
95  lst[nElem++] = names[enumI];
96  }
97  }
98 
99  lst.setSize(nElem);
100  return lst;
101 }
102 
103 
104 template<class Enum, unsigned int nEnum>
106 {
107  wordList lst(nEnum);
108 
109  label nElem = 0;
110  for (unsigned int enumI = 0; enumI < nEnum; ++enumI)
111  {
112  if (names[enumI] && names[enumI][0])
113  {
114  lst[nElem++] = names[enumI];
115  }
116  }
117 
118  lst.setSize(nElem);
119  return lst;
120 }
121 
122 
123 template<class Enum, unsigned int nEnum>
124 const char* Foam::NamedEnum<Enum, nEnum>::operator[](const Enum e) const
125 {
126  unsigned int ue = unsigned(e);
127 
128  if (ue < nEnum)
129  {
130  return names[ue];
131  }
132  else
133  {
135  << "names array index " << ue << " out of range 0-"
136  << nEnum - 1
137  << exit(FatalError);
138 
139  return names[0];
140  }
141 }
142 
143 
144 // ************************************************************************* //
static wordList words()
The set of names as a list of words.
Definition: NamedEnum.C:105
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:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
const Enum operator[](const char *name) const
Return the enumeration element corresponding to the given name.
Definition: NamedEnum.H:97
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
void write(const Enum e, Ostream &) const
Write the name representation of the enumeration to an Ostream.
Definition: NamedEnum.C:79
static stringList strings()
The set of names as a list of strings.
Definition: NamedEnum.C:86
void insert(const scalar, DynamicList< floatScalar > &)
Append scalar to given DynamicList.
const dimensionedScalar & e
Elementary charge.
Definition: doubleScalar.H:105
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
errorManip< error > abort(error &err)
Definition: errorManip.H:131
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
void setSize(const label)
Reset size of List.
Definition: List.C:281
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
Enum read(Istream &) const
Read a word from Istream and return the corresponding.
Definition: NamedEnum.C:61
IOerror FatalIOError