int32.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) 2014-2016 OpenFOAM Foundation
9 Copyright (C) 2016-2021 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
27Primitive
28 int32_t
29
30Description
31 32bit signed integer
32
33SourceFiles
34 int32.C
35 int32IO.C
36
37\*---------------------------------------------------------------------------*/
38
39#ifndef Foam_primitives_int32_H
40#define Foam_primitives_int32_H
41
42#include <cstdint>
43#include <climits>
44#include <cstdlib>
45
46#include "direction.H"
47#include "pTraits.H"
48#include "word.H"
49
50// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51
52namespace Foam
53{
54
55// Forward Declarations
56class Istream;
57class Ostream;
58
59// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
60
61//- A word representation of int32 value
62inline word name(const int32_t val)
63{
64 return word(std::to_string(val), false); // Needs no stripping
65}
66
67
68//- A word representation of int32 value
69template<>
70struct nameOp<int32_t>
71{
72 word operator()(const int32_t val) const
73 {
74 return word(std::to_string(val), false); // Needs no stripping
75 }
76};
77
78
79// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
80
81//- Read int32_t from stream
82int32_t readInt32(Istream& is);
83
84//- Parse entire buffer as a int32_t, skipping leading/trailing whitespace.
85// \return Parsed value or FatalIOError on any problem
86int32_t readInt32(const char* buf);
87
88//- Parse entire string as a int32_t, skipping leading/trailing whitespace.
89// \return Parsed value or FatalIOError on any problem
90inline int32_t readInt32(const std::string& str)
91{
92 return readInt32(str.c_str());
93}
94
95//- Read entire buffer as a int32_t, skipping leading/trailing whitespace.
96// \return True if successful.
97bool readInt32(const char* buf, int32_t& val);
98
99//- Read entire string as a int32_t, skipping leading/trailing whitespace.
100// \return True if successful.
101inline bool readInt32(const std::string& str, int32_t& val)
102{
103 return readInt32(str.c_str(), val);
104}
105
106//- Same as readInt32
107// \return True if successful.
108inline bool read(const char* buf, int32_t& val)
109{
110 return readInt32(buf, val);
111}
112
113//- Same as readInt32
114// \return True if successful.
115inline bool read(const std::string& str, int32_t& val)
116{
117 return readInt32(str, val);
118}
119
120
121Istream& operator>>(Istream& is, int32_t& val);
122Ostream& operator<<(Ostream& os, const int32_t val);
123
124// 32bit compilation with long as int32_t
125// - resolve explicitly for input and output
126//
127// Test works for gcc, icc, llvm.
128#if (__SIZEOF_LONG__ == 4)
129 Istream& operator>>(Istream& is, long& val);
130 Ostream& operator<<(Ostream& os, const long val);
131#endif
132
133
134//- Template specialization for pTraits<int32_t>
135template<>
136class pTraits<int32_t>
137{
138 int32_t p_;
139
140public:
141
142 // Typedefs
143
144 //- Component type
145 typedef int32_t cmptType;
146
147 //- Magnitude type
148 typedef int32_t magType;
149
150
151 // Member Constants
152
153 //- Dimensionality of space
154 static constexpr direction dim = 3;
155
156 //- Rank of int32_t is 0
157 static constexpr direction rank = 0;
158
159 //- Number of components in int32_t is 1
160 static constexpr direction nComponents = 1;
161
162
163 // Static Data Members
164
165 static const char* const typeName;
166 static const char* const componentNames[];
167 static const int32_t zero;
168 static const int32_t one;
169 static const int32_t min;
170 static const int32_t max;
171 static const int32_t rootMax;
172 static const int32_t rootMin;
173
174
175 // Constructors
176
177 //- Copy construct from primitive
178 explicit pTraits(int32_t val) noexcept
179 :
180 p_(val)
181 {}
182
183 //- Read construct from Istream
184 explicit pTraits(Istream& is);
185
186
187 // Member Functions
188
189 //- Return the value
190 operator int32_t() const noexcept
191 {
192 return p_;
193 }
194
195 //- Access the value
196 operator int32_t&() noexcept
197 {
198 return p_;
199 }
200};
201
202
203inline int32_t mag(const int32_t val)
204{
205 return ::abs(val);
206}
207
208
209// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
210
211} // End namespace Foam
212
213// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
214
215#endif
216
217// ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
static const char *const typeName
Definition: int32.H:165
static const int32_t one
Definition: int32.H:168
int32_t cmptType
Component type.
Definition: int32.H:145
int32_t magType
Magnitude type.
Definition: int32.H:148
static const int32_t rootMax
Definition: int32.H:171
static const int32_t rootMin
Definition: int32.H:172
pTraits(int32_t val) noexcept
Copy construct from primitive.
Definition: int32.H:178
static const int32_t min
Definition: int32.H:169
static const int32_t zero
Definition: int32.H:167
static const int32_t max
Definition: int32.H:170
A traits class, which is primarily used for primitives.
Definition: pTraits.H:59
A class for handling words, derived from Foam::string.
Definition: word.H:68
Direction is an 8-bit unsigned integer type used to represent Cartesian directions,...
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Istream & operator>>(Istream &, directionInfo &)
int32_t readInt32(Istream &is)
Read int32_t from stream.
Definition: int32IO.C:81
uint8_t direction
Definition: direction.H:56
const direction noexcept
Definition: Scalar.H:223
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
word operator()(const int32_t val) const
Definition: int32.H:72
Extract name (as a word) from an object, typically using its name() method.
Definition: word.H:238