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




commit ab2c1bfb82be732298fc5b00b69a6d7f9495e331
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 | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
---
diff --git a/gjs/enum-utils.h b/gjs/enum-utils.h
index 80044b6d..ddfe4526 100644
--- a/gjs/enum-utils.h
+++ b/gjs/enum-utils.h
@@ -6,6 +6,18 @@
 
 #include <type_traits>
 
+#ifdef _MSC_VER
+#define CAST_ENUM_WRAPPER \
+    std::conditional_t<is_class<EnumType>(), std::underlying_type_t<EnumType>, void>
+        
+#define CAST_WRAPPED_ENUM(x) static_cast<EnumType>(x)
+#define CAST_WRAPPED_ENUM_T(T,x) static_cast<T>(x)
+#else
+#define CAST_ENUM_WRAPPER std::conditional_t<is_class<EnumType>(), WrapperImpl<EnumType>, void>
+#define CAST_WRAPPED_ENUM(x) (x)
+#define CAST_WRAPPED_ENUM_T(T,x) (x)
+#endif
+
 namespace GjsEnum {
 
 template <typename T>
@@ -31,8 +43,7 @@ struct WrapperImpl {
 };
 
 template <class EnumType>
-using Wrapper =
-    std::conditional_t<is_class<EnumType>(), WrapperImpl<EnumType>, void>;
+using Wrapper = CAST_ENUM_WRAPPER;
 }  // namespace GjsEnum
 
 template <class EnumType, class Wrapped = GjsEnum::Wrapper<EnumType>>
@@ -60,7 +71,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 +79,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]