[gjs/msvc.partial.fix: 1/2] enum-utils.h: Fix type ambiguity on Visual Studio




commit e2f459e59fb1a2497b37425e1b9bcc35ca4bd688
Author: Chun-wei Fan <fanchunwei src gnome org>
Date:   Mon Mar 8 16:56:12 2021 +0800

    enum-utils.h: Fix type ambiguity on Visual Studio
    
    Make the wrapper use the underlying type on Visual Studio builds, so
    that we can pinpoint on the "right" bitwise operator to use on Visual
    Studio builds, for the class enumeration(s) we may use.
    
    Also, on the same token, define casts for Visual Studio for the wrapped
    types for use on the '|=' and '&=' operators, as well as the '&' and '|'
    operators that will be used in value assignments so that the compiler
    does not get upset when converting between the underlying type and the
    class enumeration type.

 gjs/enum-utils.h | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
---
diff --git a/gjs/enum-utils.h b/gjs/enum-utils.h
index 80044b6d..a2c3d9c8 100644
--- a/gjs/enum-utils.h
+++ b/gjs/enum-utils.h
@@ -6,6 +6,9 @@
 
 #include <type_traits>
 
+#define CAST_WRAPPED_ENUM(x) static_cast<EnumType>(x)
+#define CAST_WRAPPED_ENUM_T(T,x) static_cast<T>(x)
+
 namespace GjsEnum {
 
 template <typename T>
@@ -30,9 +33,13 @@ struct WrapperImpl {
     }
 };
 
+#if defined (_MSC_VER) && !defined (__clang__)
+template <class EnumType>
+using Wrapper = std::conditional_t<is_class<EnumType>(), std::underlying_type_t<EnumType>, void>;
+#else
 template <class EnumType>
-using Wrapper =
-    std::conditional_t<is_class<EnumType>(), WrapperImpl<EnumType>, void>;
+using Wrapper = std::conditional_t<is_class<EnumType>(), WrapperImpl<EnumType>(), void>;
+#endif
 }  // namespace GjsEnum
 
 template <class EnumType, class Wrapped = GjsEnum::Wrapper<EnumType>>
@@ -60,7 +67,7 @@ template <class EnumType, class Wrapped = GjsEnum::Wrapper<EnumType>>
 constexpr std::enable_if_t<GjsEnum::is_class<EnumType>(), Wrapped&> operator|=(
     EnumType& first,  //  NOLINT(runtime/references)
     EnumType const& second) {
-    first = (first | second);
+    first = CAST_WRAPPED_ENUM(first | second);
     return reinterpret_cast<Wrapped&>(first);
 }
 
@@ -68,7 +75,7 @@ template <class EnumType, class Wrapped = GjsEnum::Wrapper<EnumType>>
 constexpr std::enable_if_t<GjsEnum::is_class<EnumType>(), Wrapped&> operator&=(
     EnumType& first,  //  NOLINT(runtime/references)
     EnumType const& second) {
-    first = (first & second);
+    first = CAST_WRAPPED_ENUM(first & second);
     return reinterpret_cast<Wrapped&>(first);
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]