use std::function instead of the raw function pointer.
Change-Id: I02e368c09e6f673fc8881842694296d171ff9369
{
public:
virtual ~IDSObjectObserver() = default;
- virtual void destroyed(DSObject *obj) = 0;
+ virtual void destroyed(DSObject *slot) = 0;
};
}
#define __DS_SIGNAL_H_
#include "DSObject.h"
-#include <list>
-#include <memory>
#include <iostream>
#include <string>
+#include <list>
+#include <memory>
+#include <functional>
namespace display_server
{
class DSSlotsObserver : public IDSObjectObserver
{
public:
- DSSlotsObserver() {}
- ~DSSlotsObserver() {}
+ DSSlotsObserver() = default;
+ ~DSSlotsObserver() = default;
- virtual void destroyed(DSObject *obj);
+ virtual void destroyed(DSObject *slot);
virtual void addSlot(DSObject *slot);
protected:
{
public:
DSSlotConnection()
- {}
-
- DSSlotConnection(DSObject *slot, void (*func)(Args...))
- : __callback(func)
+ : __func(nullptr)
+ {};
+ DSSlotConnection(DSObject *slot, std::function<void(Args...)> func)
+ : __func(std::move(func))
{
//TODO: make callback as callback iface
//std::unique_ptr<DSCallbackIface> ptr(new DSCallbackIface(func));
- //__callback = std::move(ptr);
+ //__func = std::move(ptr);
addSlot(slot);
}
void addSlot(DSObject *slot) override
{
- _slotList.push_back(slot);
slot->attachDestroyObserver(this);
+
+ _slotList.push_back(slot);
}
void destroyed(DSObject *slot) override
void invoke(Args... args)
{
if (!_slotList.empty())
- __callback(args...);
+ std::invoke(__func, args...);
}
private:
//TODO: make callback as callback iface
- //std::unique_ptr<DSCallbackIface> __callback;
- void (*__callback)(Args...);
+ //std::unique_ptr<DSCallbackIface> __func;
+ std::function<void(Args...)> __func;
template <typename... _Args>
friend class DSSignal;
};
class DSSignal
{
public:
- DSSignal()
- {}
- virtual ~DSSignal()
- {}
+ DSSignal() = default;
+ virtual ~DSSignal() = default;
// functor
DSSignal<Args...> &operator()(void)
return *this;
}
- void connect(DSObject *slot, void (*func)(Args...))
+ void connect(DSObject *slot, std::function<void(Args...)> func)
{
if ((!slot) || (!func))
return;
}
private:
- DSSlotConnection<Args...> *__findConnection(void (*func)(Args...))
+ DSSlotConnection<Args...> *__findConnection(std::function<void(Args...)> func)
{
for (auto &&connection : __connectionList) {
- if (connection->__callback == func)
+ if (&connection->__func == &func)
return connection.get();
}
--- /dev/null
+#include "libds-tests.h"
+#include "DSSignal.h"
+#include "DSDebugLog.h"
+
+using namespace display_server;
+
+class DSSignalTest : public ::testing::Test
+{
+public:
+ void SetUp(void) override
+ {}
+ void TearDown(void) override
+ {}
+};
+
+class SenderMock : public DSObject
+{
+public:
+ SenderMock()
+ : signalA(std::make_unique<DSSignal<int>>()),
+ signalB(std::make_unique<DSSignal<double>>())
+ {}
+ ~SenderMock() = default;
+
+ void emitSignalA(int value) {
+ signalA->emit(value);
+ }
+
+ void emitSignalB(double value) {
+ signalB->emit(value);
+ }
+
+ std::unique_ptr<DSSignal<int>> signalA;
+ std::unique_ptr<DSSignal<double>> signalB;
+};
+
+class ReceiverMock : public DSObject
+{
+public:
+ ReceiverMock(std::shared_ptr<SenderMock> sender)
+ : __sender(sender)
+ {
+ sender->signalA->connect(this, std::bind(&ReceiverMock::slotA, this, std::placeholders::_1));
+ sender->signalB->connect(this, std::bind(&ReceiverMock::slotB, this, std::placeholders::_1));
+ }
+ ~ReceiverMock() = default;
+
+ void slotA(int value) {
+ DSLOG_INF("DSTEST", "%s is invoked! : value, %d\n", __func__, value);
+ }
+
+ void slotB(double value) {
+ DSLOG_INF("DSTEST", "%s is invoked! : value, %lf\n", __func__, value);
+ }
+
+private:
+ std::shared_ptr<SenderMock> __sender;
+};
+
+TEST_F(DSSignalTest, CreateSignal)
+{
+ DSSignal<int> *signal = new DSSignal<int>();
+ delete signal;
+ ASSERT_TRUE(true);
+}
+
+TEST_F(DSSignalTest, ConnectSignal)
+{
+ auto sender1 = std::make_shared<SenderMock>();
+ auto receiver = std::make_unique<ReceiverMock>(sender1);
+
+ sender1->emitSignalA(30);
+ sender1->emitSignalB(3.0);
+}
'DSDisplayArea-test.cpp',
'DSDebugLog-test.cpp',
'DSDisplayDeviceTDMImpl-test.cpp',
+ 'DSSignal-test.cpp',
]
gmock_dep = dependency('gmock', method : 'pkg-config')