LIEF: Library to Instrument Executable Formats Version 0.15.1
Loading...
Searching...
No Matches
enums.hpp
Go to the documentation of this file.
1/* Copyright 2021 - 2024 R. Thomas
2 * Copyright 2021 - 2024 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_ENUMS_H
17#define LIEF_ENUMS_H
18#include <type_traits>
19
20#define _LIEF_EN(N) class N : size_t
21#define _LIEF_EN_2(N, TYPE) class N : TYPE
22#define _LIEF_EI(X) X
23
24#define ENABLE_BITMASK_OPERATORS(X) \
25template<> \
26struct EnableBitMaskOperators<X> \
27{ \
28 static const bool bit_mask_enabled = true; \
29};
30
31template<typename Enum>
33{
34 static const bool bit_mask_enabled = false;
35};
36
37template<typename Enum>
38typename std::enable_if<EnableBitMaskOperators<Enum>::bit_mask_enabled, Enum>::type
39operator |(Enum lhs, Enum rhs)
40{
41 using underlying = typename std::underlying_type<Enum>::type;
42 return static_cast<Enum> (
43 static_cast<underlying>(lhs) |
44 static_cast<underlying>(rhs)
45 );
46}
47
48template<typename Enum>
49typename std::enable_if<EnableBitMaskOperators<Enum>::bit_mask_enabled, Enum>::type
50operator &(Enum lhs, Enum rhs)
51{
52 using underlying = typename std::underlying_type<Enum>::type;
53 return static_cast<Enum> (
54 static_cast<underlying>(lhs) &
55 static_cast<underlying>(rhs)
56 );
57}
58
59template<typename Enum>
60typename std::enable_if<EnableBitMaskOperators<Enum>::bit_mask_enabled, Enum>::type
62{
63 using underlying = typename std::underlying_type<Enum>::type;
64 return static_cast<Enum>(~static_cast<underlying>(e));
65}
66
67template<typename Enum>
68typename std::enable_if<EnableBitMaskOperators<Enum>::bit_mask_enabled, typename std::add_lvalue_reference<Enum>::type>::type
69operator |=(Enum& lhs, Enum rhs)
70{
71 using underlying = typename std::underlying_type<Enum>::type;
72 lhs = static_cast<Enum>(static_cast<underlying>(lhs) | static_cast<underlying>(rhs));
73 return lhs;
74}
75
76template<typename Enum>
77typename std::enable_if<EnableBitMaskOperators<Enum>::bit_mask_enabled, typename std::add_lvalue_reference<Enum>::type>::type
78operator &=(Enum& lhs, Enum rhs)
79{
80 using underlying = typename std::underlying_type<Enum>::type;
81 lhs = static_cast<Enum>(static_cast<underlying>(lhs) & static_cast<underlying>(rhs));
82 return lhs;
83}
84
85template<typename Enum>
86typename std::enable_if<EnableBitMaskOperators<Enum>::bit_mask_enabled, bool>::type
87is_true(Enum e)
88{
89 using underlying = typename std::underlying_type<Enum>::type;
90 return static_cast<underlying>(e) > 0;
91}
92#endif
std::enable_if< EnableBitMaskOperators< Enum >::bit_mask_enabled, bool >::type is_true(Enum e)
Definition enums.hpp:87
std::enable_if< EnableBitMaskOperators< Enum >::bit_mask_enabled, Enum >::type operator|(Enum lhs, Enum rhs)
Definition enums.hpp:39
std::enable_if< EnableBitMaskOperators< Enum >::bit_mask_enabled, Enum >::type operator&(Enum lhs, Enum rhs)
Definition enums.hpp:50
std::enable_if< EnableBitMaskOperators< Enum >::bit_mask_enabled, typenamestd::add_lvalue_reference< Enum >::type >::type operator&=(Enum &lhs, Enum rhs)
Definition enums.hpp:78
std::enable_if< EnableBitMaskOperators< Enum >::bit_mask_enabled, typenamestd::add_lvalue_reference< Enum >::type >::type operator|=(Enum &lhs, Enum rhs)
Definition enums.hpp:69
std::enable_if< EnableBitMaskOperators< Enum >::bit_mask_enabled, Enum >::type operator~(Enum e)
Definition enums.hpp:61
Definition enums.hpp:33
static const bool bit_mask_enabled
Definition enums.hpp:34