From: hyunho Date: Tue, 19 Mar 2019 06:42:36 +0000 (+0900) Subject: Using unique_ptr for sender and listener X-Git-Tag: accepted/tizen/unified/20190327.160606~14 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=425c9f3ceab38f07ccfcb26b2458a53d92f84cb4;p=platform%2Fcore%2Fapi%2Fnotification.git Using unique_ptr for sender and listener Change-Id: Iaf238d82dbe64501aef23fb06cf7124dcd3d0bde Signed-off-by: hyunho --- diff --git a/notification-ex/dbus_event_listener.cc b/notification-ex/dbus_event_listener.cc index 1aa4b45..2346b59 100644 --- a/notification-ex/dbus_event_listener.cc +++ b/notification-ex/dbus_event_listener.cc @@ -57,7 +57,7 @@ void DBusEventListener::Impl::SignalCb(GDBusConnection* connection, const gchar* signal_name, GVariant* parameters, void* user_data) { - DBusEventListener* dl = static_cast(user_data); + DBusEventListener::Impl* dl = static_cast(user_data); char* appid = nullptr; GVariantIter *iter = nullptr; char* event_info_raw = nullptr; @@ -84,7 +84,7 @@ void DBusEventListener::Impl::SignalCb(GDBusConnection* connection, Bundle b(event_info_raw); EventInfo info(b); - dl->NotifyObserver(info, ret_list); + dl->parent_->NotifyObserver(info, ret_list); } void DBusEventListener::Impl::OnMethodCall( @@ -93,7 +93,7 @@ void DBusEventListener::Impl::OnMethodCall( GVariant *parameters, GDBusMethodInvocation *invocation, gpointer user_data) { LOGI("method_name[%s] sender[%s]", method_name, sender); - DBusEventListener* dl = static_cast(user_data); + DBusEventListener::Impl* dl = static_cast(user_data); GVariant *reply_body = NULL; if (g_strcmp0(method_name, "Get") == 0) { char* appid = NULL; @@ -102,7 +102,7 @@ void DBusEventListener::Impl::OnMethodCall( Bundle b(serialized); EventInfo info(b); - list result = dl->NotifyObserver(info); + list result = dl->parent_->NotifyObserver(info); GVariantBuilder* builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)")); for (auto& i : result) { g_variant_builder_add(builder, "(s)", diff --git a/notification-ex/event_info.cc b/notification-ex/event_info.cc index 0ffbe10..552159a 100644 --- a/notification-ex/event_info.cc +++ b/notification-ex/event_info.cc @@ -49,9 +49,10 @@ EventInfo::Impl::Impl(EventInfo* parent, EventInfo::EventType type, std::string LOGI("EventInfo impl created"); } -EventInfo::EventInfo(Bundle serialized) { +EventInfo::EventInfo(Bundle serialized) + : impl_(new Impl(this, EventInfo::Post, "", "", "", "")) { string event_str = serialized.GetString(NOTIFICATION_EX_EVENT_TYPE_KEY); - impl_->type_ = static_cast(strtol(event_str.c_str(), NULL, 10)); + impl_->type_ = (EventInfo::EventType)(int)strtol(event_str.c_str(), NULL, 10); impl_->owner_ = serialized.GetString(NOTIFICATION_EX_EVENT_OWNER_KEY); impl_->channel_ = serialized.GetString(NOTIFICATION_EX_EVENT_CHANNEL_KEY); impl_->item_id_ = serialized.GetString(NOTIFICATION_EX_EVENT_ITEM_ID_KEY); diff --git a/notification-ex/ex_util.cc b/notification-ex/ex_util.cc index a7ddcaf..be5136f 100644 --- a/notification-ex/ex_util.cc +++ b/notification-ex/ex_util.cc @@ -96,4 +96,4 @@ string GetAppId() { } } // namespace util -} // namespace watchface_complication +} // namespace notification diff --git a/notification-ex/manager.cc b/notification-ex/manager.cc index 78d83c2..e0cd39b 100644 --- a/notification-ex/manager.cc +++ b/notification-ex/manager.cc @@ -41,18 +41,20 @@ using namespace std; using namespace notification::item; namespace notification { -Manager::Manager(IEventSender* sender, IEventListener* listener, string receiver_group) - : impl_(new Impl(this, sender, listener, receiver_group)) { +Manager::Manager(unique_ptr sender, + unique_ptr listener, string receiver_group) + : impl_(new Impl(this, move(sender), move(listener), receiver_group)) { } Manager::~Manager() = default; Manager::Impl::~Impl() { listener_->UnRegisterObserver(parent_); } - Manager::Impl::Impl(Manager* parent, - IEventSender* sender, IEventListener* listener, string receiver_group) - : sender_(sender), listener_(listener), receiver_group_(receiver_group), + unique_ptr sender, + unique_ptr listener, string receiver_group) + : sender_(move(sender)), listener_(move(listener)), + receiver_group_(receiver_group), parent_(parent) { LOGI("impl created"); listener_->RegisterObserver(parent_); diff --git a/notification-ex/manager.h b/notification-ex/manager.h index 56b2b34..7168b2c 100644 --- a/notification-ex/manager.h +++ b/notification-ex/manager.h @@ -19,6 +19,7 @@ #include #include +#include #include "notification-ex/abstract_item.h" #include "notification-ex/event_observer_interface.h" @@ -33,7 +34,8 @@ namespace notification { class EXPORT_API Manager : public IEventObserver { public: - Manager(IEventSender* sender, IEventListener* listener, std::string receiver_group = ""); + Manager(std::unique_ptr sender, + std::unique_ptr listener, std::string receiver_group = ""); virtual ~Manager(); std::list> Get(); diff --git a/notification-ex/manager_implementation.h b/notification-ex/manager_implementation.h index 9b7c79e..667cff7 100644 --- a/notification-ex/manager_implementation.h +++ b/notification-ex/manager_implementation.h @@ -30,15 +30,15 @@ class Manager::Impl { virtual ~Impl(); private: - Impl(Manager* parent, - IEventSender* sender, IEventListener* listener, std::string receiver_group); + Impl(Manager* parent, std::unique_ptr sender, + std::unique_ptr listener, std::string receiver_group); private: friend class Manager; void SendNotify(std::shared_ptr noti, EventInfo::EventType type); - IEventSender* sender_; - IEventListener* listener_; + std::unique_ptr sender_; + std::unique_ptr listener_; std::string receiver_group_; Manager* parent_; }; diff --git a/notification-ex/reporter.cc b/notification-ex/reporter.cc index bd32670..2c9a4a2 100644 --- a/notification-ex/reporter.cc +++ b/notification-ex/reporter.cc @@ -39,18 +39,18 @@ using namespace std; using namespace notification::item; namespace notification { -Reporter::Reporter(IEventSender* sender, IEventListener* listener) - : impl_(new Impl(this, sender, listener)) { +Reporter::Reporter( + unique_ptr sender, unique_ptr listener) + : impl_(new Impl(this, move(sender), move(listener))) { } Reporter::~Reporter() = default; Reporter::Impl::~Impl() { listener_->UnRegisterObserver(parent_); } - Reporter::Impl::Impl(Reporter* parent, - IEventSender* sender, IEventListener* listener) - : sender_(sender), listener_(listener), parent_(parent) { + unique_ptr sender, unique_ptr listener) + : sender_(move(sender)), listener_(move(listener)), parent_(parent) { LOGI("impl created"); listener_->RegisterObserver(parent_); } diff --git a/notification-ex/reporter.h b/notification-ex/reporter.h index 61aa457..3b5fd96 100644 --- a/notification-ex/reporter.h +++ b/notification-ex/reporter.h @@ -19,6 +19,7 @@ #include #include +#include #include "notification-ex/event_info.h" #include "notification-ex/event_observer_interface.h" @@ -34,7 +35,8 @@ namespace notification { class EXPORT_API Reporter : public IEventObserver { public: - Reporter(IEventSender* sender, IEventListener* listener); + Reporter(std::unique_ptr sender, + std::unique_ptr listener); virtual ~Reporter(); void SendEvent(const EventInfo& info, std::shared_ptr noti); diff --git a/notification-ex/reporter_implementation.h b/notification-ex/reporter_implementation.h index 378e507..dc63b88 100644 --- a/notification-ex/reporter_implementation.h +++ b/notification-ex/reporter_implementation.h @@ -30,15 +30,15 @@ class Reporter::Impl { virtual ~Impl(); private: - Impl(Reporter* parent, - IEventSender* sender, IEventListener* listener); + Impl(Reporter* parent, std::unique_ptr sender, + std::unique_ptr listener); private: friend class Reporter; void SendNotify(std::shared_ptr noti, EventInfo::EventType type); - IEventSender* sender_; - IEventListener* listener_; + std::unique_ptr sender_; + std::unique_ptr listener_; Reporter* parent_; }; diff --git a/unittest/src/test_event_info.cc b/unittest/src/test_event_info.cc new file mode 100644 index 0000000..b1f20dc --- /dev/null +++ b/unittest/src/test_event_info.cc @@ -0,0 +1,38 @@ +// Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved +// Use of this source code is governed by a apache 2.0 license that can be +// found in the LICENSE file. + +#include + +#include "notification-ex/event_info.h" + +using namespace notification; +using namespace std; + +namespace { + +class EventInfoTest : public ::testing::Test { + protected: + void SetUp() override {} + void TearDown() override {} +}; + +TEST_F(EventInfoTest, SerializeDeserialize) { + EventInfo info(EventInfo::Get, "test", "channel", "id", "tag"); + Bundle b = info.Serialize(); + EventInfo serialized(b); + ASSERT_EQ(serialized.GetEventType(), info.GetEventType()); + ASSERT_EQ(serialized.GetOwner(), info.GetOwner()); + ASSERT_EQ(serialized.GetChannel(), info.GetChannel()); + ASSERT_EQ(serialized.GetItemId(), info.GetItemId()); + ASSERT_EQ(serialized.GetTag(), info.GetTag()); +} + +TEST_F(EventInfoTest, GetString) { + ASSERT_EQ(EventInfo::GetString(EventInfo::Post), "Post"); + ASSERT_EQ(EventInfo::GetString(EventInfo::Update), "Update"); + ASSERT_EQ(EventInfo::GetString(EventInfo::Delete), "Delete"); + ASSERT_EQ(EventInfo::GetString(EventInfo::Get), "Get"); +} + +} // namespace