aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Core/MultiEnum.h
blob: 36b69b1b41b3aa96547f32466493f1e192463f72 (plain) (blame)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#pragma once

#include <cstdint>
#include <type_traits>

// Wrapper around enum that can have multiple values (or none) set at once.
template <typename EnumTypeT, typename StorageTypeT = std::uint64_t>
struct MultiEnum
{
    using StorageType = StorageTypeT;
    using EnumType = EnumTypeT;

    MultiEnum() = default;

    template <typename... EnumValues>
    requires std::conjunction_v<std::is_same<EnumTypeT, EnumValues>...>
    constexpr explicit MultiEnum(EnumValues... v) : MultiEnum((toBitFlag(v) | ... | 0u))
    {}

    template <typename ValueType>
    requires std::is_convertible_v<ValueType, StorageType>
    constexpr explicit MultiEnum(ValueType v)
        : bitset(v)
    {
        static_assert(std::is_unsigned_v<ValueType>);
        static_assert(std::is_unsigned_v<StorageType> && std::is_integral_v<StorageType>);
    }

    MultiEnum(const MultiEnum & other) = default;
    MultiEnum & operator=(const MultiEnum & other) = default;

    bool isSet(EnumType value) const
    {
        return bitset & toBitFlag(value);
    }

    void set(EnumType value)
    {
        bitset |= toBitFlag(value);
    }

    void unSet(EnumType value)
    {
        bitset &= ~(toBitFlag(value));
    }

    void reset()
    {
        bitset = 0;
    }

    StorageType getValue() const
    {
        return bitset;
    }

    template <typename ValueType>
    requires std::is_convertible_v<ValueType, StorageType>
    void setValue(ValueType new_value)
    {
        // Can't set value from any enum avoid confusion
        static_assert(!std::is_enum_v<ValueType>);
        bitset = new_value;
    }

    bool operator==(const MultiEnum & other) const
    {
        return bitset == other.bitset;
    }

    template <typename ValueType>
    requires std::is_convertible_v<ValueType, StorageType>
    bool operator==(ValueType other) const
    {
        // Shouldn't be comparable with any enum to avoid confusion
        static_assert(!std::is_enum_v<ValueType>);
        return bitset == other;
    }

    template <typename U>
    bool operator!=(U && other) const
    {
        return !(*this == other);
    }

    template <typename ValueType>
    requires std::is_convertible_v<ValueType, StorageType>
    friend bool operator==(ValueType left, MultiEnum right)
    {
        return right.operator==(left);
    }

    template <typename L>
    requires (!std::is_same_v<L, MultiEnum>)
    friend bool operator!=(L left, MultiEnum right)
    {
        return !(right.operator==(left));
    }

private:
    StorageType bitset = 0;

    static constexpr StorageType toBitFlag(EnumType v) { return StorageType{1} << static_cast<StorageType>(v); }
};