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-2019 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 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 #define INT_ADD_SIZE(x,s,y) x ## s ## y
47 #define INT_ADD_DEF_SIZE(x,s,y) INT_ADD_SIZE(x,s,y)
48 #define INT_SIZE(x,y) INT_ADD_DEF_SIZE(x,WM_LABEL_SIZE,y)
49 
50 #if WM_LABEL_SIZE != 32 && WM_LABEL_SIZE != 64
51  #error "label.H: WM_LABEL_SIZE must be set to either 32 or 64"
52 #endif
53 
54 
55 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56 
57 namespace Foam
58 {
59 
60 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
61 
62 typedef INT_SIZE(int, _t) label;
63 
64 constexpr label labelMin = INT_SIZE(INT, _MIN);
65 constexpr label labelMax = INT_SIZE(INT, _MAX);
66 
67 //- Parse entire buffer as a label, skipping leading/trailing whitespace.
68 // Uses readInt32 or readInt64 according to WM_LABEL_SIZE
69 // \return Parsed value or FatalIOError on any problem
70 inline label readLabel(const char* buf)
71 {
72  return INT_SIZE(readInt,) (buf);
73 }
74 
75 //- Parse entire string as a label, skipping leading/trailing whitespace.
76 // Uses readInt32 or readInt64 according to WM_LABEL_SIZE
77 // \return Parsed value or FatalIOError on any problem
78 inline label readLabel(const std::string& str)
79 {
80  return INT_SIZE(readInt,) (str);
81 }
82 
83 //- Parse entire buffer as a label, skipping leading/trailing whitespace.
84 // Uses readInt32 or readInt64 according to WM_LABEL_SIZE
85 // \return True if successful.
86 inline bool readLabel(const char* buf, label& val)
87 {
88  return INT_SIZE(readInt,) (buf, val);
89 }
90 
91 //- Parse entire string as a label, skipping leading/trailing whitespace.
92 // Uses readInt32 or readInt64 according to WM_LABEL_SIZE
93 // \return True if successful.
94 inline bool readLabel(const std::string& str, label& val)
95 {
96  return INT_SIZE(readInt,) (str, val);
97 }
98 
99 
100 //- Read label from stream.
101 // Uses readInt32 or readInt64 according to WM_LABEL_SIZE
103 {
104  return INT_SIZE(readInt,) (is);
105 }
106 
107 //- Read raw label from binary stream.
108 // \note No internal check for binary vs ascii,
109 // the caller knows what they are doing
110 label readRawLabel(Istream& is);
111 
112 //- Read raw label(s) from binary stream.
113 // \note No internal check for binary vs ascii,
114 // the caller knows what they are doing
115 void readRawLabel(Istream& is, label* data, size_t nElem = 1);
116 
117 
118 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
119 
120 //- Raise one label to the power of another
121 label pow(label a, label b);
122 
123 //- Evaluate n! : 0 < n <= 12
125 
126 
127 inline label& setComponent(label& l, const direction)
128 {
129  return l;
130 }
131 
132 inline label component(const label l, const direction)
133 {
134  return l;
135 }
136 
137 
138 /*---------------------------------------------------------------------------*\
139  Struct labelOp Declaration
140 \*---------------------------------------------------------------------------*/
141 
142 //- Conversion/extraction to label operation
143 // Specialization of this shall provide a corresponding \c operator().
144 template<class> struct labelOp;
145 
146 //- Convert (likely promote) from int32_t to label
147 template<>
148 struct labelOp<int32_t>
149 {
150  constexpr label operator()(const int32_t& val) const noexcept
151  {
152  return val;
153  }
154 };
155 
156 
157 //- Convert (possibly truncate) from int64_t to label
158 template<>
159 struct labelOp<int64_t>
160 {
161  constexpr label operator()(const int64_t& val) const noexcept
162  {
163  #if WM_LABEL_SIZE == 32
164  return label(val);
165  #elif WM_LABEL_SIZE == 64
166  return val;
167  #endif
168  }
169 };
170 
171 
172 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
173 
174 } // End namespace Foam
175 
176 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
177 
178 #include "labelSpecific.H"
179 
180 #undef INT_ADD_SIZE
181 #undef INT_ADD_DEF_SIZE
182 #undef INT_SIZE
183 
184 #endif
185 
186 // ************************************************************************* //
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::setComponent
label & setComponent(label &l, const direction)
Definition: label.H:127
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:65
n
label n
Definition: TABSMDCalcMethod2.H:31
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::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:150
Foam::pow
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
Definition: dimensionedScalar.C:75
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
INT_SIZE
#define INT_SIZE(x, y)
Definition: label.H:48
labelSpecific.H
Foam::labelOp< int64_t >::operator()
constexpr label operator()(const int64_t &val) const noexcept
Definition: label.H:161
Foam::readLabel
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition: label.H:70
Foam::direction
uint8_t direction
Definition: direction.H:47
Foam::labelMin
constexpr label labelMin
Definition: label.H:64
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:144