Using unique_ptr for sender and listener 38/201738/5
authorhyunho <hhstark.kang@samsung.com>
Tue, 19 Mar 2019 06:42:36 +0000 (15:42 +0900)
committerhyunho <hhstark.kang@samsung.com>
Tue, 19 Mar 2019 07:43:55 +0000 (16:43 +0900)
Change-Id: Iaf238d82dbe64501aef23fb06cf7124dcd3d0bde
Signed-off-by: hyunho <hhstark.kang@samsung.com>
notification-ex/dbus_event_listener.cc
notification-ex/event_info.cc
notification-ex/ex_util.cc
notification-ex/manager.cc
notification-ex/manager.h
notification-ex/manager_implementation.h
notification-ex/reporter.cc
notification-ex/reporter.h
notification-ex/reporter_implementation.h
unittest/src/test_event_info.cc [new file with mode: 0644]

index 1aa4b45..2346b59 100644 (file)
@@ -57,7 +57,7 @@ void DBusEventListener::Impl::SignalCb(GDBusConnection* connection,
                       const gchar* signal_name,
                       GVariant* parameters,
                       void* user_data) {
-  DBusEventListener* dl = static_cast<DBusEventListener*>(user_data);
+  DBusEventListener::Impl* dl = static_cast<DBusEventListener::Impl*>(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<DBusEventListener*>(user_data);
+  DBusEventListener::Impl* dl = static_cast<DBusEventListener::Impl*>(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<Bundle> result = dl->NotifyObserver(info);
+    list<Bundle> 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)",
index 0ffbe10..552159a 100644 (file)
@@ -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<EventInfo::EventType>(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);
index a7ddcaf..be5136f 100644 (file)
@@ -96,4 +96,4 @@ string GetAppId() {
 }
 
 }  // namespace util
-}  // namespace watchface_complication
+}  // namespace notification
index 78d83c2..e0cd39b 100644 (file)
@@ -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<IEventSender> sender,
+    unique_ptr<IEventListener> 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<IEventSender> sender,
+    unique_ptr<IEventListener> listener, string receiver_group)
+  : sender_(move(sender)), listener_(move(listener)),
+    receiver_group_(receiver_group),
     parent_(parent) {
   LOGI("impl created");
   listener_->RegisterObserver(parent_);
index 56b2b34..7168b2c 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <string>
 #include <list>
+#include <memory>
 
 #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<IEventSender> sender,
+      std::unique_ptr<IEventListener> listener, std::string receiver_group = "");
   virtual ~Manager();
 
   std::list<std::shared_ptr<item::AbstractItem>> Get();
index 9b7c79e..667cff7 100644 (file)
@@ -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<IEventSender> sender,
+      std::unique_ptr<IEventListener> listener, std::string receiver_group);
 
  private:
   friend class Manager;
   void SendNotify(std::shared_ptr<item::AbstractItem> noti,
       EventInfo::EventType type);
-  IEventSender* sender_;
-  IEventListener* listener_;
+  std::unique_ptr<IEventSender> sender_;
+  std::unique_ptr<IEventListener> listener_;
   std::string receiver_group_;
   Manager* parent_;
 };
index bd32670..2c9a4a2 100644 (file)
@@ -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<IEventSender> sender, unique_ptr<IEventListener> 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<IEventSender> sender, unique_ptr<IEventListener> listener)
+  : sender_(move(sender)), listener_(move(listener)), parent_(parent) {
   LOGI("impl created");
   listener_->RegisterObserver(parent_);
 }
index 61aa457..3b5fd96 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <string>
 #include <list>
+#include <memory>
 
 #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<IEventSender> sender,
+        std::unique_ptr<IEventListener> listener);
   virtual ~Reporter();
 
   void SendEvent(const EventInfo& info, std::shared_ptr<item::AbstractItem> noti);
index 378e507..dc63b88 100644 (file)
@@ -30,15 +30,15 @@ class Reporter::Impl {
   virtual ~Impl();
 
  private:
-  Impl(Reporter* parent,
-    IEventSender* sender, IEventListener* listener);
+  Impl(Reporter* parent, std::unique_ptr<IEventSender> sender,
+      std::unique_ptr<IEventListener> listener);
 
  private:
   friend class Reporter;
   void SendNotify(std::shared_ptr<item::AbstractItem> noti,
       EventInfo::EventType type);
-  IEventSender* sender_;
-  IEventListener* listener_;
+  std::unique_ptr<IEventSender> sender_;
+  std::unique_ptr<IEventListener> listener_;
   Reporter* parent_;
 };
 
diff --git a/unittest/src/test_event_info.cc b/unittest/src/test_event_info.cc
new file mode 100644 (file)
index 0000000..b1f20dc
--- /dev/null
@@ -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 <gmock/gmock.h>
+
+#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