Project
Loading...
Searching...
No Matches
bitfield.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
14
15#ifndef Q_BITFIELD_H
16#define Q_BITFIELD_H
17
18#if !defined(GPUCA_GPUCODE_DEVICE) && !defined(GPUCA_GPUCODE_COMPILEKERNELS)
19#include <type_traits>
20#endif
21
22template <class T, class S>
24{
25 public:
26 bitfield(T v) : bits((S)v) {}
27 bitfield(S v = 0) : bits(v) {}
28 bitfield(const bitfield&) = default;
29 bitfield& operator=(const bitfield&) = default;
30 bitfield operator|(const bitfield v) const { return bits | v.bits; }
31 bitfield operator|(const T v) const { return bits | static_cast<S>(v); }
33 {
34 bits |= v.bits;
35 return *this;
36 }
37 bitfield operator&(const bitfield v) const { return bits & v.bits; }
38 bitfield operator&(const T v) const { return bits & static_cast<S>(v); }
40 {
41 bits &= v.bits;
42 return *this;
43 }
44 bitfield operator~() const { return ~bits; }
45 bool operator==(const bitfield v) { return bits == v.bits; }
46 bool operator==(const T v) { return bits == static_cast<S>(v); }
47 bool operator!=(const bitfield v) { return bits != v.bits; }
48 bool operator!=(const T v) { return bits != static_cast<S>(v); }
49 bitfield& setBits(const bitfield v, bool w)
50 {
51 if (w) {
52 bits |= v.bits;
53 } else {
54 bits &= ~v.bits;
55 }
56 return *this;
57 }
58 void clear() { bits = 0; }
59 void set(S v) { bits = v; }
60 void set(T v) { bits = static_cast<S>(v); }
61 template <typename... Args>
62 void set(T v, Args... args)
63 {
64 this->set(args...);
65 bits |= static_cast<S>(v);
66 }
67 S get() const { return bits; }
68 operator bool() const { return bits; }
69 operator S() const { return bits; }
70 bool isSet(const bitfield& v) const { return *this & v; }
71 bool isSet(const S v) const { return bits & v; }
72 template <typename... Args>
73 bool isSetAll(Args... args)
74 {
75 return (bits & lor(args...).bits) == lor(args...).bits;
76 }
77 template <typename... Args>
78 bool isSetAny(Args... args)
79 {
80 return (bits & lor(args...).bits) != 0;
81 }
82 template <typename... Args>
83 bool isOnlySet(Args... args)
84 {
85 return (bits & ~lor(args...).bits) == 0;
86 }
87 template <typename... Args>
88 static bitfield lor(const Args&... args)
89 {
91 retVal.set(args...);
92 return retVal;
93 }
94
95#if !defined(GPUCA_GPUCODE_DEVICE)
96 static_assert(std::is_integral<S>::value, "Storage type non integral");
97 static_assert(sizeof(S) >= sizeof(T), "Storage type has insufficient capacity");
98#endif
99
100 private:
101 S bits;
102};
103
104#endif
int32_t retVal
bitfield & operator&=(const bitfield v)
Definition bitfield.h:39
void set(S v)
Definition bitfield.h:59
void set(T v, Args... args)
Definition bitfield.h:62
bool operator==(const bitfield v)
Definition bitfield.h:45
void clear()
Definition bitfield.h:58
bool isSet(const S v) const
Definition bitfield.h:71
bitfield(const bitfield &)=default
void set(T v)
Definition bitfield.h:60
bool operator!=(const bitfield v)
Definition bitfield.h:47
bool operator==(const T v)
Definition bitfield.h:46
bitfield operator&(const T v) const
Definition bitfield.h:38
bitfield(T v)
Definition bitfield.h:26
S get() const
Definition bitfield.h:67
bitfield operator|(const T v) const
Definition bitfield.h:31
bitfield operator|(const bitfield v) const
Definition bitfield.h:30
bitfield & setBits(const bitfield v, bool w)
Definition bitfield.h:49
bitfield & operator=(const bitfield &)=default
bitfield operator~() const
Definition bitfield.h:44
bitfield(S v=0)
Definition bitfield.h:27
bool isSetAll(Args... args)
Definition bitfield.h:73
bitfield & operator|=(const bitfield v)
Definition bitfield.h:32
bool isSetAny(Args... args)
Definition bitfield.h:78
static bitfield lor(const Args &... args)
Definition bitfield.h:88
bool isOnlySet(Args... args)
Definition bitfield.h:83
bool isSet(const bitfield &v) const
Definition bitfield.h:70
bitfield operator&(const bitfield v) const
Definition bitfield.h:37
bool operator!=(const T v)
Definition bitfield.h:48
const GLdouble * v
Definition glcorearb.h:832
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const void * bits
Definition glcorearb.h:4150
GLubyte GLubyte GLubyte GLubyte w
Definition glcorearb.h:852