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