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