From 672f0574e28186c81d8f06ceac08173a37d091a6 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Tue, 24 Apr 2018 13:17:22 +0200
Subject: [PATCH] COMP: resolve signed/unsigned long ambiguity on Darwin

---
 applications/test/limits/Test-limits.C        |  63 ++++++++++
 applications/test/machine-sizes/Make/files    |   3 +
 applications/test/machine-sizes/Make/options  |   2 +
 .../test/machine-sizes/Test-machine-sizes.cpp | 112 ++++++++++++++++++
 src/OpenFOAM/primitives/Scalar/Scalar.H       |   4 +-
 src/OpenFOAM/primitives/bools/bool/bool.H     |   4 +-
 src/OpenFOAM/primitives/ints/int32/int32.H    |   9 +-
 src/OpenFOAM/primitives/ints/int64/int64.H    |  13 +-
 src/OpenFOAM/primitives/ints/int64/int64IO.C  |  11 +-
 src/OpenFOAM/primitives/ints/uint32/uint32.H  |   4 +-
 src/OpenFOAM/primitives/ints/uint64/uint64.H  |  13 +-
 .../primitives/ints/uint64/uint64IO.C         |  11 +-
 12 files changed, 230 insertions(+), 19 deletions(-)
 create mode 100644 applications/test/limits/Test-limits.C
 create mode 100644 applications/test/machine-sizes/Make/files
 create mode 100644 applications/test/machine-sizes/Make/options
 create mode 100644 applications/test/machine-sizes/Test-machine-sizes.cpp

diff --git a/applications/test/limits/Test-limits.C b/applications/test/limits/Test-limits.C
new file mode 100644
index 00000000000..4123d475d82
--- /dev/null
+++ b/applications/test/limits/Test-limits.C
@@ -0,0 +1,63 @@
+/*---------------------------------------------------------------------------*\
+  =========                 |
+  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+   \\    /   O peration     |
+    \\  /    A nd           | Copyright (C) 2018 OpenCFD Ltd.
+     \\/     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/>.
+
+Description
+    Print max limits.
+
+\*---------------------------------------------------------------------------*/
+
+#include <limits>
+#include "int.H"
+#include "uint.H"
+#include "string.H"
+#include "IOstreams.H"
+
+using namespace Foam;
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+// Main program:
+
+int main(int argc, char *argv[])
+{
+    //NONE Info<<"int16:" << pTraits<int16_t>::max << nl;
+    Info<<"int16:" << std::numeric_limits<int16_t>::max() << nl;
+    Info<<"int32:" << pTraits<int32_t>::max << nl;
+    Info<<"int64:" << pTraits<int64_t>::max << nl;
+    Info<<"uint32:" << pTraits<uint32_t>::max << nl;
+    Info<<"uint64:" << pTraits<uint64_t>::max << nl;
+
+    Info<< nl;
+
+    cout<<"int16:" << std::numeric_limits<int16_t>::max() << nl;
+    cout<<"int32:" << pTraits<int32_t>::max << nl;
+    cout<<"int64:" << pTraits<int64_t>::max << nl;
+    cout<<"uint32:" << pTraits<uint32_t>::max << nl;
+    cout<<"uint64:" << pTraits<uint64_t>::max << nl;
+
+    Info << "---\nEnd\n" << endl;
+
+    return 0;
+}
+
+
+// ************************************************************************* //
diff --git a/applications/test/machine-sizes/Make/files b/applications/test/machine-sizes/Make/files
new file mode 100644
index 00000000000..15c18ac3210
--- /dev/null
+++ b/applications/test/machine-sizes/Make/files
@@ -0,0 +1,3 @@
+Test-machine-sizes.cpp
+
+EXE = $(FOAM_USER_APPBIN)/Test-machine-sizes
diff --git a/applications/test/machine-sizes/Make/options b/applications/test/machine-sizes/Make/options
new file mode 100644
index 00000000000..18e6fe47afa
--- /dev/null
+++ b/applications/test/machine-sizes/Make/options
@@ -0,0 +1,2 @@
+/* EXE_INC = */
+/* EXE_LIBS = */
diff --git a/applications/test/machine-sizes/Test-machine-sizes.cpp b/applications/test/machine-sizes/Test-machine-sizes.cpp
new file mode 100644
index 00000000000..784fcfbca85
--- /dev/null
+++ b/applications/test/machine-sizes/Test-machine-sizes.cpp
@@ -0,0 +1,112 @@
+/*---------------------------------------------------------------------------*\
+  =========                 |
+  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+   \\    /   O peration     |
+    \\  /    A nd           | Copyright (C) 2018 OpenCFD Ltd.
+     \\/     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/>.
+
+Description
+    Test the sizeof for basic types. Can be compiled and run without
+    any OpenFOAM libraries.
+
+\*---------------------------------------------------------------------------*/
+
+#include <cstdint>
+#include <climits>
+#include <cstdlib>
+#include <limits>
+#include <iostream>
+
+// Can also compile without OpenFOAM
+#ifdef WM_LABEL_SIZE
+    #include "IOstreams.H"
+#endif
+
+template<class T>
+void print(const char* msg)
+{
+    std::cout<< msg << ' ' << sizeof(T) << '\n';
+}
+
+
+#ifdef WM_LABEL_SIZE
+template<class T>
+void printMax(const char* msg)
+{
+    std::cout<< msg << ' ' << sizeof(T)
+        << " max "
+        << std::numeric_limits<T>::max() << '\n';
+
+    Foam::Info<< msg << ' ' << sizeof(T)
+        << " max "
+        << long(std::numeric_limits<T>::max()) << '\n';
+}
+#endif
+
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+// Main program:
+
+int main(int argc, char *argv[])
+{
+    std::cout<<"machine sizes\n---\n\n";
+
+    print<mode_t>("mode_t");
+    print<short>("short");
+    print<int>("int");
+    print<long>("long");
+    print<long long>("long long");
+    print<unsigned short>("ushort");
+    print<unsigned int>("uint");
+    print<unsigned long>("ulong");
+    print<unsigned long long>("ulong-long");
+    print<int16_t>("int16");
+    print<int32_t>("int32");
+    print<int64_t>("int64");
+    print<uint16_t>("uint16");
+    print<uint32_t>("uint32");
+    print<uint64_t>("uint64");
+    print<float>("float");
+    print<double>("double");
+    print<std::string>("std::string");
+    print<std::string::size_type>("std::string::size_type");
+
+    #ifdef WM_LABEL_SIZE
+    std::cout<<"\nmax values\n---\n\n";
+
+    printMax<mode_t>("mode_t");
+    Foam::Info<< "mode_t 0777: " << mode_t(0777) << '\n';
+
+    printMax<short>("short");
+    printMax<int>("int");
+    printMax<long>("long");
+    printMax<unsigned short>("ushort");
+    printMax<unsigned int>("uint");
+    printMax<unsigned long>("ulong");
+    printMax<float>("float");
+    printMax<double>("double");
+    #endif
+
+    std::cout << "\n---\nEnd\n\n";
+
+    return 0;
+}
+
+
+// ************************************************************************* //
diff --git a/src/OpenFOAM/primitives/Scalar/Scalar.H b/src/OpenFOAM/primitives/Scalar/Scalar.H
index e351068785a..f091f427650 100644
--- a/src/OpenFOAM/primitives/Scalar/Scalar.H
+++ b/src/OpenFOAM/primitives/Scalar/Scalar.H
@@ -89,13 +89,13 @@ public:
 
     // Member Functions
 
