Skip to content
Snippets Groups Projects

(Scoped)SubscriptionHandle to unsubscribe from memory updates

Merged Patrick Dormanns requested to merge MemoryListener-unsubscribe into master
5 files
+ 143
115
Compare changes
  • Side-by-side
  • Inline
Files
5
@@ -13,71 +13,6 @@
namespace armarx::armem::client::util
{
MemoryListener::SubscriptionHandle::SubscriptionHandle(MemoryListener* memoryListener,
const MemoryID& memoryID,
long id) :
valid{true}, memoryListener{memoryListener}, memoryID(memoryID), id{id}
{
}
MemoryListener::SubscriptionHandle::SubscriptionHandle() : valid{false}
{
}
MemoryListener::SubscriptionHandle::SubscriptionHandle(SubscriptionHandle&& other) :
valid{other.valid},
memoryListener{other.memoryListener},
memoryID(std::move(other.memoryID)),
id{other.id}
{
other.valid = false;
}
MemoryListener::SubscriptionHandle&
MemoryListener::SubscriptionHandle::operator=(SubscriptionHandle other)
{
swap(*this, other);
return *this;
}
void
swap(MemoryListener::SubscriptionHandle& first, MemoryListener::SubscriptionHandle& second)
{
std::swap(first.valid, second.valid);
std::swap(first.memoryListener, second.memoryListener);
std::swap(first.memoryID, second.memoryID);
std::swap(first.id, second.id);
}
void
MemoryListener::SubscriptionHandle::release()
{
memoryListener->unsubscribe(*this);
}
MemoryListener::ScopedSubscriptionHandle::ScopedSubscriptionHandle()
{
}
MemoryListener::ScopedSubscriptionHandle::ScopedSubscriptionHandle(
SubscriptionHandle&& handle) :
handle(std::move(handle))
{
}
MemoryListener::ScopedSubscriptionHandle&
MemoryListener::ScopedSubscriptionHandle::operator=(MemoryListener::SubscriptionHandle handle)
{
std::swap(this->handle, handle);
return *this;
}
MemoryListener::ScopedSubscriptionHandle::~ScopedSubscriptionHandle()
{
handle.release();
}
std::string
MemoryListener::MakeMemoryTopicName(const MemoryID& memoryID)
{
@@ -177,7 +112,7 @@ namespace armarx::armem::client::util
}
}
MemoryListener::SubscriptionHandle
SubscriptionHandle
MemoryListener::subscribe(const MemoryID& memoryID, Callback callback)
{
ARMARX_CHECK_NOT_EMPTY(memoryID.memoryName)
@@ -188,7 +123,7 @@ namespace armarx::armem::client::util
component->usingTopic(MakeMemoryTopicName(memoryID));
}
auto id = next_id++;
auto id = nextId++;
callbacks[memoryID].push_back({id, callback});
memoryRefCount[memoryID.memoryName]++;
@@ -196,7 +131,7 @@ namespace armarx::armem::client::util
return SubscriptionHandle(this, memoryID, id);
}
MemoryListener::SubscriptionHandle
SubscriptionHandle
MemoryListener::subscribe(const MemoryID& subscriptionID, CallbackUpdatedOnly callback)
{
return subscribe(
@@ -219,11 +154,6 @@ namespace armarx::armem::client::util
callbacks[handle.memoryID].end(),
[&handle](ManagedCallback& mCb) { return mCb.id == handle.id; });
if (it->id != handle.id)
{
return;
}
std::iter_swap(it, callbacks[handle.memoryID].end() - 1);
callbacks[handle.memoryID].pop_back();
Loading