forked from CopernicaMarketingSoftware/AMQP-CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbooleanset.h
163 lines (147 loc) · 3.21 KB
/
booleanset.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* BooleanSet.h
*
* AMQP can store eight booleans in a single byte. This class
* is a utility class for setting and getting the eight
* booleans
*
* @copyright 2014 Copernica BV
*/
/**
* Set up namespace
*/
namespace AMQP {
/**
* Class definition
*/
class BooleanSet : public Field
{
private:
/**
* The actual byte
* @var uint8_t
*/
uint8_t _byte;
public:
/**
* Constructor
* @param first the first bool to set
* @param second the second bool to set
* @param third the third bool to set
* @param fourth the fourth bool to set
* @param fifth the fifth bool to set
* @param sixth the sixth bool to set
* @param seventh the seventh bool to set
* @param eigth the eigth bool to set
*/
BooleanSet(bool first = false, bool second = false, bool third = false, bool fourth = false, bool fifth = false, bool sixth = false, bool seventh = false, bool eigth = false)
{
_byte = 0;
set(0, first);
set(1, second);
set(2, third);
set(3, fourth);
set(4, fifth);
set(5, sixth);
set(6, seventh);
set(7, eigth);
}
/**
* Constructor based on incoming data
* @param frame
*/
BooleanSet(ReceivedFrame &frame)
{
_byte = frame.nextUint8();
}
/**
* Copy constructor
* @param that
*/
BooleanSet(const BooleanSet &that)
{
_byte = that._byte;
}
/**
* Destructor
*/
virtual ~BooleanSet() {}
/**
* Extending from field forces us to implement a clone function.
* @return shared_ptr
*/
virtual Field *clone() const override
{
return new BooleanSet(*this);
}
/**
* Get one of the booleans
* @param index from 0 to 7, where 0 is rightmost bit
* @return bool
*/
bool get(uint32_t index) const
{
// bigger than seven is not an option
if (index > 7) return false;
// magic bit manipulation...
return (bool)((1 << index) & _byte);
}
/**
* Set a boolean in the set
* @param index
* @param value
*/
void set(uint32_t index, bool value)
{
// index must be valid
if (index > 7) return;
// are we setting or unsetting
if (value)
{
// magic bit manipulation...
_byte |= (1 << index);
}
else
{
// magic bit manipulation...
_byte &= ~(1 << index);
}
}
/**
* Fill the buffer
* @param buffer
*/
virtual void fill(OutBuffer& buffer) const override
{
buffer.add(_byte);
}
/**
* Get the byte value
* @return value
*/
uint8_t value()
{
return _byte;
}
/**
* Type ID
* @return char
*/
virtual char typeID() const override
{
return 't';
}
/**
* Get the size this field will take when
* encoded in the AMQP wire-frame format
*/
virtual size_t size() const override
{
// booleanset takes up a single byte.
return 1;
}
};
/**
* End of namespace
*/
}