Branch data Line data Source code
1 : : /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 : : // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
3 : : // SPDX-FileCopyrightText: 2020 Marco Trevisan <marco.trevisan@canonical.com>
4 : :
5 : : #pragma once
6 : :
7 : : #include <config.h>
8 : :
9 : : #include <type_traits>
10 : :
11 : : namespace GjsEnum {
12 : :
13 : : template <typename T>
14 : : constexpr bool is_class() {
15 : : if constexpr (std::is_enum_v<T>) {
16 : : return !std::is_convertible_v<T, std::underlying_type_t<T>>;
17 : : }
18 : : return false;
19 : : }
20 : :
21 : : template <class EnumType>
22 : : struct WrapperImpl {
23 : : EnumType e;
24 : :
25 : 198450 : constexpr explicit WrapperImpl(EnumType const& en) : e(en) {}
26 : 99225 : constexpr explicit WrapperImpl(std::underlying_type_t<EnumType> const& en)
27 : 99225 : : e(static_cast<EnumType>(en)) {}
28 : 7800 : constexpr explicit operator bool() const { return static_cast<bool>(e); }
29 : : constexpr operator EnumType() const { return e; }
30 : 289875 : constexpr operator std::underlying_type_t<EnumType>() const {
31 : 289875 : return std::underlying_type_t<EnumType>(e);
32 : : }
33 : : };
34 : :
35 : :
36 : : #if defined (__clang__) || defined (__GNUC__)
37 : : template <class EnumType>
38 : : using Wrapper =
39 : : std::conditional_t<is_class<EnumType>(), WrapperImpl<EnumType>, void>;
40 : : #else
41 : : template <class EnumType>
42 : : using Wrapper =
43 : : std::conditional_t<is_class<EnumType>(), std::underlying_type_t<EnumType>, void>;
44 : : #endif
45 : : } // namespace GjsEnum
46 : :
47 : : template <class EnumType, class Wrapped = GjsEnum::Wrapper<EnumType>>
48 : 99225 : constexpr std::enable_if_t<GjsEnum::is_class<EnumType>(), Wrapped> operator&(
49 : : EnumType const& first, EnumType const& second) {
50 : 198450 : return static_cast<Wrapped>(static_cast<Wrapped>(first) &
51 : 198450 : static_cast<Wrapped>(second));
52 : : }
53 : :
54 : : template <class EnumType, class Wrapped = GjsEnum::Wrapper<EnumType>>
55 : : constexpr std::enable_if_t<GjsEnum::is_class<EnumType>(), Wrapped> operator|(
56 : : EnumType const& first, EnumType const& second) {
57 : : return static_cast<Wrapped>(static_cast<Wrapped>(first) |
58 : : static_cast<Wrapped>(second));
59 : : }
60 : :
61 : : template <class EnumType, class Wrapped = GjsEnum::Wrapper<EnumType>>
62 : : constexpr std::enable_if_t<GjsEnum::is_class<EnumType>(), Wrapped> operator^(
63 : : EnumType const& first, EnumType const& second) {
64 : : return static_cast<Wrapped>(static_cast<Wrapped>(first) ^
65 : : static_cast<Wrapped>(second));
66 : : }
67 : :
68 : : template <class EnumType, class Wrapped = GjsEnum::Wrapper<EnumType>>
69 : 29937 : constexpr std::enable_if_t<GjsEnum::is_class<EnumType>(), Wrapped&> operator|=(
70 : : EnumType& first, // NOLINT(runtime/references)
71 : : EnumType const& second) {
72 : 29937 : first = static_cast<EnumType>(first | second);
73 : 29937 : return reinterpret_cast<Wrapped&>(first);
74 : : }
75 : :
76 : : template <class EnumType, class Wrapped = GjsEnum::Wrapper<EnumType>>
77 : : constexpr std::enable_if_t<GjsEnum::is_class<EnumType>(), Wrapped&> operator&=(
78 : : EnumType& first, // NOLINT(runtime/references)
79 : : EnumType const& second) {
80 : : first = static_cast<EnumType>(first & second);
81 : : return reinterpret_cast<Wrapped&>(first);
82 : : }
83 : :
84 : : template <class EnumType, class Wrapped = GjsEnum::Wrapper<EnumType>>
85 : : constexpr std::enable_if_t<GjsEnum::is_class<EnumType>(), EnumType> operator~(
86 : : EnumType const& first) {
87 : : return static_cast<EnumType>(~static_cast<Wrapped>(first));
88 : : }
|