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



commit 9849975bfca0a17ee0d1c2c39803f373322ac2a0
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 | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
---
diff --git a/gjs/enum-utils.h b/gjs/enum-utils.h
index 80044b6d..679e8c16 100644
--- a/gjs/enum-utils.h
+++ b/gjs/enum-utils.h
@@ -6,6 +6,16 @@
 
 #include <type_traits>
 
+#if defined (_MSC_VER) && !defined (__clang__)
+#    define ENUM_WRAP_UNDERLYING_TYPE std::conditional_t<is_class<EnumType>(), 
std::underlying_type_t<EnumType>, void>
+#    define CAST_WRAPPED(x) static_cast<EnumType>(x)
+#    define CAST_WRAPPED_T(T,x) static_cast<T>(x)
+#else
+#    define ENUM_WRAP_UNDERLYING_TYPE std::conditional_t<is_class<EnumType>(), WrapperImpl<EnumType>(), void>
+#    define CAST_WRAPPED(x) (x)
+#    define CAST_WRAPPED_T(T,x) (x)
+#endif
+
 namespace GjsEnum {
 
 template <typename T>
@@ -31,8 +41,7 @@ struct WrapperImpl {
 };
 
 template <class EnumType>
-using Wrapper =
-    std::conditional_t<is_class<EnumType>(), WrapperImpl<EnumType>, void>;
+using Wrapper = ENUM_WRAP_UNDERLYING_TYPE;
 }  // namespace GjsEnum
 
 template <class EnumType, class Wrapped = GjsEnum::Wrapper<EnumType>>
@@ -60,7 +69,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(first | second);
     return reinterpret_cast<Wrapped&>(first);
 }
 
@@ -68,7 +77,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(first & second);
     return reinterpret_cast<Wrapped&>(first);
 }
 


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