EnumI.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) 2017-2019 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
29 
30 template<class EnumType>
31 inline bool Foam::Enum<EnumType>::empty() const noexcept
32 {
33  return keys_.empty();
34 }
35 
36 
37 template<class EnumType>
38 inline Foam::label Foam::Enum<EnumType>::size() const noexcept
39 {
40  return keys_.size();
41 }
42 
43 
44 template<class EnumType>
46 {
47  return keys_;
48 }
49 
50 
51 template<class EnumType>
53 {
54  return keys_;
55 }
56 
57 
58 template<class EnumType>
60 {
61  return vals_;
62 }
63 
64 
65 template<class EnumType>
67 {
68  keys_.clear();
69  vals_.clear();
70 }
71 
72 
73 template<class EnumType>
74 inline Foam::label Foam::Enum<EnumType>::find(const word& enumName) const
75 {
76  return keys_.find(enumName);
77 }
78 
79 
80 template<class EnumType>
81 inline Foam::label Foam::Enum<EnumType>::find(const EnumType e) const
82 {
83  return vals_.find(int(e));
84 }
85 
86 
87 template<class EnumType>
88 inline bool Foam::Enum<EnumType>::found(const word& enumName) const
89 {
90  return keys_.found(enumName);
91 }
92 
93 
94 template<class EnumType>
95 inline bool Foam::Enum<EnumType>::found(const EnumType e) const
96 {
97  return vals_.found(int(e));
98 }
99 
100 
101 template<class EnumType>
102 inline const Foam::word& Foam::Enum<EnumType>::get(const EnumType e) const
103 {
104  const label idx = find(e);
105 
106  if (idx < 0)
107  {
108  return word::null;
109  }
110 
111  return keys_[idx];
112 }
113 
114 
115 template<class EnumType>
117 (
118  Ostream& os,
119  const label shortLen
120 ) const
121 {
122  return keys_.writeList(os, shortLen);
123 }
124 
125 
126 template<class EnumType>
127 inline void Foam::Enum<EnumType>::write(const EnumType e, Ostream& os) const
128 {
129  const label idx = find(e);
130 
131  if (idx >= 0)
132  {
133  os << keys_[idx];
134  }
135 }
136 
137 
138 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
139 
140 template<class EnumType>
141 inline EnumType Foam::Enum<EnumType>::operator[]
142 (
143  const word& enumName
144 ) const
145 {
146  return get(enumName);
147 }
148 
149 
150 template<class EnumType>
152 (
153  const EnumType e
154 ) const
155 {
156  return get(e);
157 }
158 
159 
160 template<class EnumType>
162 (
163  const EnumType e
164 ) const
165 {
166  return get(e);
167 }
168 
169 
170 template<class EnumType>
171 inline EnumType Foam::Enum<EnumType>::operator()
172 (
173  const word& enumName,
174  const EnumType deflt
175 ) const
176 {
177  const label idx = find(enumName);
178 
179  if (idx >= 0)
180  {
181  return EnumType(vals_[idx]);
182  }
183 
184  return deflt;
185 }
186 
187 
188 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
189 
190 template<class EnumType>
191 inline Foam::Ostream& Foam::operator<<
192 (
193  Ostream& os,
194  const Enum<EnumType>& list
195 )
196 {
197  return list.names().writeList(os, 0);
198 }
199 
200 
201 template<class EnumType>
202 inline std::ostream& Foam::operator<<
203 (
204  std::ostream& os,
205  const Enum<EnumType>& list
206 )
207 {
208  os << '(';
209 
210  unsigned i = 0;
211 
212  for (const word& k : list.names())
213  {
214  if (i++) os << ' ';
215  os << k;
216  }
217 
218  os << ')';
219 
220  return os;
221 }
222 
223 
224 // ************************************************************************* //
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:51
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::Enum::found
bool found(const word &enumName) const
Test if there is an enumeration corresponding to the given name.
Definition: EnumI.H:88
Foam::Enum::names
const List< word > & names() const
The list of enum names, in construction order. Same as toc()
Definition: EnumI.H:45
Foam::Enum::empty
bool empty() const noexcept
True if the enumeration list is empty.
Definition: EnumI.H:31
Foam::Enum::writeList
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write the names as a list to an Ostream.
Definition: EnumI.H:117
Foam::Enum::get
EnumType get(const word &enumName) const
The enumeration corresponding to the given name.
Definition: Enum.C:86
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::Enum::write
void write(const EnumType e, Ostream &os) const
Write the name representation of the enumeration to an Ostream.
Definition: EnumI.H:127
Foam::Enum::values
const List< int > & values() const
The list of enum values, in construction order.
Definition: EnumI.H:59
Foam::ListOps::find
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Find index of the first occurrence that satisfies the predicate.
Foam::List< word >
Foam::Enum::size
label size() const noexcept
The number of name/value pairs for the enumeration.
Definition: EnumI.H:38
Foam::Enum::clear
void clear()
Clear all entries.
Definition: EnumI.H:66
k
label k
Boltzmann constant.
Definition: LISASMDCalcMethod2.H:41
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
Foam::Enum::find
label find(const word &enumName) const
Find the index of the given name.
Definition: EnumI.H:74
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Enum::toc
const List< word > & toc() const
The list of enum names, in construction order. Same as names()
Definition: EnumI.H:52