From b72b9dcf884d03ad9dafc20e1293404873188ed7 Mon Sep 17 00:00:00 2001
From: Henry Weller <http://cfd.direct>
Date: Fri, 6 May 2016 17:40:49 +0100
Subject: [PATCH] primitives/direction: Changed from "char" to "int8_t" and
 added IO operators to avoid problems when writing "nComponents"

---
 .../foamToVTK/patchWriterTemplates.C          |  6 +-
 .../foamToVTK/foamToVTK/writeFunsTemplates.C  |  6 +-
 src/OpenFOAM/Make/files                       |  2 +
 src/OpenFOAM/primitives/chars/char/charIO.C   | 25 ++----
 src/OpenFOAM/primitives/direction/direction.H | 29 +++++--
 .../primitives/direction/directionIO.C        | 86 +++++++++++++++++++
 src/OpenFOAM/primitives/ints/int32/int32IO.C  |  4 +-
 src/OpenFOAM/primitives/ints/int64/int64IO.C  |  4 +-
 .../primitives/ints/uint32/uint32IO.C         |  4 +-
 .../primitives/ints/uint64/uint64IO.C         |  4 +-
 10 files changed, 129 insertions(+), 41 deletions(-)
 create mode 100644 src/OpenFOAM/primitives/direction/directionIO.C

