Skip to content
Snippets Groups Projects
Commit 7645d35c authored by Rainer Kartmann's avatar Rainer Kartmann
Browse files

Add overrides to Reader::subscribe(). Use a list of callbacks instead one per id

parent c6ce2373
No related branches found
No related tags found
1 merge request!157armem/dev => master
......@@ -86,7 +86,7 @@ namespace armarx::armem::client
void
Reader::updated(const std::vector<MemoryID>& updatedSnapshotIDs) const
{
for (const auto& [subscription, callback] : callbacks)
for (const auto& [subscription, callbacks] : this->callbacks)
{
std::vector<MemoryID> matchingSnapshotIDs;
......@@ -100,7 +100,10 @@ namespace armarx::armem::client
if (not matchingSnapshotIDs.empty())
{
callback(subscription, matchingSnapshotIDs);
for (auto& callback : callbacks)
{
callback(subscription, matchingSnapshotIDs);
}
}
}
}
......@@ -124,7 +127,15 @@ namespace armarx::armem::client
void
Reader::subscribe(const MemoryID& id, callback callback)
{
callbacks[id] = callback;
callbacks[id].push_back(callback);
}
void Reader::subscribe(const MemoryID& subscriptionID, callback_updated_only callback)
{
subscribe(subscriptionID, [callback](const MemoryID&, const std::vector<MemoryID>& updatedSnapshotIDs)
{
callback(updatedSnapshotIDs);
});
}
......
......@@ -27,8 +27,13 @@ namespace armarx::armem::client
{
using callback = std::function<void(const MemoryID& subscriptionID, const std::vector<MemoryID>& updatedSnapshotIDs)>;
using callback_updated_only = std::function<void(const std::vector<MemoryID>& updatedSnapshotIDs)>;
template <class CalleeT>
using member_callback = void(CalleeT::*)(const MemoryID& subscriptionID, const std::vector<MemoryID>& updatedSnapshotIDs);
template <class CalleeT>
using member_callback_updated_only = void(CalleeT::*)(const std::vector<MemoryID>& updatedSnapshotIDs);
public:
......@@ -54,6 +59,7 @@ namespace armarx::armem::client
data::StoreResult readAndStore(const data::StoreInput& input) const;
void subscribe(const MemoryID& subscriptionID, callback callback);
void subscribe(const MemoryID& subscriptionID, callback_updated_only callback);
/**
* Subscribe with a class member function:
* @code
......@@ -69,6 +75,15 @@ namespace armarx::armem::client
};
subscribe(subscriptionID, cb);
}
template <class CalleeT>
void subscribe(const MemoryID& subscriptionID, CalleeT* callee, member_callback_updated_only<CalleeT> callback)
{
auto cb = [callee, callback](const MemoryID&, const std::vector<MemoryID>& updatedSnapshotIDs)
{
(callee->*callback)(updatedSnapshotIDs);
};
subscribe(subscriptionID, cb);
}
/// Function handling updates from the MemoryListener ice topic.
void updated(const std::vector<MemoryID>& updatedIDs) const;
......@@ -84,7 +99,7 @@ namespace armarx::armem::client
private:
std::unordered_map<MemoryID, callback> callbacks;
std::unordered_map<MemoryID, std::vector<callback>> callbacks;
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment