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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
/**
* \file
* \brief SignalSet class header
*
* \author Copyright (C) 2015 Kamil Szczygiel http://www.distortec.com http://www.freddiechopin.info
*
* \par License
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
* distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef INCLUDE_DISTORTOS_SIGNALSET_HPP_
#define INCLUDE_DISTORTOS_SIGNALSET_HPP_
#include <bitset>
namespace distortos
{
/**
* \brief SignalSet class is used as a set of signals.
*
* Similar to POSIX sigset_t - http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html
*
* \ingroup signals
*/
class SignalSet
{
public:
/// type of internal bitset for 32 signals
using Bitset = std::bitset<32>;
/// tag struct to construct empty SignalSet
struct Empty
{
};
/// tag struct to construct full SignalSet
struct Full
{
};
/// tag object to construct empty SignalSet
constexpr static Empty empty = {};
/// tag object to construct full SignalSet
constexpr static Full full = {};
/**
* \brief SignalSet's constructor
*
* \param [in] bitmask is the bit mask used to initialize internal bitset
*/
constexpr explicit SignalSet(uint32_t bitmask) :
bitset_{bitmask}
{
}
/**
* \brief SignalSet's constructor
*
* \param [in] bitset is a reference to Bitset from which internal bitset is copy-constructed
*/
constexpr explicit SignalSet(const Bitset& bitset) :
bitset_{bitset}
{
}
/**
* \brief SignalSet's constructor
*
* Constructs empty SignalSet.
*
* Similar to sigemptyset() - http://pubs.opengroup.org/onlinepubs/9699919799/functions/sigemptyset.html#
*/
constexpr explicit SignalSet(const Empty&) :
SignalSet{uint32_t{}}
{
}
/**
* \brief SignalSet's constructor
*
* Constructs full SignalSet.
*
* Similar to sigfillset() - http://pubs.opengroup.org/onlinepubs/9699919799/functions/sigfillset.html#
*/
constexpr explicit SignalSet(const Full&) :
SignalSet{~uint32_t{}}
{
}
/**
* \brief Sets single bit.
*
* Similar to sigaddset() - http://pubs.opengroup.org/onlinepubs/9699919799/functions/sigaddset.html#
*
* \param [in] signalNumber is the bit position that will be set, [0; 31]
*
* \return 0 on success, error code otherwise:
* - EINVAL - \a signalNumber value is invalid;
*/
int add(const uint8_t signalNumber)
{
return set(signalNumber, true);
}
/**
* \return copy of internal bitset
*/
Bitset getBitset() const
{
return bitset_;
}
/**
* \brief Clears single bit.
*
* Similar to sigdelset() - http://pubs.opengroup.org/onlinepubs/9699919799/functions/sigdelset.html#
*
* \param [in] signalNumber is the bit position that will be cleared, [0; 31]
*
* \return 0 on success, error code otherwise:
* - EINVAL - \a signalNumber value is invalid;
*/
int remove(const uint8_t signalNumber)
{
return set(signalNumber, false);
}
/**
* \brief Tests whether the bit is set.
*
* Similar to sigismember() - http://pubs.opengroup.org/onlinepubs/9699919799/functions/sigismember.html#
*
* \param [in] signalNumber is the bit position that will be tested, [0; 31]
*
* \return pair with return code (0 on success, error code otherwise) and value of selected bit; error codes:
* - EINVAL - \a signalNumber value is invalid;
*/
std::pair<int, bool> test(uint8_t signalNumber) const;
private:
/**
* \brief Sets value of single bit.
*
* \param [in] signalNumber is the bit position that will be modified, [0; 31]
* \param [in] value is the new value for selected bit
*
* \return 0 on success, error code otherwise:
* - EINVAL - \a signalNumber value is invalid;
*/
int set(uint8_t signalNumber, bool value);
/// internal bitset for 32 signals
Bitset bitset_;
};
} // namespace distortos
#endif // INCLUDE_DISTORTOS_SIGNALSET_HPP_
|