Add Noti class 84/259384/6
authorjh9216.park <jh9216.park@samsung.com>
Mon, 7 Jun 2021 06:27:30 +0000 (02:27 -0400)
committerHwanKyu Jhun <h.jhun@samsung.com>
Thu, 17 Jun 2021 23:35:18 +0000 (23:35 +0000)
- Replace old c api into c++

- example
int TestCb(const Noti::Msg& msg) {
  auto [pid, uid, b] = GetNotiArgs<pid_t, uid_t, bundle*>(msg);
...
  return Noti::CONTINUE;
}

void NotiParams() {
  Noti::Listen("TestMsg", TestCb);
...
  Noti::Send({"TestMsg", getpid(), getuid(), b1.GetHandle(), b2.GetHandle()});
}

Change-Id: Iab1ba059ab3e761d6ce51fa6f0082bca6f6cdf89
Signed-off-by: jh9216.park <jh9216.park@samsung.com>
CMakeLists.txt
packaging/amd.spec
src/lib/CMakeLists.txt
src/lib/amd_noti.cc
src/lib/api/amd_api_noti.cc
src/lib/api/amd_api_noti.hh [new file with mode: 0644]

index dff4ff9c56f2c891a0656b9f8fa773c6289caa9a..6839319dfe494374190dca7451f1daffa25a6fb5 100644 (file)
@@ -8,7 +8,7 @@ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
 SET(CMAKE_C_FLAGS_DEBUG "-O0 -g")
 SET(CMAKE_C_FLAGS_RELEASE "-O2")
 
-SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_C_FLAGS} -std=c++14")
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_C_FLAGS} -std=c++17")
 SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
 SET(CMAKE_CXX_FLAGS_RELEASE "-O2")
 
index be735396b18276acec58f52818d9db495b3c0542..9686c6dcb1083d08da072480e1bea9d6c78d72dd 100644 (file)
@@ -334,7 +334,7 @@ systemctl daemon-reload
 %{_moddir}/conf/amd.conf
 
 %files devel
-%{_includedir}/amd/*.h
+%{_includedir}/amd/*.h*
 %{_libdir}/libamd.so
 %{_libdir}/pkgconfig/*pc
 
index 258b144f71f8ca10c134b6b3137b9dfb659f7563..6ad7f4eb4b506cd36819bd0cd0fa789ca93d4bfe 100644 (file)
@@ -67,4 +67,4 @@ INSTALL(TARGETS ${TARGET_LIB_AMD} DESTINATION ${LIB_INSTALL_DIR} COMPONENT
   RuntimeLibraries)
 INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/api/ DESTINATION include/amd
   FILES_MATCHING
-  PATTERN "*.h")
+  PATTERN "*.h*")
index bdca2c735b36d13dbe2cc7a4b4bf7ba2229c9c65..254f2383a843dfdcaa0c5d6f36d0ec23f9493b08 100644 (file)
  * limitations under the License.
  */
 
-#include <bundle.h>
 #include <stdlib.h>
 #include <stdio.h>
 
-#include <vector>
-#include <string>
-#include <unordered_map>
-
 #include "amd_config.h"
 #include "amd_util.h"
 #include "amd_noti.h"
