SMACC
Loading...
Searching...
No Matches
smacc_asynchronous_client_behavior.h
Go to the documentation of this file.
1/*****************************************************************************************************************
2 * ReelRobotix Inc. - Software License Agreement Copyright (c) 2018
3 * Authors: Pablo Inigo Blasco, Brett Aldrich
4 *
5 ******************************************************************************************************************/
6
7#pragma once
8#include <smacc/smacc_client_behavior_base.h>
10#include <thread>
11#include <condition_variable>
12#include <mutex>
13#include <future>
14
15namespace smacc
16{
17 template <typename AsyncCB, typename Orthogonal>
18 struct EvCbFinished : sc::event<EvCbFinished<AsyncCB, Orthogonal>>
19 {
20 };
21
22 template <typename AsyncCB, typename Orthogonal>
23 struct EvCbSuccess : sc::event<EvCbSuccess<AsyncCB, Orthogonal>>
24 {
25 };
26
27 template <typename AsyncCB, typename Orthogonal>
28 struct EvCbFailure : sc::event<EvCbFailure<AsyncCB, Orthogonal>>
29 {
30 };
31
32 // Asnchronous client behaviors are used when the onEntry or onExit function execution is slow
33 // CONCEPT: this funcionality is related with the orthogonality of SmaccState machines.
34 // No behavior should block the creation of other behaviors, all of them conceptually start in parallel.
35 // Alternative for long duration behaviors: using default-synchromous SmaccClientBehaviors with the update method
36 // ASYNCHRONOUS STATE MACHINES DESIGN NOTES: Asynchronous behaviors can safely post events and use its local methods,
37 // but the interaction with other components or elements of
38 // the state machine is not by-default thread safe and must be manually implemented. For example, if some element of the architecture
39 // (components, states, clients) need to access to this behavior client information it is needed to implement a mutex for the internal
40 // state of this behavior. Other example: if this behavior access to some component located in other thread, it is also may be needed
41 // to some mutex for that component
43 {
44 public:
45 template <typename TOrthogonal, typename TSourceObject>
47
49
50 template <typename TCallback, typename T>
51 boost::signals2::connection onSuccess(TCallback callback, T *object);
52
53 template <typename TCallback, typename T>
54 boost::signals2::connection onFinished(TCallback callback, T *object);
55
56 template <typename TCallback, typename T>
57 boost::signals2::connection onFailure(TCallback callback, T *object);
58
59 protected:
60 virtual void executeOnEntry() override;
61 virtual void executeOnExit() override;
62
63 void postSuccessEvent();
64 void postFailureEvent();
65
66 virtual void dispose() override;
67
68 private:
69 std::future<int> onEntryThread_;
70 std::future<int> onExitThread_;
71
72 std::function<void()> postFinishEventFn_;
73 std::function<void()> postSuccessEventFn_;
74 std::function<void()> postFailureEventFn_;
75
79 };
80} // namespace smacc
81
boost::signals2::connection onFailure(TCallback callback, T *object)
boost::signals2::connection onSuccess(TCallback callback, T *object)
boost::signals2::connection onFinished(TCallback callback, T *object)