Loading...
Searching...
No Matches
label.C
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-2015 OpenFOAM Foundation
9 Copyright (C) 2019-2025 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
27\*---------------------------------------------------------------------------*/
28
29#include "error.H"
30#include "label.H"
31#include "Istream.H"
32
33// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34
35#if WM_LABEL_SIZE == 32
36const char* const Foam::pTraits<int32_t>::typeName = "label";
37const char* const Foam::pTraits<int64_t>::typeName = "int64";
38#elif WM_LABEL_SIZE == 64
39const char* const Foam::pTraits<int32_t>::typeName = "int32";
40const char* const Foam::pTraits<int64_t>::typeName = "label";
41#endif
42
43
44// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
45
46namespace
47{
48
49// Binary reading with narrowing/widening
50template<class readType, class dataType>
51void reading(Foam::Istream& is, dataType* data, size_t nElem)
52{
53 if constexpr (sizeof(dataType) == sizeof(readType))
54 {
55 // Read uses the native data size
56 is.readRaw(reinterpret_cast<char*>(data), nElem*sizeof(dataType));
57 }
58 else
59 {
60 for (const dataType* last = data + nElem; data != last; ++data)
61 {
62 readType val;
63 is.readRaw(reinterpret_cast<char*>(&val), sizeof(readType));
64
65 if constexpr (sizeof(dataType) < sizeof(readType))
66 {
67 // Narrowing: currently only need (int32 <- int64)
68 *data = Foam::narrowInt32(val);
69 }
70 else
71 {
72 // Type widening
73 *data = dataType(val);
74 }
75 }
76 }
78
79} // End anonymous namespace
80
81
82// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
83
84void Foam::readRawLabel(Istream& is, label* data, size_t nElem)
85{
86 // No check for binary vs ascii, the caller knows what they are doing
87
88 switch (is.labelByteSize())
89 {
90 case sizeof(int32_t):
91 {
92 reading<int32_t, label>(is, data, nElem);
93 break;
94 }
95 case sizeof(int64_t):
96 {
97 reading<int64_t, label>(is, data, nElem);
98 break;
99 }
100 default:
101 {
102 // Cannot recover from this
104 << "Currently no code to read int" << (8*is.labelByteSize())
105 << " as int" << (8*sizeof(label)) << nl
106 << abort(FatalIOError);
108 }
109}
110
111
112// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
113
114Foam::label Foam::pow(label a, label b)
115{
116 label ans = 1;
117 for (label i=0; i<b; i++)
118 {
119 ans *= a;
120 }
121
122 #ifdef FULLDEBUG
123 if (b < 0)
124 {
126 << "negative value for b is not supported"
127 << abort(FatalError);
129 #endif
130
131 return ans;
132}
133
134
135Foam::label Foam::factorial(label n)
136{
137 static label factTable[13] =
138 {
139 1, 1, 2, 6, 24, 120, 720, 5040, 40320,
140 362880, 3628800, 39916800, 479001600
141 };
142
143 #ifdef FULLDEBUG
144 if (n < 0 || n > 12)
145 {
147 << "n value out of range"
148 << abort(FatalError);
149 }
150 #endif
151
152 return factTable[n];
153}
154
155
156// ************************************************************************* //
label n
unsigned labelByteSize() const noexcept
The sizeof (label) in bytes associated with the stream.
Definition IOstream.H:332
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
virtual Istream & readRaw(char *data, std::streamsize count)=0
Low-level raw binary read (without possible block delimiters). Reading into a null pointer shall idea...
A traits class, which is primarily used for primitives and vector-space.
Definition pTraits.H:64
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
void readRawLabel(Istream &is, label *data, size_t nElem=1)
Read raw label(s) from binary stream.
Definition label.C:77
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
errorManip< error > abort(error &err)
Definition errorManip.H:139
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
constexpr int32_t narrowInt32(const int64_t val) noexcept
Type narrowing from int64_t to int32_t.
Definition label.H:248
label factorial(label n)
Evaluate n! : 0 < n <= 12.
Definition label.C:128
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
volScalarField & b