-
-namespace {
-
-std::unordered_map<std::string, std::vector<noti_cb>> __listeners;
-
-}  // namespace
+#include "api/amd_api_noti.hh"
 
 int _noti_send(const char* msg, int arg1, int arg2, void* arg3, bundle* data) {
-  int ret;
-
-  if (!msg) {
-    _E("Invalid parameter");
-    return -1;
-  }
-
-  auto iter = __listeners.find(msg);
-  if (iter == __listeners.end())
-    return 0;
-
-  auto& li = iter->second;
-  for (noti_cb noti_callback : li) {
-    if (noti_callback) {
-      ret = noti_callback(msg, arg1, arg2, arg3, data);
-      if (ret != NOTI_CONTINUE)
-        return -1;
-    }
-  }
-
-  return 0;
+  return amd::api::Noti::Send({msg, arg1, arg2, arg3, data});
 }
 
 int _noti_listen(const char* msg, noti_cb callback) {
-  if (!msg) {
-    _E("Invalid parameter");
-    return -1;
-  }
-  auto& li = __listeners[msg];
-  li.push_back(callback);
-  return 0;
+  return amd::api::Noti::Listen(msg, callback);
 }
 
 int _noti_init(void) {
@@ -71,5 +35,5 @@ int _noti_init(void) {
 }
 
 void _noti_fini(void) {
-  __listeners.clear();
-}
+  amd::api::Noti::Dispose();
+}
\ No newline at end of file
index ae1996fd8a6915dcaea1d57654817f526bcf1d1c..932687bf8da4edf0070428901e7d0a94329f32f9 100644 (file)
  */
 
 #include "lib/amd_api.h"
-#include "lib/amd_noti.h"
+#include "lib/amd_util.h"
 #include "lib/api/amd_api_noti.h"
