NamedEnum.C
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 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, int nEnum>
32 :
33  HashTable<int>(2*nEnum)
34 {
35  for (int enumI = 0; enumI < nEnum; ++enumI)
36  {
37  if (!names[enumI] || names[enumI][0] == '\0')
38  {
39  stringList goodNames(enumI);
40 
41  for (int i = 0; i < enumI; ++i)
42  {
43  goodNames[i] = names[i];
44  }
45 
46  FatalErrorIn("NamedEnum<Enum, nEnum>::NamedEnum()")
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, int nEnum>
62 {
63  const word name(is);
64 
66 
67  if (iter == HashTable<int>::end())
68  {
70  (
71  "NamedEnum<Enum, nEnum>::read(Istream&) const", is
72  ) << name << " is not in enumeration: "
73  << sortedToc() << exit(FatalIOError);
74  }
75 
76  return Enum(iter());
77 }
78 
79 
80 template<class Enum, int nEnum>
81 void Foam::NamedEnum<Enum, nEnum>::write(const Enum e, Ostream& os) const
82 {
83  os << operator[](e);
84 }
85 
86 
87 template<class Enum, int nEnum>
89 {
90  stringList lst(nEnum);
91 
92  label nElem = 0;
93  for (int enumI = 0; enumI < nEnum; ++enumI)
94  {
95  if (names[enumI] && names[enumI][0])
96  {
97  lst[nElem++] = names[enumI];
98  }
99  }
100 
101  lst.setSize(nElem);
102  return lst;
103 }
104 
105 
106 template<class Enum, int nEnum>
108 {
109  wordList lst(nEnum);
110 
111  label nElem = 0;
112  for (int enumI = 0; enumI < nEnum; ++enumI)
113  {
114  if (names[enumI] && names[enumI][0])
115  {
116  lst[nElem++] = names[enumI];
117  }
118  }
119 
120  lst.setSize(nElem);
121  return lst;
122 }
123 
124 
125 // ************************************************************************* //
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
bool insert(const word &, const int &newElmt)
Insert a new hashedEntry.
List< word > sortedToc() const
Return the table of contents as a sorted list.
An STL-conforming hash table.
Definition: HashTable.H:61
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A class for handling words, derived from string.
Definition: word.H:59
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
static stringList strings()
The set of names as a list of strings.
Definition: NamedEnum.C:88
const char * names[]
Definition: directions.C:46
const double e
Elementary charge.
Definition: doubleFloat.H:78
void setSize(const label)
Reset size of List.
Definition: List.C:318
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
static wordList words()
The set of names as a list of words.
Definition: NamedEnum.C:107
IOerror FatalIOError
NamedEnum()
Construct from names.
Definition: NamedEnum.C:31
errorManip< error > abort(error &err)
Definition: errorManip.H:131
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
error FatalError
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
iterator find(const word &)
Find and return an iterator set at the hashedEntry.
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:325
Enum read(Istream &) const
Read a word from Istream and return the corresponding.
Definition: NamedEnum.C:61
void write(const Enum e, Ostream &) const
Write the name representation of the enumeration to an Ostream.
Definition: NamedEnum.C:81