MemoryListener.h
Go to the documentation of this file.
1#pragma once
2
3// STD/STL
4#include <functional>
5#include <mutex>
6#include <unordered_map>
7#include <vector>
8
9// RobotAPI
10#include <RobotAPI/interface/armem/client/MemoryListenerInterface.h>
12
13#include "SubscriptionHandle.h"
14
15namespace armarx
16{
17 class ManagedIceObject;
18}
19
21{
22 /**
23 * @brief Handles update signals from the memory system and distributes it
24 * to its subsribers.
25 */
27 {
28
29 public:
30 using Callback = std::function<void(const MemoryID& subscriptionID,
31 const std::vector<MemoryID>& updatedSnapshotIDs)>;
33 std::function<void(const std::vector<MemoryID>& updatedSnapshotIDs)>;
34
35 template <class CalleeT>
36 using MemberCallback = void (CalleeT::*)(const MemoryID& subscriptionID,
37 const std::vector<MemoryID>& updatedSnapshotIDs);
38 template <class CalleeT>
40 void (CalleeT::*)(const std::vector<MemoryID>& updatedSnapshotIDs);
41
42 static std::string MakeMemoryTopicName(const MemoryID& memoryID);
43
44
45 public:
46 MemoryListener(ManagedIceObject* component = nullptr);
47
48 void setComponent(ManagedIceObject* component);
49
52
53 /**
54 * Subscribe with a class member function:
55 * @code
56 * listener.subscribe(entityID, this, &This::myCallback);
57 * @endcode
58 */
59 template <class CalleeT>
61 subscribe(const MemoryID& subscriptionID, CalleeT* callee, MemberCallback<CalleeT> callback)
62 {
63 auto cb = [callee, callback](const MemoryID& subscriptionID,
64 const std::vector<MemoryID>& updatedSnapshotIDs)
65 { (callee->*callback)(subscriptionID, updatedSnapshotIDs); };
66 return subscribe(subscriptionID, cb);
67 }
68
69 template <class CalleeT>
71 subscribe(const MemoryID& subscriptionID,
72 CalleeT* callee,
74 {
75 auto cb =
76 [callee, callback](const MemoryID&, const std::vector<MemoryID>& updatedSnapshotIDs)
77 {
78 if (callee)
79 {
80 (callee->*callback)(updatedSnapshotIDs);
81 }
82 };
83 return subscribe(subscriptionID, cb);
84 }
85
86 void unsubscribe(SubscriptionHandle& subscriptionHandle);
87
88 /// Function handling updates from the MemoryListener ice topic.
89 void updated(const std::vector<MemoryID>& updatedIDs) const;
90 void updated(const std::vector<data::MemoryID>& updatedIDs) const;
91
92
93 protected:
94 long nextId = 0;
95
97 {
98 long id = 0;
100 };
101
102 mutable std::mutex subscribeMutex;
103 std::unordered_map<MemoryID, std::vector<ManagedCallback>> callbacks;
104
105 /// memoryName -> #callbacks needing memory topic
106 std::unordered_map<std::string, int> memoryRefCount;
107
108 private:
109 armarx::ManagedIceObject* component;
110 };
111
112} // namespace armarx::armem::client::util
The ManagedIceObject is the base class for all ArmarX objects.
void(CalleeT::*)(const MemoryID &subscriptionID, const std::vector< MemoryID > &updatedSnapshotIDs) MemberCallback
void unsubscribe(SubscriptionHandle &subscriptionHandle)
void updated(const std::vector< MemoryID > &updatedIDs) const
Function handling updates from the MemoryListener ice topic.
std::unordered_map< MemoryID, std::vector< ManagedCallback > > callbacks
SubscriptionHandle subscribe(const MemoryID &subscriptionID, CalleeT *callee, MemberCallbackUpdatedOnly< CalleeT > callback)
SubscriptionHandle subscribe(const MemoryID &subscriptionID, Callback Callback)
MemoryListener(ManagedIceObject *component=nullptr)
std::unordered_map< std::string, int > memoryRefCount
memoryName -> callbacks needing memory topic
SubscriptionHandle subscribe(const MemoryID &subscriptionID, CalleeT *callee, MemberCallback< CalleeT > callback)
Subscribe with a class member function:
std::function< void(const MemoryID &subscriptionID, const std::vector< MemoryID > &updatedSnapshotIDs)> Callback
void(CalleeT::*)(const std::vector< MemoryID > &updatedSnapshotIDs) MemberCallbackUpdatedOnly
std::function< void(const std::vector< MemoryID > &updatedSnapshotIDs)> CallbackUpdatedOnly
static std::string MakeMemoryTopicName(const MemoryID &memoryID)
void setComponent(ManagedIceObject *component)
This file offers overloads of toIce() and fromIce() functions for STL container types.