BmnRoot
26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Toggle main menu visibility
Loading...
Searching...
No Matches
bitmask_operators.h
Go to the documentation of this file.
1
/* Copyright (C) 2017-2021 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2
SPDX-License-Identifier: GPL-3.0-only
3
Authors: Pierre-Alain Loizeau [committer] */
4
5
#ifndef JSS_BITMASK_HPP
6
#define JSS_BITMASK_HPP
7
8
// (C) Copyright 2015 Just Software Solutions Ltd
9
//
10
// Distributed under the Boost Software License, Version 1.0.
11
//
12
// Boost Software License - Version 1.0 - August 17th, 2003
13
//
14
// Permission is hereby granted, free of charge, to any person or
15
// organization obtaining a copy of the software and accompanying
16
// documentation covered by this license (the "Software") to use,
17
// reproduce, display, distribute, execute, and transmit the
18
// Software, and to prepare derivative works of the Software, and
19
// to permit third-parties to whom the Software is furnished to
20
// do so, all subject to the following:
21
//
22
// The copyright notices in the Software and this entire
23
// statement, including the above license grant, this restriction
24
// and the following disclaimer, must be included in all copies
25
// of the Software, in whole or in part, and all derivative works
26
// of the Software, unless such copies or derivative works are
27
// solely in the form of machine-executable object code generated
28
// by a source language processor.
29
//
30
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
31
// KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
32
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
33
// PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
34
// COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE
35
// LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN
36
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
37
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
38
// THE SOFTWARE.
39
40
#include <type_traits>
41
42
template
<
typename
E>
43
struct
enable_bitmask_operators
44
{
45
static
const
bool
enable
=
false
;
46
};
47
48
/*********** Preproc macro for simpler use ****************************/
49
/* copied 17/07/2017 from
50
* http://blog.bitwigglers.org/using-enum-classes-as-type-safe-bitmasks/
51
*/
52
#define ENABLE_BITMASK_OPERATORS(x) \
53
template<> \
54
struct enable_bitmask_operators<x> \
55
{ \
56
static const bool enable = true; \
57
};
58
/**********************************************************************/
59
60
template
<
typename
E>
61
typename
std::enable_if<enable_bitmask_operators<E>::enable, E>::type
operator|
(E lhs, E rhs)
62
{
63
typedef
typename
std::underlying_type<E>::type underlying;
64
return
static_cast<
E
>
(
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
operator&
(E lhs, E rhs)
69
{
70
typedef
typename
std::underlying_type<E>::type underlying;
71
return
static_cast<
E
>
(
static_cast<
underlying
>
(lhs) &
static_cast<
underlying
>
(rhs));
72
}
73
74
template
<
typename
E>
75
typename
std::enable_if<enable_bitmask_operators<E>::enable, E>::type
operator^
(E lhs, E rhs)
76
{
77
typedef
typename
std::underlying_type<E>::type underlying;
78
return
static_cast<
E
>
(
static_cast<
underlying
>
(lhs) ^
static_cast<
underlying
>
(rhs));
79
}
80
81
template
<
typename
E>
82
typename
std::enable_if<enable_bitmask_operators<E>::enable, E>::type
operator~
(E lhs)
83
{
84
typedef
typename
std::underlying_type<E>::type underlying;
85
return
static_cast<
E
>
(~static_cast<underlying>(lhs));
86
}
87
88
template
<
typename
E>
89
typename
std::enable_if<enable_bitmask_operators<E>::enable, E&>::type
operator|=
(E& lhs, E rhs)
90
{
91
typedef
typename
std::underlying_type<E>::type underlying;
92
lhs =
static_cast<
E
>
(
static_cast<
underlying
>
(lhs) |
static_cast<
underlying
>
(rhs));
93
return
lhs;
94
}
95
96
template
<
typename
E>
97
typename
std::enable_if<enable_bitmask_operators<E>::enable, E&>::type
operator&=
(E& lhs, E rhs)
98
{
99
typedef
typename
std::underlying_type<E>::type underlying;
100
lhs =
static_cast<
E
>
(
static_cast<
underlying
>
(lhs) &
static_cast<
underlying
>
(rhs));
101
return
lhs;
102
}
103
104
template
<
typename
E>
105
typename
std::enable_if<enable_bitmask_operators<E>::enable, E&>::type
operator^=
(E& lhs, E rhs)
106
{
107
typedef
typename
std::underlying_type<E>::type underlying;
108
lhs =
static_cast<
E
>
(
static_cast<
underlying
>
(lhs) ^
static_cast<
underlying
>
(rhs));
109
return
lhs;
110
}
111
112
#endif
operator~
std::enable_if< enable_bitmask_operators< E >::enable, E >::type operator~(E lhs)
Definition
bitmask_operators.h:82
operator^
std::enable_if< enable_bitmask_operators< E >::enable, E >::type operator^(E lhs, E rhs)
Definition
bitmask_operators.h:75
operator&=
std::enable_if< enable_bitmask_operators< E >::enable, E & >::type operator&=(E &lhs, E rhs)
Definition
bitmask_operators.h:97
operator|=
std::enable_if< enable_bitmask_operators< E >::enable, E & >::type operator|=(E &lhs, E rhs)
Definition
bitmask_operators.h:89
operator&
std::enable_if< enable_bitmask_operators< E >::enable, E >::type operator&(E lhs, E rhs)
Definition
bitmask_operators.h:68
operator|
std::enable_if< enable_bitmask_operators< E >::enable, E >::type operator|(E lhs, E rhs)
Definition
bitmask_operators.h:61
operator^=
std::enable_if< enable_bitmask_operators< E >::enable, E & >::type operator^=(E &lhs, E rhs)
Definition
bitmask_operators.h:105
enable_bitmask_operators
Definition
bitmask_operators.h:44
enable_bitmask_operators::enable
static const bool enable
Definition
bitmask_operators.h:45
install
include
bitmask_operators.h
Generated on
for BmnRoot by
1.17.0