[Libreoffice-commits] core.git: include/o3tl

Stephan Bergmann sbergman at redhat.com
Fri Apr 10 00:54:04 PDT 2015


 include/o3tl/typed_flags_set.hxx |  108 ++++++++++++++++++++++++++++++---------
 include/o3tl/underlying_type.hxx |    2 
 2 files changed, 86 insertions(+), 24 deletions(-)

New commits:
commit 6bee77922c2571a7b55f099531f024def3e1036b
Author: Stephan Bergmann <sbergman at redhat.com>
Date:   Thu Apr 9 17:31:19 2015 +0200

    Get rid of false GCC 4.7 -Werror=type-limits in template code
    
    Change-Id: I4b13d3b7d74690401234dfa30c8c1b6647820b6a
    Reviewed-on: https://gerrit.libreoffice.org/15216
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Tor Lillqvist <tml at collabora.com>
    Tested-by: Tor Lillqvist <tml at collabora.com>

diff --git a/include/o3tl/typed_flags_set.hxx b/include/o3tl/typed_flags_set.hxx
index 3e96003..6151bab 100644
--- a/include/o3tl/typed_flags_set.hxx
+++ b/include/o3tl/typed_flags_set.hxx
@@ -23,11 +23,29 @@
 #include <sal/config.h>
 
 #include <cassert>
+#include <type_traits>
 
 #include <o3tl/underlying_type.hxx>
 
 namespace o3tl {
 
+namespace detail {
+
+template<typename T> inline
+typename std::enable_if<std::is_signed<T>::value, bool>::type isNonNegative(
+    T value)
+{
+    return value >= 0;
+}
+
+template<typename T> inline
+typename std::enable_if<std::is_unsigned<T>::value, bool>::type isNonNegative(T)
+{
+    return true;
+}
+
+}
+
 template<typename T> struct typed_flags {};
 
 /// Mark a (scoped) enumeration as a set of bit flags, with accompanying
@@ -54,7 +72,7 @@ struct is_typed_flags {
     public:
         explicit Wrap(typename underlying_type<E>::type value):
             value_(value)
-        { assert(value >= 0); }
+        { assert(detail::isNonNegative(value)); }
 
         operator E() { return static_cast<E>(value_); }
 
@@ -73,7 +91,9 @@ struct is_typed_flags {
 
 template<typename E>
 inline typename o3tl::typed_flags<E>::Wrap operator ~(E rhs) {
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(rhs) >= 0);
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(rhs)));
     return static_cast<typename o3tl::typed_flags<E>::Wrap>(
         o3tl::typed_flags<E>::mask
         & ~static_cast<typename o3tl::underlying_type<E>::type>(rhs));
@@ -90,8 +110,12 @@ inline typename o3tl::typed_flags<E>::Wrap operator ~(
 
 template<typename E>
 inline typename o3tl::typed_flags<E>::Wrap operator ^(E lhs, E rhs) {
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(lhs) >= 0);
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(rhs) >= 0);
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(lhs)));
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(rhs)));
     return static_cast<typename o3tl::typed_flags<E>::Wrap>(
         static_cast<typename o3tl::underlying_type<E>::type>(lhs)
         ^ static_cast<typename o3tl::underlying_type<E>::type>(rhs));
@@ -101,7 +125,9 @@ template<typename E>
 inline typename o3tl::typed_flags<E>::Wrap operator ^(
     E lhs, typename o3tl::typed_flags<E>::Wrap rhs)
 {
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(lhs) >= 0);
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(lhs)));
     return static_cast<typename o3tl::typed_flags<E>::Wrap>(
         static_cast<typename o3tl::underlying_type<E>::type>(lhs)
         ^ static_cast<typename o3tl::underlying_type<E>::type>(rhs));
@@ -111,7 +137,9 @@ template<typename E>
 inline typename o3tl::typed_flags<E>::Wrap operator ^(
     typename o3tl::typed_flags<E>::Wrap lhs, E rhs)
 {
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(rhs) >= 0);
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(rhs)));
     return static_cast<typename o3tl::typed_flags<E>::Wrap>(
         static_cast<typename o3tl::underlying_type<E>::type>(lhs)
         ^ static_cast<typename o3tl::underlying_type<E>::type>(rhs));
@@ -129,8 +157,12 @@ inline typename o3tl::typed_flags<E>::Wrap operator ^(
 
 template<typename E>
 inline typename o3tl::typed_flags<E>::Wrap operator &(E lhs, E rhs) {
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(lhs) >= 0);
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(rhs) >= 0);
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(lhs)));
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(rhs)));
     return static_cast<typename o3tl::typed_flags<E>::Wrap>(
         static_cast<typename o3tl::underlying_type<E>::type>(lhs)
         & static_cast<typename o3tl::underlying_type<E>::type>(rhs));
