label.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) 2011-2014 OpenFOAM Foundation
9  Copyright (C) 2018-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Typedef
28  Foam::label
29 
30 Description
31  A label is an int32_t or int64_t as specified by the pre-processor macro
32  WM_LABEL_SIZE.
33 
34  A readLabel function is defined so that label can be constructed from
35  Istream.
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef Foam_primitives_label_H
40 #define Foam_primitives_label_H
41 
42 #include "int.H"
43 #include "labelFwd.H"
44 
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 
47 #define INT_ADD_SIZE(x,s,y) x ## s ## y
48 #define INT_ADD_DEF_SIZE(x,s,y) INT_ADD_SIZE(x,s,y)
49 #define INT_SIZE(x,y) INT_ADD_DEF_SIZE(x,WM_LABEL_SIZE,y)
50 
51 // Size checks and typedefs (label) in labelFwd.H
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
55 namespace Foam
56 {
57 
58 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
59 
60 constexpr label labelMin = INT_SIZE(INT, _MIN);
61 constexpr label labelMax = INT_SIZE(INT, _MAX);
62 
63 //- Parse entire buffer as a label, skipping leading/trailing whitespace.
64 // Uses readInt32 or readInt64 according to WM_LABEL_SIZE
65 // \return Parsed value or FatalIOError on any problem
66 inline label readLabel(const char* buf)
67 {
68  return INT_SIZE(readInt,) (buf);
69 }
70 
71 //- Parse entire string as a label, skipping leading/trailing whitespace.
72 // Uses readInt32 or readInt64 according to WM_LABEL_SIZE
73 // \return Parsed value or FatalIOError on any problem
74 inline label readLabel(const std::string& str)
75 {
76  return INT_SIZE(readInt,) (str);
77 }
78 
79 //- Parse entire buffer as a label, skipping leading/trailing whitespace.
80 // Uses readInt32 or readInt64 according to WM_LABEL_SIZE
81 // \return True if successful.
82 inline bool readLabel(const char* buf, label& val)
83 {
84  return INT_SIZE(readInt,) (buf, val);
85 }
86 
87 //- Parse entire string as a label, skipping leading/trailing whitespace.
88 // Uses readInt32 or readInt64 according to WM_LABEL_SIZE
89 // \return True if successful.
90 inline bool readLabel(const std::string& str, label& val)
91 {
92  return INT_SIZE(readInt,) (str, val);
93 }
94 
95 
96 //- Read label from stream.
97 // Uses readInt32 or readInt64 according to WM_LABEL_SIZE
98 inline label readLabel(Istream& is)
99 {
100  return INT_SIZE(readInt,) (is);
101 }
102 
103 //- Read raw label from binary stream.
104 // \note No internal check for binary vs ascii,
105 // the caller knows what they are doing
106 label readRawLabel(Istream& is);
108 //- Read raw label(s) from binary stream.
109 // \note No internal check for binary vs ascii,
110 // the caller knows what they are doing
111 void readRawLabel(Istream& is, label* data, size_t nElem = 1);
112 
113 
114 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
115 
116 //- Raise one label to the power of another
117 label pow(label a, label b);
118 
119 //- Evaluate n! : 0 < n <= 12
120 label factorial(label n);
121 
122 //- Non-const access to integer-type (has no components)
123 inline label& setComponent(label& val, const direction) noexcept
124 {
125  return val;
126 }
127 
128 //- Return integer value (has no components)
129 inline constexpr label component(const label val, const direction) noexcept
130 {
131  return val;
132 }
133 
134 
135 // * * * * * * * * * * * * * * * General Functions * * * * * * * * * * * * * //
136 
137 //- Compare two values for equality
138 template<class T>
139 inline bool equal(const T& a, const T& b)
140 {
141  return (a == b);
142 }
143 
144 //- Return value clamped between upper and lower limits.
145 // Unlike the std::clamp, which selects between references, this version
146 // wraps the min/max free functions for component-wise clamping
147 template<class T>
148 inline T clamp(const T& val, const T& lower, const T& upper)
149 {
150  return min(max(val, lower), upper);
151 }
153 
154 /*---------------------------------------------------------------------------*\
155  Struct labelOp Declaration
156 \*---------------------------------------------------------------------------*/
157 
158 //- Conversion/extraction to label operation
159 // Specialization of this shall provide a corresponding \c operator().
160 template<class> struct labelOp;
161 
162 //- Convert (likely promote) from int32_t to label
163 template<>
164 struct labelOp<int32_t>
165 {
166  constexpr label operator()(const int32_t& val) const noexcept
167  {
168  return val;
169  }
170 };
171 
172 
173 //- Convert (possibly truncate) from int64_t to label
174 template<>
175 struct labelOp<int64_t>
176 {
177  constexpr label operator()(const int64_t& val) const noexcept
178  {
179  #if WM_LABEL_SIZE == 32
180  return label(val);
181  #elif WM_LABEL_SIZE == 64
182  return val;
183  #endif
184  }
185 };
186 
187 
188 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
189 
190 // Type conversions (narrowing)
192 //- Type narrowing from int64_t to int32_t
193 // Overflow: silently fix, or raise error?
194 inline int32_t narrowInt32(const int64_t val)
195 {
196  // Single statement - future constexpr?
197  return
198  (
199  (val < INT32_MIN) ? INT32_MIN
200  : (val > INT32_MAX) ? INT32_MAX
201  : static_cast<int32_t>(val)
202  );
203 }
204 
205 
206 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
207 
208 } // End namespace Foam
209 
210 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
211 
212 #include "labelSpecific.H"
213 
214 #undef INT_ADD_SIZE
215 #undef INT_ADD_DEF_SIZE
216 #undef INT_SIZE
217 
218 #endif
219 
220 // ************************************************************************* //
System signed integer.
uint8_t direction
Definition: direction.H:46
bool equal(const T &a, const T &b)
Compare two values for equality.
Definition: label.H:164
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition: label.H:63
string upper(const std::string &s)
Return string copy transformed with std::toupper on each character.
Definition: stringOps.C:1187
int readInt(Istream &is)
Read int from stream.
Definition: intIO.C:73
constexpr label labelMin
Definition: label.H:54
Typedefs for label/uLabel without requiring label.H.
Conversion/extraction to label operation.
Definition: label.H:191
int32_t narrowInt32(const int64_t val)
Type narrowing from int64_t to int32_t.
Definition: label.H:232
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
label factorial(label n)
Evaluate n! : 0 < n <= 12.
Definition: label.C:138
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:26
const direction noexcept
Definition: Scalar.H:258
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
label readRawLabel(Istream &is)
Read raw label from binary stream.
Definition: label.C:39
string lower(const std::string &s)
Return string copy transformed with std::tolower on each character.
Definition: stringOps.C:1171
constexpr label labelMax
Definition: label.H:55
label n
label & setComponent(label &val, const direction) noexcept
Non-const access to integer-type (has no components)
Definition: label.H:144
#define INT_SIZE(x, y)
Definition: label.H:43
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Namespace for OpenFOAM.
dimensionSet clamp(const dimensionSet &a, const dimensionSet &range)
Definition: dimensionSet.C:271