-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcmsDummyActiveObject.hpp
106 lines (91 loc) · 3.23 KB
/
cmsDummyActiveObject.hpp
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
/// @brief A dummy active object, useful for testing interactions
/// in a unit test. Same basic idea as QActiveDummy in
/// QUTest, adapted for CppUTest.
/// @ingroup
/// @cond
///***************************************************************************
///
/// Copyright (C) 2022 Matthew Eshleman. All rights reserved.
///
/// This program is open source software: you can redistribute it and/or
/// modify it under the terms of the GNU General Public License as published
/// by the Free Software Foundation, either version 3 of the License, or
/// (at your option) any later version.
///
/// Alternatively, upon written permission from Matthew Eshleman, this program
/// may be distributed and modified under the terms of a Commercial
/// License. For further details, see the Contact Information below.
///
/// Contact Information:
/// Matthew Eshleman
/// https://covemountainsoftware.com
///***************************************************************************
/// @endcond
#ifndef CMS_DUMMY_ACTIVE_OBJECT_HPP
#define CMS_DUMMY_ACTIVE_OBJECT_HPP
#include "qpcpp.hpp"
#include <array>
#include <functional>
#include <cstddef>
namespace cms {
/// The Dummy Active Object may be used
/// when an active object (AO) under test is
/// interacting with another AO during a test.
template <size_t InternalEventCount>
class DummyActiveObject : public QP::QActive {
public:
using PostedEventHandler = std::function<void(QP::QEvt const*)>;
explicit DummyActiveObject() :
QP::QActive(Q_STATE_CAST(initial)), m_eventHandler(nullptr),
m_incomingEvents()
{
m_incomingEvents.fill(nullptr);
}
DummyActiveObject(const DummyActiveObject&) = delete;
DummyActiveObject& operator=(const DummyActiveObject&) = delete;
DummyActiveObject(DummyActiveObject&&) = delete;
DummyActiveObject& operator=(DummyActiveObject&&) = delete;
void SetPostedEventHandler(const PostedEventHandler& handler)
{
m_eventHandler = handler;
}
void dummyStart(uint_fast8_t priority = 1)
{
start(priority, m_incomingEvents.data(), m_incomingEvents.size(),
nullptr, 0);
}
protected:
static QP::QState initial(DummyActiveObject* const me,
QP::QEvt const* const)
{
return Q_TRAN(&running);
}
static QP::QState running(DummyActiveObject* const me,
QP::QEvt const* const e)
{
QP::QState rtn;
switch (e->sig) {
case Q_INIT_SIG:
rtn = Q_SUPER(&top);
break;
case Q_ENTRY_SIG: // purposeful fall through
case Q_EXIT_SIG:
rtn = Q_HANDLED();
break;
default:
if ((me->m_eventHandler != nullptr) && (e->sig != 0)) {
me->m_eventHandler(e);
}
rtn = Q_SUPER(&top);
break;
}
return rtn;
}
private:
PostedEventHandler m_eventHandler;
std::array<QP::QEvt const*, InternalEventCount> m_incomingEvents;
};
using DefaultDummyActiveObject = DummyActiveObject<50>;
} // namespace cms
#endif // CMS_DUMMY_ACTIVE_OBJECT_HPP