qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From astitc...@apache.org
Subject qpid-cpp git commit: QPID-7392: More judicious templates (stolen from c++11) to avoid warnings
Date Fri, 12 Aug 2016 22:18:46 GMT
Repository: qpid-cpp
Updated Branches:
  refs/heads/master e65f39fe5 -> 2a9e20b68


QPID-7392: More judicious templates (stolen from c++11) to avoid warnings


Project: http://git-wip-us.apache.org/repos/asf/qpid-cpp/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-cpp/commit/2a9e20b6
Tree: http://git-wip-us.apache.org/repos/asf/qpid-cpp/tree/2a9e20b6
Diff: http://git-wip-us.apache.org/repos/asf/qpid-cpp/diff/2a9e20b6

Branch: refs/heads/master
Commit: 2a9e20b688d130b1e9cbddf1ee4ac9d24434bb4c
Parents: e65f39f
Author: Andrew Stitcher <astitcher@apache.org>
Authored: Fri Aug 12 17:59:24 2016 -0400
Committer: Andrew Stitcher <astitcher@apache.org>
Committed: Fri Aug 12 17:59:24 2016 -0400

----------------------------------------------------------------------
 src/qpid/framing/FieldValue.cpp | 28 ++++++++++-----------
 src/qpid/types/Variant.cpp      | 49 ++++++++++++++++++++++++++++++------
 2 files changed, 56 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-cpp/blob/2a9e20b6/src/qpid/framing/FieldValue.cpp
----------------------------------------------------------------------
diff --git a/src/qpid/framing/FieldValue.cpp b/src/qpid/framing/FieldValue.cpp
index d2bfe89..56b0d0b 100644
--- a/src/qpid/framing/FieldValue.cpp
+++ b/src/qpid/framing/FieldValue.cpp
@@ -47,30 +47,30 @@ template<int W> struct FloatType{};
 template<> struct FloatType<4> { typedef float Type; };
 template<> struct FloatType<8> { typedef double Type; };
 
+// Stolen from C++11
+template <bool, class T=void> struct enable_if {};
+template <class T> struct enable_if<true, T> { typedef T type; };
+
 // Construct the right subclass of FixedWidthValue for numeric types using width and kind.
 // Kind 1=int, 2=unsigned int, 3=float
-template<int W> FixedWidthValue<W>* numericFixedWidthValue(uint8_t kind) {
+template<int W>
+typename enable_if<(W<3), FixedWidthValue<W>*>::type
+numericFixedWidthValue(uint8_t kind) {
     switch (kind) {
       case 1: return new FixedWidthIntValue<typename IntType<W>::Type>();
       case 2: return new FixedWidthIntValue<typename UintType<W>::Type>();
-      case 3: return new FixedWidthFloatValue<typename FloatType<W>::Type>();
       default: return new FixedWidthValue<W>();
     }
 }
 
-template<> FixedWidthValue<1>* numericFixedWidthValue<1>(uint8_t kind)
{
-    switch (kind) {
-      case 1: return new FixedWidthIntValue<typename IntType<1>::Type>();
-      case 2: return new FixedWidthIntValue<typename UintType<1>::Type>();
-      default: return new FixedWidthValue<1>();
-    }
-}
-
-template<> FixedWidthValue<2>* numericFixedWidthValue<2>(uint8_t kind)
{
+template<int W>
+typename enable_if<(W>=3), FixedWidthValue<W>*>::type
+numericFixedWidthValue(uint8_t kind) {
     switch (kind) {
-      case 1: return new FixedWidthIntValue<typename IntType<2>::Type>();
-      case 2: return new FixedWidthIntValue<typename UintType<2>::Type>();
-      default: return new FixedWidthValue<2>();
+      case 1: return new FixedWidthIntValue<typename IntType<W>::Type>();
+      case 2: return new FixedWidthIntValue<typename UintType<W>::Type>();
+      case 3: return new FixedWidthFloatValue<typename FloatType<W>::Type>();
+      default: return new FixedWidthValue<W>();
     }
 }
 

http://git-wip-us.apache.org/repos/asf/qpid-cpp/blob/2a9e20b6/src/qpid/types/Variant.cpp
----------------------------------------------------------------------
diff --git a/src/qpid/types/Variant.cpp b/src/qpid/types/Variant.cpp
index 26dbe0c..eb2ff42 100644
--- a/src/qpid/types/Variant.cpp
+++ b/src/qpid/types/Variant.cpp
@@ -31,6 +31,27 @@
 namespace qpid {
 namespace types {
 
+// Stolen from C++11
+template <bool, class T=void> struct enable_if {};
+template <class T> struct enable_if<true, T> { typedef T type; };
+
+struct true_type { static const bool value = true; };
+struct false_type { static const bool value = false; };
+
+template <class T> struct is_signed : public false_type {};
+template <> struct is_signed<uint8_t> : public false_type {};
+template <> struct is_signed<uint16_t> : public false_type {};
+template <> struct is_signed<uint32_t> : public false_type {};
+template <> struct is_signed<uint64_t> : public false_type {};
+template <> struct is_signed<int8_t> : public true_type {};
+template <> struct is_signed<int16_t> : public true_type {};
+template <> struct is_signed<int32_t> : public true_type {};
+template <> struct is_signed<int64_t> : public true_type {};
+
+template <> struct is_signed<float> : public true_type {};
+template <> struct is_signed<double> : public true_type {};
+
+
 namespace {
 const std::string EMPTY;
 const std::string PREFIX("invalid conversion: ");
@@ -115,7 +136,8 @@ class VariantImpl
     } value;
     std::string encoding;       // Optional encoding for variable length data.
 
-  template<class T> T convertFromString() const
+    template<class T>
+    typename enable_if<is_signed<T>::value, T>::type convertFromString() const
     {
         const std::string& s = *value.string;
 
@@ -125,18 +147,31 @@ class VariantImpl
             if ( s[0] != '-' ) {
                 return boost::lexical_cast<T>(s);
             } else {
-                T r = boost::lexical_cast<T>(s.substr(1));
-                if (std::numeric_limits<T>::is_signed) {
-                    return -r;
-                } else {
-                    if (r==0) return 0;
-                }
+                return -boost::lexical_cast<T>(s.substr(1));
             }
         } catch(const boost::bad_lexical_cast&) {
         }
         throw InvalidConversion(QPID_MSG("Cannot convert " << s));
     }
 
+    template<class T>
+    typename enable_if<!is_signed<T>::value, T>::type convertFromString() const
+    {
+        const std::string& s = *value.string;
+
+        try {
+            // Extra shenanigans to work around negative zero
+            // conversion error in older GCC libs.
+            if ( s[0] != '-' ) {
+                return boost::lexical_cast<T>(s);
+            } else {
+                T r = boost::lexical_cast<T>(s.substr(1));
+                if (r==0) return 0;
+            }
+        } catch(const boost::bad_lexical_cast&) {
+        }
+        throw InvalidConversion(QPID_MSG("Cannot convert " << s));
+    }
 };
 
 VariantImpl::VariantImpl() : type(VAR_VOID) {}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


Mime
View raw message