@@ -140,7 +172,9 @@ template<typename E>
 inline typename o3tl::typed_flags<E>::Wrap operator &(
     E lhs, typename o3tl::typed_flags<E>::Wrap rhs)
 {
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(lhs) >= 0);
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(lhs)));
     return static_cast<typename o3tl::typed_flags<E>::Wrap>(
         static_cast<typename o3tl::underlying_type<E>::type>(lhs)
         & static_cast<typename o3tl::underlying_type<E>::type>(rhs));
@@ -150,7 +184,9 @@ template<typename E>
 inline typename o3tl::typed_flags<E>::Wrap operator &(
     typename o3tl::typed_flags<E>::Wrap lhs, E rhs)
 {
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(rhs) >= 0);
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(rhs)));
     return static_cast<typename o3tl::typed_flags<E>::Wrap>(
         static_cast<typename o3tl::underlying_type<E>::type>(lhs)
         & static_cast<typename o3tl::underlying_type<E>::type>(rhs));
@@ -168,8 +204,12 @@ inline typename o3tl::typed_flags<E>::Wrap operator &(
 
 template<typename E>
 inline typename o3tl::typed_flags<E>::Wrap operator |(E lhs, E rhs) {
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(lhs) >= 0);
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(rhs) >= 0);
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(lhs)));
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(rhs)));
     return static_cast<typename o3tl::typed_flags<E>::Wrap>(
         static_cast<typename o3tl::underlying_type<E>::type>(lhs)
         | static_cast<typename o3tl::underlying_type<E>::type>(rhs));
@@ -179,7 +219,9 @@ template<typename E>
 inline typename o3tl::typed_flags<E>::Wrap operator |(
     E lhs, typename o3tl::typed_flags<E>::Wrap rhs)
 {
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(lhs) >= 0);
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(lhs)));
     return static_cast<typename o3tl::typed_flags<E>::Wrap>(
         static_cast<typename o3tl::underlying_type<E>::type>(lhs)
         | static_cast<typename o3tl::underlying_type<E>::type>(rhs));
@@ -189,7 +231,9 @@ template<typename E>
 inline typename o3tl::typed_flags<E>::Wrap operator |(
     typename o3tl::typed_flags<E>::Wrap lhs, E rhs)
 {
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(rhs) >= 0);
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(rhs)));
     return static_cast<typename o3tl::typed_flags<E>::Wrap>(
         static_cast<typename o3tl::underlying_type<E>::type>(lhs)
         | static_cast<typename o3tl::underlying_type<E>::type>(rhs));
@@ -207,8 +251,12 @@ inline typename o3tl::typed_flags<E>::Wrap operator |(
 
 template<typename E>
 inline typename o3tl::typed_flags<E>::Self operator &=(E & lhs, E rhs) {
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(lhs) >= 0);
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(rhs) >= 0);
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(lhs)));
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(rhs)));
     lhs = lhs & rhs;
     return lhs;
 }
@@ -217,15 +265,21 @@ template<typename E>
 inline typename o3tl::typed_flags<E>::Self operator &=(
     E & lhs, typename o3tl::typed_flags<E>::Wrap rhs)
 {
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(lhs) >= 0);
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(lhs)));
     lhs = lhs & rhs;
     return lhs;
 }
 
 template<typename E>
 inline typename o3tl::typed_flags<E>::Self operator |=(E & lhs, E rhs) {
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(lhs) >= 0);
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(rhs) >= 0);
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(lhs)));
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(rhs)));
     lhs = lhs | rhs;
     return lhs;
 }
@@ -234,15 +288,21 @@ template<typename E>
 inline typename o3tl::typed_flags<E>::Self operator |=(
     E & lhs, typename o3tl::typed_flags<E>::Wrap rhs)
 {
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(lhs) >= 0);
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(lhs)));
     lhs = lhs | rhs;
     return lhs;
 }
 
 template<typename E>
 inline typename o3tl::typed_flags<E>::Self operator ^=(E & lhs, E rhs) {
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(lhs) >= 0);
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(rhs) >= 0);
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(lhs)));
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(rhs)));
     lhs = lhs ^ rhs;
     return lhs;
 }
@@ -251,7 +311,9 @@ template<typename E>
 inline typename o3tl::typed_flags<E>::Self operator ^=(
     E & lhs, typename o3tl::typed_flags<E>::Wrap rhs)
 {
-    assert(static_cast<typename o3tl::underlying_type<E>::type>(lhs) >= 0);
+    assert(
+        o3tl::detail::isNonNegative(
+            static_cast<typename o3tl::underlying_type<E>::type>(lhs)));
     lhs = lhs ^ rhs;
     return lhs;
 }
diff --git a/include/o3tl/underlying_type.hxx b/include/o3tl/underlying_type.hxx
index 3d8b625..4b2e077 100644
--- a/include/o3tl/underlying_type.hxx
+++ b/include/o3tl/underlying_type.hxx
@@ -17,7 +17,7 @@
 namespace o3tl {
 
 template<typename T> struct underlying_type {
-#if defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ <= 7 && \
+#if defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ <= 6 && \
         !defined __clang__
     typedef int type;
 #else


More information about the Libreoffice-commits mailing list