-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPadsAccess.h
executable file
·50 lines (41 loc) · 971 Bytes
/
PadsAccess.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
#pragma once
#include "Pads.h"
#include <stdexcept>
namespace ps
{
class PiSample;
/// Ensure only one component displays data on the pads at a given time.
/// Only PiSample can grant access to the pads to other components.
/// Access can be given and taken at any time, do not store states in the pads.
class PadsAccess
{
public:
PadsAccess(Pads& pads) : _pads(pads)
{ }
protected:
bool isAccessing() const { return _pads._access == this; }
Pads& pads();
private:
friend class PiSample;
void receiveAccess();
virtual void onAccess() {};
Pads& _pads;
};
inline void PadsAccess::receiveAccess()
{
if (not isAccessing()) {
_pads._access = this;
onAccess();
}
else {
_pads._access = this;
}
}
inline Pads& PadsAccess::pads()
{
if (not isAccessing()) {
throw std::logic_error("Trying to get pads without being given access");
}
return _pads;
}
}