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