Loading...
Searching...
No Matches
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-2025 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Typedef
28 Foam::label
29
30Description
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
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// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
55namespace Foam
56{
57
58// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
59
60constexpr label labelMin = INT_SIZE(INT, _MIN);
61constexpr 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
66inline 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
74inline 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.
82inline bool readLabel(const char* buf, label& val)
83{
84 return INT_SIZE(readInt,) (buf, val);
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.
90inline 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
98inline label readLabel(Istream& is)
99{
100 return INT_SIZE(readInt,) (is);
101}
102
103//- Read raw label(s) from binary stream.
104// \note No internal check for binary vs ascii,
105// the caller knows what they are doing
106void readRawLabel(Istream& is, label* data, size_t nElem = 1);
108//- Read raw label from binary stream.
109// \note No internal check for binary vs ascii,
110// the caller knows what they are doing
111inline void readRawLabel(Istream& is, label& val)
112{
113 readRawLabel(is, &val, 1);
114}
115
116//- Read raw label from binary stream and return value.
117// \note No internal check for binary vs ascii,
118// the caller knows what they are doing
119inline label readRawLabel(Istream& is)
120{
121 label val(0);
122 readRawLabel(is, &val, 1);
123 return val;
124}
125
127// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
128
129//- Raise one label to the power of another
130label pow(label a, label b);
131
132//- Evaluate n! : 0 < n <= 12
133label factorial(label n);
134
135//- Non-const access to integer-type (has no components)
136inline label& setComponent(label& val, const direction) noexcept
138 return val;
139}
140
141//- Return integer value (has no components)
142inline constexpr label component(const label val, const direction) noexcept
143{
144 return val;
145}
146
147
148// * * * * * * * * * * * * * * * General Functions * * * * * * * * * * * * * //
149
150//- Compare two values for equality
151template<class T>
152inline bool equal(const T& a, const T& b)
153{
154 return (a == b);
155}
156
157//- Return value clamped between upper and lower limits.
158// Unlike the std::clamp, which selects between references, this version
159// wraps the min/max free functions for component-wise clamping
160template<class T>
161inline T clamp(const T& val, const T& lower, const T& upper)
162{
163 return min(max(val, lower), upper);
164}
165
166
167/*---------------------------------------------------------------------------*\
168 Struct labelOp Declaration
169\*---------------------------------------------------------------------------*/
170
171//- Conversion/extraction to label operation
172// Specialization of this shall provide a corresponding \c operator().
173template<class> struct labelOp;
174
175//- Convert (likely promote) from int32_t to label
176template<>
177struct labelOp<int32_t>
178{
179 constexpr label operator()(const int32_t& val) const noexcept
181 return val;
182 }
183};
184
185
186//- Convert (possibly truncate) from int64_t to label
187template<>
188struct labelOp<int64_t>
189{
190 constexpr label operator()(const int64_t& val) const noexcept
191 {
192 #if WM_LABEL_SIZE == 32
193 return label(val);
194 #elif WM_LABEL_SIZE == 64
195 return val;
196 #endif
197 }
198};
199
200
201// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
202
203// Type conversions (narrowing)
204
205//- Type narrowing from int64_t to int32_t
206// Overflow: fix silently.
207inline constexpr int32_t narrowInt32(const int64_t val) noexcept
208{
209 return
210 (
211 (val <= INT32_MIN) ? INT32_MIN
212 : (val >= INT32_MAX) ? INT32_MAX
213 : static_cast<int32_t>(val)
214 );
216
217
218// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
219
220} // End namespace Foam
221
222// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
223
224#include "labelSpecific.H"
225
226#undef INT_ADD_SIZE
227#undef INT_ADD_DEF_SIZE
228#undef INT_SIZE
229
230#endif
231
232// ************************************************************************* //
label n
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
System signed integer.
Typedefs for label/uLabel without requiring label.H.
#define INT_SIZE(x, y)
Definition label.H:43
Namespace for OpenFOAM.
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:40
void readRawLabel(Istream &is, label *data, size_t nElem=1)
Read raw label(s) from binary stream.
Definition label.C:77
constexpr label labelMin
Definition label.H:54
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
bool equal(const T &a, const T &b)
Compare two values for equality.
Definition label.H:180
label & setComponent(label &val, const direction) noexcept
Non-const access to integer-type (has no components).
Definition label.H:160
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition label.H:63
dimensionSet clamp(const dimensionSet &a, const dimensionSet &range)
constexpr label labelMax
Definition label.H:55
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
int readInt(Istream &is)
Read int from stream.
Definition intIO.C:73
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:26
uint8_t direction
Definition direction.H:49
constexpr int32_t narrowInt32(const int64_t val) noexcept
Type narrowing from int64_t to int32_t.
Definition label.H:248
label factorial(label n)
Evaluate n! : 0 < n <= 12.
Definition label.C:128
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
volScalarField & b
constexpr label operator()(const int32_t &val) const noexcept
Definition label.H:215
constexpr label operator()(const int64_t &val) const noexcept
Definition label.H:228
Conversion/extraction to label operation.
Definition label.H:207