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