diff --git a/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/patchWriterTemplates.C b/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/patchWriterTemplates.C
index e67aa4c835..022df6630d 100644
--- a/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/patchWriterTemplates.C
+++ b/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/patchWriterTemplates.C
@@ -38,7 +38,7 @@ void Foam::patchWriter::write
     {
         const GeometricField<Type, fvPatchField, volMesh>& fld = flds[fieldi];
 
-        os_ << fld.name() << ' ' << int(pTraits<Type>::nComponents) << ' '
+        os_ << fld.name() << ' ' << pTraits<Type>::nComponents << ' '
             << nFaces_ << " float" << std::endl;
 
         DynamicList<floatScalar> fField(pTraits<Type>::nComponents*nFaces_);
@@ -74,7 +74,7 @@ void Foam::patchWriter::write
         const GeometricField<Type, pointPatchField, pointMesh>& fld =
             flds[fieldi];
 
-        os_ << fld.name() << ' ' << int(pTraits<Type>::nComponents) << ' '
+        os_ << fld.name() << ' ' << pTraits<Type>::nComponents << ' '
             << nPoints_ << " float" << std::endl;
 
         DynamicList<floatScalar> fField(pTraits<Type>::nComponents*nPoints_);
@@ -103,7 +103,7 @@ void Foam::patchWriter::write
     {
         const GeometricField<Type, fvPatchField, volMesh>& fld = flds[fieldi];
 
-        os_ << fld.name() << ' ' << int(pTraits<Type>::nComponents) << ' '
+        os_ << fld.name() << ' ' << pTraits<Type>::nComponents << ' '
             << nPoints_ << " float" << std::endl;
 
         DynamicList<floatScalar> fField(pTraits<Type>::nComponents*nPoints_);
diff --git a/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/writeFunsTemplates.C b/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/writeFunsTemplates.C
index 137dc10e9a..4dcf09ae2c 100644
--- a/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/writeFunsTemplates.C
+++ b/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/writeFunsTemplates.C
@@ -74,7 +74,7 @@ void Foam::writeFuns::write
 
     label nValues = mesh.nCells() + superCells.size();
 
-    os  << vvf.name() << ' ' << int(pTraits<Type>::nComponents) << ' '
+    os  << vvf.name() << ' ' << pTraits<Type>::nComponents << ' '
         << nValues << " float" << std::endl;
 
     DynamicList<floatScalar> fField(pTraits<Type>::nComponents*nValues);
@@ -106,7 +106,7 @@ void Foam::writeFuns::write
     const labelList& addPointCellLabels = topo.addPointCellLabels();
     const label nTotPoints = mesh.nPoints() + addPointCellLabels.size();
 
-    os  << pvf.name() << ' ' << int(pTraits<Type>::nComponents) << ' '
+    os  << pvf.name() << ' ' << pTraits<Type>::nComponents << ' '
         << nTotPoints << " float" << std::endl;
 
     DynamicList<floatScalar> fField(pTraits<Type>::nComponents*nTotPoints);
@@ -139,7 +139,7 @@ void Foam::writeFuns::write
     const labelList& addPointCellLabels = topo.addPointCellLabels();
     const label nTotPoints = mesh.nPoints() + addPointCellLabels.size();
 
-    os  << vvf.name() << ' ' << int(pTraits<Type>::nComponents) << ' '
+    os  << vvf.name() << ' ' << pTraits<Type>::nComponents << ' '
         << nTotPoints << " float" << std::endl;
 
     DynamicList<floatScalar> fField(pTraits<Type>::nComponents*nTotPoints);
diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files
index 34a1300503..a24631da12 100644
--- a/src/OpenFOAM/Make/files
+++ b/src/OpenFOAM/Make/files
@@ -15,6 +15,8 @@ chars = primitives/chars
 $(chars)/char/charIO.C
 $(chars)/wchar/wcharIO.C
 
+primitives/direction/directionIO.C
+
 ints = primitives/ints
 $(ints)/uint32/uint32.C
 $(ints)/uint32/uint32IO.C
diff --git a/src/OpenFOAM/primitives/chars/char/charIO.C b/src/OpenFOAM/primitives/chars/char/charIO.C
index 4de84ca9b9..ca69b91e05 100644
--- a/src/OpenFOAM/primitives/chars/char/charIO.C
+++ b/src/OpenFOAM/primitives/chars/char/charIO.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -21,20 +21,21 @@ License
     You should have received a copy of the GNU General Public License
     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
 
-Description
-    Reads a char from an input stream, for a given version
-    number and File format. If an ascii File is being read, then the line
-    numbers are counted and an erroneous read is reported.
-
 \*---------------------------------------------------------------------------*/
 
-#include "error.H"
-
 #include "char.H"
 #include "IOstreams.H"
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
+char Foam::readChar(Istream& is)
+{
+   char c;
+   is.read(c);
+   return c;
+}
+
+
 Foam::Istream& Foam::operator>>(Istream& is, char& c)
 {
     is.read(c);
@@ -51,14 +52,6 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const char c)
 }
 
 
-char Foam::readChar(Istream& is)
-{
-   char c;
-   is.read(c);
-   return c;
-}
-
-
 Foam::Ostream& Foam::operator<<(Ostream& os, const char* s)
 {
     os.write(s);
diff --git a/src/OpenFOAM/primitives/direction/direction.H b/src/OpenFOAM/primitives/direction/direction.H
index a7ca6d3fe2..459138fc50 100644
--- a/src/OpenFOAM/primitives/direction/direction.H
+++ b/src/OpenFOAM/primitives/direction/direction.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -25,23 +25,38 @@ Primitive
     direction
 
 Description
-    Direction is an integer type used to represent the Cartesian directions
-    etc.  Currently it is a typedef to char which is limited to the range
-    (0, 255) which should be adequate.
+    Direction is an 8-bit unsigned integer type used to represent the Cartesian
+    directions etc.
 
 \*---------------------------------------------------------------------------*/
 
 #ifndef direction_H
 #define direction_H
 
-#include "char.H"
+#define __STDC_LIMIT_MACROS
+#include <stdint.h>
+#include <iostream>
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
 namespace Foam
 {
-    typedef unsigned char direction;
-}
+
+class Istream;
+class Ostream;
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+typedef uint8_t direction;
+
+direction readDirection(Istream&);
+Istream& operator>>(Istream&, direction&);
+Ostream& operator<<(Ostream&, const direction);
+std::ostream& operator<<(std::ostream&, const direction);
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+} // End namespace Foam
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
diff --git a/src/OpenFOAM/primitives/direction/directionIO.C b/src/OpenFOAM/primitives/direction/directionIO.C
new file mode 100644
index 0000000000..f8a8812a01
--- /dev/null
+++ b/src/OpenFOAM/primitives/direction/directionIO.C
@@ -0,0 +1,86 @@
+/*---------------------------------------------------------------------------*\
+  =========                 |
+  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+   \\    /   O peration     |
+    \\  /    A nd           | Copyright (C) 2016 OpenFOAM Foundation
+     \\/     M anipulation  |
+-------------------------------------------------------------------------------
+License
+    This file is part of OpenFOAM.
+
+    OpenFOAM is free software: you can redistribute it and/or modify it
+    under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
+    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+    for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
+
+\*---------------------------------------------------------------------------*/
+
+#include "direction.H"
+#include "IOstreams.H"
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+Foam::direction Foam::readDirection(Istream& is)
+{
+    direction val;
+    is >> val;
+
+    return val;
+}
+
+
+Foam::Istream& Foam::operator>>(Istream& is, direction& d)
+{
+    token t(is);
+
+    if (!t.good())
+    {
+        is.setBad();
+        return is;
+    }
+
+    if (t.isLabel())
+    {
+        d = direction(t.labelToken());
+    }
+    else
+    {
+        is.setBad();
+        FatalIOErrorInFunction(is)
+            << "wrong token type - expected direction, found " << t.info()
+            << exit(FatalIOError);
+
+        return is;
+    }
+
+    // Check state of Istream
+    is.check("Istream& operator>>(Istream&, direction&)");
+
+    return is;
+}
+
+
+Foam::Ostream& Foam::operator<<(Ostream& os, const direction d)
+{
+    os.write(label(d));
+    os.check("Ostream& operator<<(Ostream&, const direction)");
+    return os;
+}
+
+
+std::ostream& Foam::operator<<(std::ostream& os, const direction d)
+{
+    os << int(d);
+    return os;
+}
+
+
+// ************************************************************************* //
diff --git a/src/OpenFOAM/primitives/ints/int32/int32IO.C b/src/OpenFOAM/primitives/ints/int32/int32IO.C
index 33dd078e35..4377f95992 100644
--- a/src/OpenFOAM/primitives/ints/int32/int32IO.C
+++ b/src/OpenFOAM/primitives/ints/int32/int32IO.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2014-2015 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2014-2016 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -23,8 +23,6 @@ License
 
 \*---------------------------------------------------------------------------*/
 
-#include "error.H"
-
 #include "int32.H"
 #include "IOstreams.H"
 
diff --git a/src/OpenFOAM/primitives/ints/int64/int64IO.C b/src/OpenFOAM/primitives/ints/int64/int64IO.C
index bb897d0d17..e9355a3012 100644
--- a/src/OpenFOAM/primitives/ints/int64/int64IO.C
+++ b/src/OpenFOAM/primitives/ints/int64/int64IO.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2014-2015 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2014-2016 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -23,8 +23,6 @@ License
 
 \*---------------------------------------------------------------------------*/
 
-#include "error.H"
-
 #include "int64.H"
 #include "IOstreams.H"
 
diff --git a/src/OpenFOAM/primitives/ints/uint32/uint32IO.C b/src/OpenFOAM/primitives/ints/uint32/uint32IO.C
index a57bcfbd4c..9111c2d741 100644
--- a/src/OpenFOAM/primitives/ints/uint32/uint32IO.C
+++ b/src/OpenFOAM/primitives/ints/uint32/uint32IO.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2014-2015 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2014-2016 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -23,8 +23,6 @@ License
 
 \*---------------------------------------------------------------------------*/
 
-#include "error.H"
-
 #include "uint32.H"
 #include "IOstreams.H"
 
diff --git a/src/OpenFOAM/primitives/ints/uint64/uint64IO.C b/src/OpenFOAM/primitives/ints/uint64/uint64IO.C
index d814a99664..dfb7ab4db6 100644
--- a/src/OpenFOAM/primitives/ints/uint64/uint64IO.C
+++ b/src/OpenFOAM/primitives/ints/uint64/uint64IO.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2014-2015 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2014-2016 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -23,8 +23,6 @@ License
 
 \*---------------------------------------------------------------------------*/
 
-#include "error.H"
-
 #include "uint64.H"
 #include "IOstreams.H"
 
-- 
GitLab