Skip to content
Snippets Groups Projects
Commit c4210d4f authored by Patrick Dormanns's avatar Patrick Dormanns
Browse files

implemented feedback

parent 9aea719d
No related branches found
No related tags found
1 merge request!391(Scoped)SubscriptionHandle to unsubscribe from memory updates
Pipeline #15772 failed
This commit is part of merge request !391. Comments created here will be created in the context of that merge request.
......@@ -63,6 +63,7 @@ set(LIB_FILES
client/plugins/PluginUser.cpp
client/plugins/Plugin.cpp
client/util/SubscriptionHandle.cpp
client/util/MemoryListener.cpp
client/util/SimpleReaderBase.cpp
client/util/SimpleWriterBase.cpp
......@@ -154,6 +155,7 @@ set(LIB_HEADERS
client/query/detail/NameSelectorOps.h
client/query/detail/SelectorOps.h
client/util/SubscriptionHandle.h
client/util/MemoryListener.h
client/util/SimpleReaderBase.h
client/util/SimpleWriterBase.h
......
......@@ -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();
......
......@@ -9,6 +9,8 @@
#include <RobotAPI/interface/armem/client/MemoryListenerInterface.h>
#include <RobotAPI/libraries/armem/core/MemoryID.h>
#include "SubscriptionHandle.h"
namespace armarx
{
class ManagedIceObject;
......@@ -16,7 +18,6 @@ namespace armarx
namespace armarx::armem::client::util
{
/**
* @brief Handles update signals from the memory system and distributes it
* to its subsribers.
......@@ -40,44 +41,6 @@ namespace armarx::armem::client::util
static std::string MakeMemoryTopicName(const MemoryID& memoryID);
public:
class SubscriptionHandle
{
friend class MemoryListener;
public:
SubscriptionHandle();
SubscriptionHandle(SubscriptionHandle&& other);
SubscriptionHandle& operator=(SubscriptionHandle other);
friend void swap(SubscriptionHandle& first, SubscriptionHandle& second);
void release();
private:
SubscriptionHandle(MemoryListener* memoryListener, const MemoryID& memoryID, long id);
private:
bool valid = false;
MemoryListener* memoryListener = nullptr;
MemoryID memoryID;
long id = 0;
};
class ScopedSubscriptionHandle
{
public:
ScopedSubscriptionHandle();
ScopedSubscriptionHandle(SubscriptionHandle&& handle);
ScopedSubscriptionHandle& operator=(SubscriptionHandle handle);
~ScopedSubscriptionHandle();
private:
SubscriptionHandle handle;
};
public:
MemoryListener(ManagedIceObject* component = nullptr);
......@@ -93,7 +56,7 @@ namespace armarx::armem::client::util
* @endcode
*/
template <class CalleeT>
MemoryListener::SubscriptionHandle
SubscriptionHandle
subscribe(const MemoryID& subscriptionID, CalleeT* callee, MemberCallback<CalleeT> callback)
{
auto cb = [callee, callback](const MemoryID& subscriptionID,
......@@ -103,7 +66,7 @@ namespace armarx::armem::client::util
}
template <class CalleeT>
MemoryListener::SubscriptionHandle
SubscriptionHandle
subscribe(const MemoryID& subscriptionID,
CalleeT* callee,
MemberCallbackUpdatedOnly<CalleeT> callback)
......@@ -127,7 +90,7 @@ namespace armarx::armem::client::util
protected:
long next_id = 0;
long nextId = 0;
struct ManagedCallback
{
......
#include "SubscriptionHandle.h"
#include "MemoryListener.h"
namespace armarx::armem::client::util
{
SubscriptionHandle::SubscriptionHandle(MemoryListener* memoryListener,
const MemoryID& memoryID,
long id) :
valid{true}, memoryListener{memoryListener}, memoryID(memoryID), id{id}
{
}
SubscriptionHandle::SubscriptionHandle() : valid{false}
{
}
SubscriptionHandle::SubscriptionHandle(SubscriptionHandle&& other) :
valid{other.valid},
memoryListener{other.memoryListener},
memoryID(std::move(other.memoryID)),
id{other.id}
{
other.valid = false;
}
SubscriptionHandle&
SubscriptionHandle::operator=(SubscriptionHandle other)
{
swap(*this, other);
return *this;
}
void
SubscriptionHandle::release()
{
memoryListener->unsubscribe(*this);
}
ScopedSubscriptionHandle::ScopedSubscriptionHandle()
{
}
ScopedSubscriptionHandle::ScopedSubscriptionHandle(SubscriptionHandle&& handle) :
handle(std::move(handle))
{
}
ScopedSubscriptionHandle&
ScopedSubscriptionHandle::operator=(SubscriptionHandle handle)
{
std::swap(this->handle, handle);
return *this;
}
ScopedSubscriptionHandle::~ScopedSubscriptionHandle()
{
handle.release();
}
} // namespace armarx::armem::client::util
namespace armarx::armem::client
{
void
util::swap(util::SubscriptionHandle& first, util::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);
}
} // namespace armarx::armem::client
#pragma once
#include <RobotAPI/libraries/armem/core/MemoryID.h>
namespace armarx::armem::client::util
{
class MemoryListener;
class SubscriptionHandle
{
friend class MemoryListener;
public:
SubscriptionHandle();
SubscriptionHandle(SubscriptionHandle&& other);
/**
* @brief Assignment operator.
*
* @note Intentional call by value, since this leverages the move constructor. See
* https://stackoverflow.com/a/11540204 (section "Move assignment operators").
*/
SubscriptionHandle& operator=(SubscriptionHandle other);
friend void swap(SubscriptionHandle& first, SubscriptionHandle& second);
void release();
private:
SubscriptionHandle(MemoryListener* memoryListener, const MemoryID& memoryID, long id);
private:
bool valid = false;
MemoryListener* memoryListener = nullptr;
MemoryID memoryID;
long id = 0;
};
class ScopedSubscriptionHandle
{
public:
ScopedSubscriptionHandle();
ScopedSubscriptionHandle(SubscriptionHandle&& handle);
/**
* @brief Assignment operator.
*
* @note Intentional call by value, since this leverages the move constructor. See
* https://stackoverflow.com/a/11540204 (section "Move assignment operators").
*/
ScopedSubscriptionHandle& operator=(SubscriptionHandle handle);
~ScopedSubscriptionHandle();
private:
SubscriptionHandle handle;
};
} // namespace armarx::armem::client::util
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