Line data Source code
1 : /********************************************************************************
2 : * Copyright (c) 2024 Accenture
3 : *
4 : * This program and the accompanying materials are made available under the
5 : * terms of the Apache License Version 2.0 which is available at
6 : * https://www.apache.org/licenses/LICENSE-2.0
7 : *
8 : * SPDX-License-Identifier: Apache-2.0
9 : ********************************************************************************/
10 :
11 : /**
12 : * \file Mask.h
13 : */
14 : #pragma once
15 :
16 : #include <platform/estdint.h>
17 :
18 : // This class is in the global scope for backwards compatibility
19 : /**
20 : * Generic bitmask helper class
21 : *
22 : */
23 : template<typename T, typename V = uint8_t>
24 : class Mask
25 : {
26 : public:
27 : using mask_type = V;
28 : static_assert((sizeof(V) * 8) >= T::MAX_INDEX, "");
29 :
30 : Mask(V mask = V());
31 :
32 2424 : Mask(Mask const& mask) : fMask(mask.fMask) {}
33 :
34 : Mask& operator=(Mask const& mask)
35 : {
36 : fMask = mask.fMask;
37 : return *this;
38 : }
39 :
40 : Mask const operator&(Mask const& mask) const
41 : {
42 : V const result = static_cast<V>(fMask & mask.fMask);
43 : return Mask(result);
44 : }
45 :
46 : Mask const operator|(Mask const& mask) const
47 : {
48 : V const result = static_cast<V>(fMask | mask.fMask);
49 : return Mask(result);
50 : }
51 :
52 : Mask& operator|=(Mask const& mask)
53 : {
54 : fMask |= mask.fMask;
55 : return *this;
56 : }
57 :
58 : bool operator==(Mask const& mask) const { return fMask == mask.getMask(); }
59 :
60 : bool operator!=(Mask const& mask) const { return fMask != mask.getMask(); }
61 :
62 : V getMask() const { return fMask; }
63 :
64 : void setMask(V mask);
65 :
66 410 : void clear() { fMask = 0U; }
67 :
68 : bool isSet(V b) const;
69 :
70 135 : bool match(T const& t) const
71 : {
72 135 : V const v = static_cast<V>(1U) << t.toIndex();
73 135 : return (fMask & v) > 0U;
74 : }
75 :
76 : bool overlaps(Mask const& mask) const { return ((fMask & mask.getMask()) != 0U); }
77 :
78 : Mask& add(T const& t) { return (*this << t); }
79 :
80 : Mask& remove(T const& t) { return (*this >> t); }
81 :
82 313 : Mask& operator<<(T const& t)
83 : {
84 313 : V const v = static_cast<V>(1U) << t.toIndex();
85 313 : fMask = static_cast<V>(fMask | v);
86 313 : return *this;
87 : }
88 :
89 : Mask& operator>>(T const& t)
90 : {
91 : V const v = static_cast<V>(1U) << t.toIndex();
92 : fMask = (fMask & static_cast<V>(~v));
93 : return *this;
94 : }
95 :
96 224 : static V getOpenMask() { return ~static_cast<V>(0); }
97 :
98 410 : static Mask& getInstance()
99 : {
100 410 : static Mask theMaskInstance;
101 410 : theMaskInstance.clear();
102 410 : return theMaskInstance;
103 : }
104 :
105 : private:
106 : V fMask;
107 : };
108 :
109 : /*
110 : * inline
111 : */
112 :
113 : template<typename T, typename V>
114 497 : inline Mask<T, V>::Mask(V const mask) : fMask(mask)
115 497 : {}
116 :
117 : template<typename T, typename V>
118 : inline void Mask<T, V>::setMask(V const mask)
119 : {
120 : fMask = mask;
121 : }
122 :
123 : template<typename T, typename V>
124 : inline bool Mask<T, V>::isSet(V const b) const
125 : {
126 : V const v = static_cast<V>(1U) << b;
127 : return (fMask & v) > 0U;
128 : }
|