Tileson 1.4.0
A helpful json parser for Tiled maps
Loading...
Searching...
No Matches
EnumBitflags.hpp
Go to the documentation of this file.
1//
2// Created by robin on 08.11.2020.
3//
4
5#ifndef TILESON_ENUMBITFLAGS_HPP
6#define TILESON_ENUMBITFLAGS_HPP
7
8#include <type_traits>
9#include <iostream>
10
11#define TILESON_ENABLE_BITMASK_OPERATORS(x) \
12namespace tson { \
13 ENABLE_BITMASK_OPERATORS(x) \
14}
15
16namespace tson
17{
18 #define ENABLE_BITMASK_OPERATORS(x) \
19 template<> \
20 struct EnableBitMaskOperators<x> \
21 { \
22 static const bool enable = true; \
23 };
24
25 template<typename Enum>
27 {
28 static const bool enable = false;
29 };
30
31
32 template<typename Enum>
33 typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type
35 {
36 static_assert(std::is_enum<Enum>::value,
37 "template parameter is not an enum type");
38
39 using underlying = typename std::underlying_type<Enum>::type;
40
41 return static_cast<Enum> (
42 static_cast<underlying>(lhs) |
43 static_cast<underlying>(rhs)
44 );
45 }
46
47 //Permissions operator &(Permissions lhs, Permissions rhs)
48 template<typename Enum>
49 typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type
51 {
52 static_assert(std::is_enum<Enum>::value,
53 "template parameter is not an enum type");
54
55 using underlying = typename std::underlying_type<Enum>::type;
56
57 return static_cast<Enum> (
58 static_cast<underlying>(lhs) &
59 static_cast<underlying>(rhs)
60 );
61 }
62
63 //Permissions operator ^(Permissions lhs, Permissions rhs)
64 template<typename Enum>
65 typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type
67 {
68 static_assert(std::is_enum<Enum>::value,
69 "template parameter is not an enum type");
70
71 using underlying = typename std::underlying_type<Enum>::type;
72
73 return static_cast<Enum> (
74 static_cast<underlying>(lhs) ^
75 static_cast<underlying>(rhs)
76 );
77 }
78
79 //Permissions operator ~(Permissions rhs)
80 template<typename Enum>
81 typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type
83 {
84 static_assert(std::is_enum<Enum>::value,
85 "template parameter is not an enum type");
86
87 using underlying = typename std::underlying_type<Enum>::type;
88
89 return static_cast<Enum> (
90 ~static_cast<underlying>(rhs)
91 );
92 }
93
94 //Permissions& operator |=(Permissions &lhs, Permissions rhs)
95 template<typename Enum>
96 typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type
97 &operator |=(Enum &lhs, Enum rhs)
98 {
99 static_assert(std::is_enum<Enum>::value,
100 "template parameter is not an enum type");
101
102 using underlying = typename std::underlying_type<Enum>::type;
103
104 lhs = static_cast<Enum> (
105 static_cast<underlying>(lhs) |
106 static_cast<underlying>(rhs)
107 );
108
109 return lhs;
110 }
111
112 //Permissions& operator &=(Permissions &lhs, Permissions rhs)
113 template<typename Enum>
114 typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type
116 {
117 static_assert(std::is_enum<Enum>::value,
118 "template parameter is not an enum type");
119
120 using underlying = typename std::underlying_type<Enum>::type;
121
122 lhs = static_cast<Enum> (
123 static_cast<underlying>(lhs) &
124 static_cast<underlying>(rhs)
125 );
126
127 return lhs;
128 }
129
130 //Permissions& operator ^=(Permissions &lhs, Permissions rhs)
131 template<typename Enum>
132 typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type
134 {
135 static_assert(std::is_enum<Enum>::value,
136 "template parameter is not an enum type");
137
138 using underlying = typename std::underlying_type<Enum>::type;
139
140 lhs = static_cast<Enum> (
141 static_cast<underlying>(lhs) ^
142 static_cast<underlying>(rhs)
143 );
144
145 return lhs;
146 }
147}
148
149#endif //TILESON_ENUMBITFLAGS_HPP
Definition Base64.hpp:12
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator~(Enum rhs)
Definition EnumBitflags.hpp:82
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator|(Enum lhs, Enum rhs)
Definition EnumBitflags.hpp:34
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator^(Enum lhs, Enum rhs)
Definition EnumBitflags.hpp:66
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type & operator&=(Enum &lhs, Enum rhs)
Definition EnumBitflags.hpp:115
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type & operator^=(Enum &lhs, Enum rhs)
Definition EnumBitflags.hpp:133
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator&(Enum lhs, Enum rhs)
Definition EnumBitflags.hpp:50
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type & operator|=(Enum &lhs, Enum rhs)
Definition EnumBitflags.hpp:97
Definition EnumBitflags.hpp:27
static const bool enable
Definition EnumBitflags.hpp:28