1
+ #ifndef JSS_BITMASK_HPP
2
+ #define JSS_BITMASK_HPP
3
+
4
+ // (C) Copyright 2015 Just Software Solutions Ltd
5
+ //
6
+ // Distributed under the Boost Software License, Version 1.0.
7
+ //
8
+ // Boost Software License - Version 1.0 - August 17th, 2003
9
+ //
10
+ // Permission is hereby granted, free of charge, to any person or
11
+ // organization obtaining a copy of the software and accompanying
12
+ // documentation covered by this license (the "Software") to use,
13
+ // reproduce, display, distribute, execute, and transmit the
14
+ // Software, and to prepare derivative works of the Software, and
15
+ // to permit third-parties to whom the Software is furnished to
16
+ // do so, all subject to the following:
17
+ //
18
+ // The copyright notices in the Software and this entire
19
+ // statement, including the above license grant, this restriction
20
+ // and the following disclaimer, must be included in all copies
21
+ // of the Software, in whole or in part, and all derivative works
22
+ // of the Software, unless such copies or derivative works are
23
+ // solely in the form of machine-executable object code generated
24
+ // by a source language processor.
25
+ //
26
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
27
+ // KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
28
+ // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
29
+ // PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
30
+ // COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE
31
+ // LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN
32
+ // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33
+ // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34
+ // THE SOFTWARE.
35
+
36
+ #include < type_traits>
37
+
38
+ namespace shogun {
39
+
40
+ template <typename E>
41
+ struct enable_bitmask_operators {
42
+ static constexpr bool enable = false ;
43
+ };
44
+
45
+ #define enableEnumClassBitmask (T ) template <> \
46
+ struct enable_bitmask_operators <T> \
47
+ { \
48
+ static constexpr bool enable = true ; \
49
+ }
50
+
51
+ template <typename E>
52
+ typename std::enable_if<enable_bitmask_operators<E>::enable, E>::type
53
+ operator |(E lhs, E rhs) {
54
+ typedef typename std::underlying_type<E>::type underlying;
55
+ return static_cast <E>(
56
+ static_cast <underlying>(lhs) | static_cast <underlying>(rhs));
57
+ }
58
+
59
+ template <typename E>
60
+ typename std::enable_if<enable_bitmask_operators<E>::enable, E>::type
61
+ operator &(E lhs, E rhs) {
62
+ typedef typename std::underlying_type<E>::type underlying;
63
+ return static_cast <E>(
64
+ static_cast <underlying>(lhs) & static_cast <underlying>(rhs));
65
+ }
66
+
67
+ template <typename E>
68
+ typename std::enable_if<enable_bitmask_operators<E>::enable, E>::type
69
+ operator ^(E lhs, E rhs) {
70
+ typedef typename std::underlying_type<E>::type underlying;
71
+ return static_cast <E>(
72
+ static_cast <underlying>(lhs) ^ static_cast <underlying>(rhs));
73
+ }
74
+
75
+ template <typename E>
76
+ typename std::enable_if<enable_bitmask_operators<E>::enable, E>::type
77
+ operator ~(E lhs) {
78
+ typedef typename std::underlying_type<E>::type underlying;
79
+ return static_cast <E>(
80
+ ~static_cast <underlying>(lhs));
81
+ }
82
+
83
+ template <typename E>
84
+ typename std::enable_if<enable_bitmask_operators<E>::enable, E &>::type
85
+ operator |=(E &lhs, E rhs) {
86
+ typedef typename std::underlying_type<E>::type underlying;
87
+ lhs = static_cast <E>(
88
+ static_cast <underlying>(lhs) | static_cast <underlying>(rhs));
89
+ return lhs;
90
+ }
91
+
92
+ template <typename E>
93
+ typename std::enable_if<enable_bitmask_operators<E>::enable, E &>::type
94
+ operator &=(E &lhs, E rhs) {
95
+ typedef typename std::underlying_type<E>::type underlying;
96
+ lhs = static_cast <E>(
97
+ static_cast <underlying>(lhs) & static_cast <underlying>(rhs));
98
+ return lhs;
99
+ }
100
+
101
+ template <typename E>
102
+ typename std::enable_if<enable_bitmask_operators<E>::enable, E &>::type
103
+ operator ^=(E &lhs, E rhs) {
104
+ typedef typename std::underlying_type<E>::type underlying;
105
+ lhs = static_cast <E>(
106
+ static_cast <underlying>(lhs) ^ static_cast <underlying>(rhs));
107
+ return lhs;
108
+ }
109
+ }
110
+ #endif
0 commit comments