+#include "lib/api/amd_api_noti.hh"
+
+namespace amd {
+namespace api {
+
+namespace {
+
+std::unordered_map<std::string,
+    std::vector<std::pair<Noti::NotiCb, noti_cb>>> listeners_;
+
+}  // namespace
+
+class Noti::Msg::Impl {
+ public:
+  Impl(std::string msg, int val1 = 0, int val2 = 0, void* val3 = nullptr)
+      : msg_(std::move(msg)), val1_(val1), val2_(val2), val3_(val3) {}
+  Impl(std::string msg, int val1, int val2, void* val3,
+      tizen_base::Bundle data)
+      : msg_(std::move(msg)), val1_(val1), val2_(val2), val3_(val3),
+        data_(std::move(data)) {}
+  Impl(std::string msg, int val1, int val2, void* val3,
+      const bundle* data)
+      : msg_(std::move(msg)), val1_(val1), val2_(val2), val3_(val3),
+        data_(data ? tizen_base::Bundle(const_cast<bundle*>(data),
+            false, false) : tizen_base::Bundle()) {}
+
+ private:
+  friend class Noti::Msg;
+  std::string msg_;
+  int val1_;
+  int val2_;
+  void* val3_;
+  tizen_base::Bundle data_;
+};
+
+Noti::Msg::Msg(std::string msg, int val1, int val2, void* val3)
+    : impl_(std::make_shared<Noti::Msg::Impl>(std::move(msg), val1, val2,
+          val3)) {}
+
+Noti::Msg::Msg(std::string msg, int val1, int val2, void* val3,
+    tizen_base::Bundle data)
+    : impl_(std::make_shared<Noti::Msg::Impl>(
+        std::move(msg), val1, val2, val3, std::move(data))) {}
+
+Noti::Msg::Msg(std::string msg, int val1, int val2, void* val3,
+    const bundle* data)
+    : impl_(std::make_shared<Noti::Msg::Impl>(
+        std::move(msg), val1, val2, val3, data)) {}
+
+const std::string& Noti::Msg::GetMessage() const {
+  return impl_->msg_;
+}
+
+const tizen_base::Bundle& Noti::Msg::GetBundle() const {
+  return impl_->data_;
+}
+
+int Noti::Msg::GetArg1() const {
+  return impl_->val1_;
+}
+
+int Noti::Msg::GetArg2() const {
+  return impl_->val2_;
+}
+
+void* Noti::Msg::GetArg3() const {
+  return impl_->val3_;
+}
+
+int Noti::Listen(const std::string& msg, Noti::NotiCb cb) {
+  if (msg.empty()) {
+    _E("Invalid parameter");
+    return -1;
+  }
+  auto& li = listeners_[msg];
+  li.push_back({ cb, nullptr });
+  return 0;
+}
+
+int Noti::Listen(const std::string& msg, noti_cb cb) {
+  if (msg.empty()) {
+    _E("Invalid parameter");
+    return -1;
+  }
+
+  auto& li = listeners_[msg];
+  li.push_back({ nullptr, cb });
+  return 0;
+}
+
+int Noti::Send(Noti::Msg msg) {
+  if (msg.GetMessage().empty()) {
+    _E("Invalid parameter");
+    return -1;
+  }
+
+  auto iter = listeners_.find(msg.GetMessage());
+  if (iter == listeners_.end())
+    return 0;
+
+  auto& li = iter->second;
+  for (auto& cb : li) {
+    if (cb.first) {
+      int ret = cb.first(msg);
+      if (ret != NOTI_CONTINUE)
+        return -1;
+    } else {
+      auto [arg1, arg2, arg3] = GetNotiArgs<int, int, void*>(msg);
+      int ret = cb.second(msg.GetMessage().c_str(), arg1, arg2, arg3,
+          msg.GetBundle().GetHandle());
+      if (ret != NOTI_CONTINUE)
+        return -1;
+    }
+  }
+
+  return 0;
+}
+
+void Noti::Dispose() {
+  listeners_.clear();
+}
+
+}  // namespace api
+}  // namespace amd
 
 extern "C" EXPORT_API int amd_noti_send(const char *msg, int arg1, int arg2,
     void* arg3, bundle* data) {
-       return _noti_send(msg, arg1, arg2, arg3, data);
+  return amd::api::Noti::Send({msg, arg1, arg2, arg3, data});
 }
 
 extern "C" EXPORT_API int amd_noti_listen(const char* msg,
     amd_noti_cb callback) {
-       return _noti_listen(msg, (noti_cb)callback);
-}
+  return amd::api::Noti::Listen(msg, callback);
+}
\ No newline at end of file
diff --git a/src/lib/api/amd_api_noti.hh b/src/lib/api/amd_api_noti.hh
new file mode 100644 (file)
index 0000000..4747969
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef LIB_API_AMD_API_NOTI_HH_
+#define LIB_API_AMD_API_NOTI_HH_
+
+#include <bundle.h>
+#include <bundle_cpp.h>
+
+#include <functional>
+#include <memory>
+#include <vector>
+#include <string>
+#include <unordered_map>
+
+#include "amd_noti.h"
+
+#undef EXPORT_API
+#define EXPORT_API __attribute__ ((visibility("default")))
+
+namespace amd {
+namespace api {
+
+class EXPORT_API Noti {
+ public:
+  class Msg {
+   public:
+    Msg(std::string msg, int val1 = 0, int val2 = 0, void* val3 = nullptr);
+    Msg(std::string msg, int val1, int val2, void* val3,
+        tizen_base::Bundle data);
+    Msg(std::string msg, int val1, int val2, void* val3,
+        const bundle* data);
+
+    const std::string& GetMessage() const;
+    const tizen_base::Bundle& GetBundle() const;
+    int GetArg1() const;
+    int GetArg2() const;
+    void* GetArg3() const;
+
+   private:
+    class Impl;
+    std::shared_ptr<Impl> impl_;
+  };
+
+  using NotiCb = std::function<int (const Msg& msg)>;
+
+  enum ReturnVal {
+    CONTINUE = 0,
+    STOP = 2
+  };
+
+  static int Listen(const std::string& msg, NotiCb cb);
+  static int Listen(const std::string& msg, noti_cb cb);
+  static int Send(Msg msg);
+  static void Dispose();
+};
+
+template<class T1, class T2, class T3>
+std::tuple<T1, T2, T3> GetNotiArgs(const Noti::Msg& msg) {
+  return std::make_tuple(static_cast<T1>(msg.GetArg1()),
+      static_cast<T2>(msg.GetArg2()), reinterpret_cast<T3>(msg.GetArg3()));
+}
+
+}  // namespace api
+}  // namespace amd;
+
+#endif  // LIB_API_AMD_API_NOTI_HH_
\ No newline at end of file