• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

igor-krechetov / hsmcpp / 5076885588

pending completion
5076885588

push

github

igor-krechetov
[1.0.0][r] first release

63 of 63 new or added lines in 9 files covered. (100.0%)

2318 of 2522 relevant lines covered (91.91%)

1610.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.79
/src/HsmImplTypes.cpp
1
// Copyright (C) 2023 Igor Krechetov
2
// Distributed under MIT license. See file LICENSE for details
3

4
#include "HsmImplTypes.hpp"
5

6
#include "hsmcpp/logging.hpp"
7

8
namespace hsmcpp {
9

10
// ============================================================================
11
// StateCallbacks
12
// ============================================================================
13
StateCallbacks::StateCallbacks(HsmStateChangedCallback_t&& cbStateChanged,
4,000✔
14
                               HsmStateEnterCallback_t&& cbEntering,
15
                               HsmStateExitCallback_t&& cbExiting)
4,000✔
16
    : onStateChanged(std::move(cbStateChanged))
4,000✔
17
    , onEntering(std::move(cbEntering))
4,000✔
18
    , onExiting(std::move(cbExiting)) {}
4,000✔
19

20
StateCallbacks::StateCallbacks(StateCallbacks&& src) noexcept {
×
21
    *this = std::move(src);
×
22
}
×
23

24
StateCallbacks& StateCallbacks::operator=(StateCallbacks&& src) noexcept {
4,000✔
25
    if (this != &src) {
4,000✔
26
        onStateChanged = std::move(src.onStateChanged);
4,000✔
27
        onEntering = std::move(src.onEntering);
4,000✔
28
        onExiting = std::move(src.onExiting);
4,000✔
29
    }
30

31
    return *this;
4,000✔
32
}
33

34
// ============================================================================
35
// TransitionInfo
36
// ============================================================================
37
TransitionInfo::TransitionInfo(const StateID_t from,
1,070✔
38
                               const StateID_t to,
39
                               const TransitionType type,
40
                               HsmTransitionCallback_t cbTransition,
41
                               HsmTransitionConditionCallback_t cbCondition)
1,070✔
42
    : fromState(from)
1,070✔
43
    , destinationState(to)
1,070✔
44
    , transitionType(type)
1,070✔
45
    , onTransition(std::move(cbTransition))
1,070✔
46
    , checkCondition(std::move(cbCondition)) {}
1,070✔
47

48
TransitionInfo::TransitionInfo(const StateID_t from,
3,070✔
49
                               const StateID_t to,
50
                               const TransitionType type,
51
                               HsmTransitionCallback_t cbTransition,
52
                               HsmTransitionConditionCallback_t cbCondition,
53
                               const bool conditionValue)
3,070✔
54
    : fromState(from)
3,070✔
55
    , destinationState(to)
3,070✔
56
    , transitionType(type)
3,070✔
57
    , onTransition(std::move(cbTransition))
3,070✔
58
    , checkCondition(std::move(cbCondition))
3,070✔
59
    , expectedConditionValue(conditionValue) {}
3,070✔
60

61
// ============================================================================
62
// HistoryInfo
63
// ============================================================================
64
HistoryInfo::HistoryInfo(const HistoryType newType,
100✔
65
                         const StateID_t newDefaultTarget,
66
                         HsmTransitionCallback_t newTransitionCallback)
100✔
67
    : type(newType)
100✔
68
    , defaultTarget(newDefaultTarget)
100✔
69
    , defaultTargetTransitionCallback(std::move(newTransitionCallback)) {}
100✔
70

71
HistoryInfo::HistoryInfo(HistoryInfo&& src) noexcept {
×
72
    *this = std::move(src);
×
73
}
×
74

75
HistoryInfo& HistoryInfo::operator=(HistoryInfo&& src) noexcept {
100✔
76
    if (this != &src) {
100✔
77
        type = src.type;
100✔
78
        defaultTarget = src.defaultTarget;
100✔
79
        defaultTargetTransitionCallback = std::move(src.defaultTargetTransitionCallback);
100✔
80
        previousActiveStates = std::move(src.previousActiveStates);
100✔
81

82
        src.type = HistoryType::SHALLOW;
100✔
83
        src.defaultTarget = INVALID_HSM_STATE_ID;
100✔
84
    }
85

86
    return *this;
100✔
87
}
88

89
// ============================================================================
90
// PendingEventInfo
91
// ============================================================================
92
constexpr const char* HSM_TRACE_CLASS = "PendingEventInfo";
93

94
PendingEventInfo::PendingEventInfo(PendingEventInfo&& src) noexcept {
240✔
95
    *this = std::move(src);
240✔
96
}
240✔
97

98
PendingEventInfo::~PendingEventInfo() {
10,077✔
99
    if (true == cvLock.unique()) {
11,247✔
100
        HSM_TRACE_CALL_DEBUG_ARGS("event=<%d> was deleted. releasing lock", SC2INT(id));
1,170✔
101
        unlock(HsmEventStatus::DONE_FAILED);
1,170✔
102
        cvLock.reset();
1,170✔
103
        syncProcessed.reset();
1,170✔
104
    }
105
}
18,860✔
106

107
PendingEventInfo& PendingEventInfo::operator=(PendingEventInfo&& src) noexcept {
3,250✔
108
    if (this != &src) {
3,250✔
109
        transitionType = src.transitionType;
3,250✔
110
        id = src.id;
3,250✔
111
        args = std::move(src.args);
3,250✔
112
        cvLock = std::move(src.cvLock);
3,250✔
113
        syncProcessed = std::move(src.syncProcessed);
3,250✔
114
        transitionStatus = std::move(src.transitionStatus);
3,250✔
115
        forcedTransitionsInfo = std::move(src.forcedTransitionsInfo);
3,250✔
116
        ignoreEntryPoints = src.ignoreEntryPoints;
3,250✔
117

118
        src.id = INVALID_HSM_EVENT_ID;
3,250✔
119
    }
120

121
    return *this;
3,250✔
122
}
123

124
void PendingEventInfo::initLock() {
1,170✔
125
    if (!cvLock) {
1,170✔
126
        cvLock = std::make_shared<Mutex>();
1,170✔
127
        syncProcessed = std::make_shared<ConditionVariable>();
1,170✔
128
        transitionStatus = std::make_shared<HsmEventStatus>();
1,170✔
129
        *transitionStatus = HsmEventStatus::PENDING;
1,170✔
130
    }
131
}
1,170✔
132

133
void PendingEventInfo::releaseLock() {
×
134
    if (true == isSync()) {
×
135
        HSM_TRACE_CALL_DEBUG_ARGS("releaseLock");
×
136
        unlock(HsmEventStatus::DONE_FAILED);
×
137
        cvLock.reset();
×
138
        syncProcessed.reset();
×
139
    }
140
}
×
141

142
bool PendingEventInfo::isSync() const {
5,350✔
143
    return (nullptr != cvLock);
5,350✔
144
}
145

146
void PendingEventInfo::wait(const int timeoutMs) {
1,170✔
147
    if (true == isSync()) {
1,170✔
148
        // NOTE: lock is needed only because we have to use cond variable
149
        UniqueLock lck(*cvLock);
1,170✔
150

151
        HSM_TRACE_CALL_DEBUG_ARGS("trying to wait... (current status=%d, %p)",
152
                                  SC2INT(*transitionStatus),
153
                                  transitionStatus.get());
1,170✔
154
        if (timeoutMs > 0) {
1,170✔
155
            // NOTE: false-positive. "return" statement belongs to lambda function, not parent function
156
            // cppcheck-suppress [misra-c2012-15.5, misra-c2012-17.7]
157
            syncProcessed->wait_for(lck, timeoutMs, [=]() { return (HsmEventStatus::PENDING != *transitionStatus); });
2,340✔
158
        } else {
159
            // NOTE: false-positive. "return" statement belongs to lambda function, not parent function
160
            // cppcheck-suppress [misra-c2012-15.5, misra-c2012-17.7]
161
            syncProcessed->wait(lck, [=]() { return (HsmEventStatus::PENDING != *transitionStatus); });
×
162
        }
163

164
        HSM_TRACE_DEBUG("unlocked! transitionStatus=%d", SC2INT(*transitionStatus));
1,170✔
165
    }
1,170✔
166
}
1,170✔
167

168
void PendingEventInfo::unlock(const HsmEventStatus status) {
4,180✔
169
    HSM_TRACE_CALL_DEBUG_ARGS("try to unlock with status=%d", SC2INT(status));
4,180✔
170

171
    if (true == isSync()) {
4,180✔
172
        HSM_TRACE_DEBUG("SYNC object (%p)", transitionStatus.get());
3,020✔
173
        *transitionStatus = status;
3,020✔
174

175
        if (status != HsmEventStatus::PENDING) {
3,020✔
176
            syncProcessed->notify();
2,340✔
177
        }
178
    } else {
179
        HSM_TRACE_DEBUG("ASYNC object");
4,180✔
180
    }
181
}
4,180✔
182

183
const VariantVector_t& PendingEventInfo::getArgs() const {
16,030✔
184
    static VariantVector_t empty;
16,030✔
185
    return (args ? *args : empty);
16,030✔
186
}
187

188
}  // namespace hsmcpp
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc