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-2020 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 label_H
40 #define 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);
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
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 
123 inline label& setComponent(label& l, const direction)
124 {
125  return l;
126 }
127 
128 inline label component(const label l, const direction)
129 {
130  return l;
131 }
132 
133 
134 /*---------------------------------------------------------------------------*\
135  Struct labelOp Declaration
136 \*---------------------------------------------------------------------------*/
137 
138 //- Conversion/extraction to label operation
139 // Specialization of this shall provide a corresponding \c operator().
140 template<class> struct labelOp;
141 
142 //- Convert (likely promote) from int32_t to label
143 template<>
144 struct labelOp<int32_t>
145 {
146  constexpr label operator()(const int32_t& val) const noexcept
147  {
148  return val;
149  }
150 };
151 
152 
153 //- Convert (possibly truncate) from int64_t to label
154 template<>
155 struct labelOp<int64_t>
156 {
157  constexpr label operator()(const int64_t& val) const noexcept
158  {
159  #if WM_LABEL_SIZE == 32
160  return label(val);
161  #elif WM_LABEL_SIZE == 64
162  return val;
163  #endif
164  }
165 };
166 
167 
168 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
169 
170 // Type conversions (narrowing)
171 
172 //- Type narrowing from int64_t to int32_t
173 // Overflow: silently fix, or raise error?
174 inline int32_t narrowInt32(const int64_t val)
175 {
176  // Single statement - future constexpr?
177  return
178  (
179  (val < INT32_MIN) ? INT32_MIN
180  : (val > INT32_MAX) ? INT32_MAX
181  : static_cast<int32_t>(val)
182  );
183 }
184 
185 
186 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
187 
188 } // End namespace Foam
189 
190 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
191 
192 #include "labelSpecific.H"
193 
194 #undef INT_ADD_SIZE
195 #undef INT_ADD_DEF_SIZE
196 #undef INT_SIZE
197 
198 #endif
199 
200 // ************************************************************************* //
Foam::setComponent
label & setComponent(label &l, const direction)
Definition: label.H:123
int.H
System signed integer.
Foam::component
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Definition: FieldFieldFunctions.C:44
Foam::labelMax
constexpr label labelMax
Definition: label.H:61
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::labelOp< int32_t >::operator()
constexpr label operator()(const int32_t &val) const noexcept
Definition: label.H:146
Foam::pow
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
Definition: dimensionedScalar.C:75
labelFwd.H
Typedefs for label/uLabel without requiring label.H.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
INT_SIZE
#define INT_SIZE(x, y)
Definition: label.H:49
labelSpecific.H
Foam::labelOp< int64_t >::operator()
constexpr label operator()(const int64_t &val) const noexcept
Definition: label.H:157
Foam::readLabel
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition: label.H:66
Foam::direction
uint8_t direction
Definition: direction.H:52
Foam::labelMin
constexpr label labelMin
Definition: label.H:60
Foam::factorial
label factorial(label n)
Evaluate n! : 0 < n <= 12.
Definition: label.C:145
Foam::readInt
int readInt(Istream &is)
Read int from stream.
Definition: intIO.C:80
Foam::readRawLabel
label readRawLabel(Istream &is)
Read raw label from binary stream.
Definition: label.C:46
Foam::labelOp
Conversion/extraction to label operation.
Definition: label.H:140
Foam::narrowInt32
int32_t narrowInt32(const int64_t val)
Type narrowing from int64_t to int32_t.
Definition: label.H:174