-        //- Access to the Scalar value
+        //- Access to the value
         operator Scalar() const
         {
             return p_;
         }
 
-        //- Access to the Scalar value
+        //- Access to the value
         operator Scalar&()
         {
             return p_;
diff --git a/src/OpenFOAM/primitives/bools/bool/bool.H b/src/OpenFOAM/primitives/bools/bool/bool.H
index 55ac2cfbff4..9d413995f3f 100644
--- a/src/OpenFOAM/primitives/bools/bool/bool.H
+++ b/src/OpenFOAM/primitives/bools/bool/bool.H
@@ -103,13 +103,13 @@ public:
 
     // Member Functions
 
-        //- Access to the bool value
+        //- Access to the value
         operator bool() const
         {
             return p_;
         }
 
-        //- Access to the bool value
+        //- Access to the value
         operator bool&()
         {
             return p_;
diff --git a/src/OpenFOAM/primitives/ints/int32/int32.H b/src/OpenFOAM/primitives/ints/int32/int32.H
index 497e7b8b5ef..2d4910a1599 100644
--- a/src/OpenFOAM/primitives/ints/int32/int32.H
+++ b/src/OpenFOAM/primitives/ints/int32/int32.H
@@ -107,9 +107,8 @@ inline bool read(const std::string& str, int32_t& val)
 Istream& operator>>(Istream& is, int32_t& val);
 Ostream& operator<<(Ostream& os, const int32_t val);
 
-// On 32bit OSs long is not unambiguously int32_t (or int64_t) causing problems
-// for IO operator resolution.
-// This problem is avoided by explicitly defining the following operators:
+// 32bit OS: long is not unambiguously (int32_t | int64_t)
+// - resolve explicitly for input and output
 #if WM_ARCH_OPTION == 32
     Istream& operator>>(Istream& is, long& val);
     Ostream& operator<<(Ostream& os, const long val);
@@ -163,13 +162,13 @@ public:
 
     // Member Functions
 
-        //- Access to the int32_t value
+        //- Access to the value
         operator int32_t() const
         {
             return p_;
         }
 
-        //- Access to the int value
+        //- Access to the value
         operator int32_t&()
         {
             return p_;
diff --git a/src/OpenFOAM/primitives/ints/int64/int64.H b/src/OpenFOAM/primitives/ints/int64/int64.H
index c6d097c437c..36dce6ee04b 100644
--- a/src/OpenFOAM/primitives/ints/int64/int64.H
+++ b/src/OpenFOAM/primitives/ints/int64/int64.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2014-2016 OpenFOAM Foundation
-     \\/     M anipulation  | Copyright (C) 2016-2017 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2016-2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -108,6 +108,13 @@ inline bool read(const std::string& str, int64_t& val)
 Istream& operator>>(Istream& is, int64_t& val);
 Ostream& operator<<(Ostream& os, const int64_t val);
 
+// On Darwin: long is not unambiguously (int32_t | int64_t)
+// - explicitly resolve for output
+#if WM_ARCH_OPTION == 64 && defined(darwin)
+    Ostream& operator<<(Ostream& os, const long val);
+#endif
+
+
 //- Template specialization for pTraits<int64_t>
 template<>
 class pTraits<int64_t>
@@ -155,13 +162,13 @@ public:
 
     // Member Functions
 
-        //- Access to the int64_t value
+        //- Access to the value
         operator int64_t() const
         {
             return p_;
         }
 
-        //- Access to the int value
+        //- Access to the value
         operator int64_t&()
         {
             return p_;
diff --git a/src/OpenFOAM/primitives/ints/int64/int64IO.C b/src/OpenFOAM/primitives/ints/int64/int64IO.C
index 1a2ad639e3f..bb0c08e1e0b 100644
--- a/src/OpenFOAM/primitives/ints/int64/int64IO.C
+++ b/src/OpenFOAM/primitives/ints/int64/int64IO.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2014-2016 OpenFOAM Foundation
-     \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2017-2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -120,4 +120,13 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const int64_t val)
 }
 
 
+#if WM_ARCH_OPTION == 64 && defined(darwin)
+Foam::Ostream& Foam::operator<<(Ostream& os, const long val)
+{
+    os << int64_t(val);
+    return os;
+}
+#endif
+
+
 // ************************************************************************* //
diff --git a/src/OpenFOAM/primitives/ints/uint32/uint32.H b/src/OpenFOAM/primitives/ints/uint32/uint32.H
index 8841d878f71..94e3c13598b 100644
--- a/src/OpenFOAM/primitives/ints/uint32/uint32.H
+++ b/src/OpenFOAM/primitives/ints/uint32/uint32.H
@@ -154,13 +154,13 @@ public:
 
     // Member Functions
 
-        //- Access to the uint32_t value
+        //- Access to the value
         operator uint32_t() const
         {
             return p_;
         }
 
-        //- Access to the uint32_t value
+        //- Access to the value
         operator uint32_t&()
         {
             return p_;
diff --git a/src/OpenFOAM/primitives/ints/uint64/uint64.H b/src/OpenFOAM/primitives/ints/uint64/uint64.H
index 7c63a695c31..8bc75023f1c 100644
--- a/src/OpenFOAM/primitives/ints/uint64/uint64.H
+++ b/src/OpenFOAM/primitives/ints/uint64/uint64.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2014-2016 OpenFOAM Foundation
-     \\/     M anipulation  | Copyright (C) 2016-2017 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2016-2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -107,6 +107,13 @@ inline bool read(const std::string& str, uint64_t& val)
 Istream& operator>>(Istream& is, uint64_t& val);
 Ostream& operator<<(Ostream& os, const uint64_t val);
 
+// On Darwin: unsigned long is not unambiguously (uint32_t | uint64_t)
+// - explicitly resolve for output
+#if WM_ARCH_OPTION == 64 && defined(darwin)
+    Ostream& operator<<(Ostream& os, const unsigned long val);
+#endif
+
+
 //- Template specialization for pTraits<uint64_t>
 template<>
 class pTraits<uint64_t>
@@ -154,13 +161,13 @@ public:
 
     // Member Functions
 
-        //- Access to the uint64_t value
+        //- Access to the value
         operator uint64_t() const
         {
             return p_;
         }
 
-        //- Access to the uint64_t value
+        //- Access to the value
         operator uint64_t&()
         {
             return p_;
diff --git a/src/OpenFOAM/primitives/ints/uint64/uint64IO.C b/src/OpenFOAM/primitives/ints/uint64/uint64IO.C
index b73e90e7714..1fb0f5adaff 100644
--- a/src/OpenFOAM/primitives/ints/uint64/uint64IO.C
+++ b/src/OpenFOAM/primitives/ints/uint64/uint64IO.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2014-2016 OpenFOAM Foundation
-     \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2017-2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -119,4 +119,13 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const uint64_t val)
 }
 
 
+#if WM_ARCH_OPTION == 64 && defined(darwin)
+Foam::Ostream& Foam::operator<<(Ostream& os, const unsigned long val)
+{
+    os << uint64_t(val);
+    return os;
+}
+#endif
+
+
 // ************************************************************************* //
-- 
GitLab