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-------------------------------------------------------------------------------
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
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
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);
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.
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 from binary stream.
104// \note No internal check for binary vs ascii,
105// the caller knows what they are doing
106label readRawLabel(Istream& is);
107
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
111void readRawLabel(Istream& is, label* data, size_t nElem = 1);
112
113
114// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
115
116//- Raise one label to the power of another
117label pow(label a, label b);
118
119//- Evaluate n! : 0 < n <= 12
120label factorial(label n);
121
122//- Non-const access to integer-type (has no components)
123inline label& setComponent(label& val, const direction) noexcept
124{
125 return val;
126}
127
128//- Return integer value (has no components)
129inline constexpr label component(const label val, const direction) noexcept
130{
131 return val;
132}
133
134
135/*---------------------------------------------------------------------------*\
136 Struct labelOp Declaration
137\*---------------------------------------------------------------------------*/
138
139//- Conversion/extraction to label operation
140// Specialization of this shall provide a corresponding \c operator().
141template<class> struct labelOp;
142
143//- Convert (likely promote) from int32_t to label
144template<>
145struct labelOp<int32_t>
146{
147 constexpr label operator()(const int32_t& val) const noexcept
148 {
149 return val;
150 }
151};
152
153
154//- Convert (possibly truncate) from int64_t to label
155template<>
156struct labelOp<int64_t>
157{
158 constexpr label operator()(const int64_t& val) const noexcept
159 {
160 #if WM_LABEL_SIZE == 32
161 return label(val);
162 #elif WM_LABEL_SIZE == 64
163 return val;
164 #endif
165 }
166};
167
168
169// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
170
171// Type conversions (narrowing)
172
173//- Type narrowing from int64_t to int32_t
174// Overflow: silently fix, or raise error?
175inline int32_t narrowInt32(const int64_t val)
176{
177 // Single statement - future constexpr?
178 return
179 (
180 (val < INT32_MIN) ? INT32_MIN
181 : (val > INT32_MAX) ? INT32_MAX
182 : static_cast<int32_t>(val)
183 );
184}
185
186
187// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
188
189} // End namespace Foam
190
191// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
192
193#include "labelSpecific.H"
194
195#undef INT_ADD_SIZE
196#undef INT_ADD_DEF_SIZE
197#undef INT_SIZE
198
199#endif
200
201// ************************************************************************* //
label n
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
System signed integer.
Typedefs for label/uLabel without requiring label.H.
#define INT_SIZE(x, y)
Definition: label.H:49
Namespace for OpenFOAM.
constexpr label labelMin
Definition: label.H:60
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
label & setComponent(label &val, const direction) noexcept
Non-const access to integer-type (has no components)
Definition: label.H:123
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition: label.H:66
int32_t narrowInt32(const int64_t val)
Type narrowing from int64_t to int32_t.
Definition: label.H:175
constexpr label labelMax
Definition: label.H:61
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
int readInt(Istream &is)
Read int from stream.
Definition: intIO.C:80
label readRawLabel(Istream &is)
Read raw label from binary stream.
Definition: label.C:46
uint8_t direction
Definition: direction.H:56
label factorial(label n)
Evaluate n! : 0 < n <= 12.
Definition: label.C:145
volScalarField & b
Definition: createFields.H:27
constexpr label operator()(const int32_t &val) const noexcept
Definition: label.H:147
constexpr label operator()(const int64_t &val) const noexcept
Definition: label.H:158
Conversion/extraction to label operation.
Definition: label.H:141