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