BmnRoot
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
42template<typename E>
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
60template<typename E>
61typename 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
67template<typename E>
68typename 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
74template<typename E>
75typename 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
81template<typename E>
82typename 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
88template<typename E>
89typename 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
96template<typename E>
97typename 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
104template<typename E>
105typename 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
std::enable_if< enable_bitmask_operators< E >::enable, E >::type operator~(E lhs)
std::enable_if< enable_bitmask_operators< E >::enable, E >::type operator^(E lhs, E rhs)
std::enable_if< enable_bitmask_operators< E >::enable, E & >::type operator&=(E &lhs, E rhs)
std::enable_if< enable_bitmask_operators< E >::enable, E & >::type operator|=(E &lhs, E rhs)
std::enable_if< enable_bitmask_operators< E >::enable, E >::type operator&(E lhs, E rhs)
std::enable_if< enable_bitmask_operators< E >::enable, E >::type operator|(E lhs, E rhs)
std::enable_if< enable_bitmask_operators< E >::enable, E & >::type operator^=(E &lhs, E rhs)