From: jh9216.park Date: Mon, 7 Jun 2021 06:27:30 +0000 (-0400) Subject: Add Noti class X-Git-Tag: submit/tizen/20210708.082443~7 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c56714710da8049c6460c64a02d0cda68003cb2b;p=platform%2Fcore%2Fappfw%2Famd.git Add Noti class - Replace old c api into c++ - example int TestCb(const Noti::Msg& msg) { auto [pid, uid, b] = GetNotiArgs(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 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index dff4ff9c..6839319d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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") diff --git a/packaging/amd.spec b/packaging/amd.spec index be735396..9686c6dc 100644 --- a/packaging/amd.spec +++ b/packaging/amd.spec @@ -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 diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 258b144f..6ad7f4eb 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -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*") diff --git a/src/lib/amd_noti.cc b/src/lib/amd_noti.cc index bdca2c73..254f2383 100644 --- a/src/lib/amd_noti.cc +++ b/src/lib/amd_noti.cc @@ -14,56 +14,20 @@ * limitations under the License. */ -#include #include #include -#include -#include -#include - #include "amd_config.h" #include "amd_util.h" #include "amd_noti.h" - -namespace { - -std::unordered_map> __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 diff --git a/src/lib/api/amd_api_noti.cc b/src/lib/api/amd_api_noti.cc index ae1996fd..932687bf 100644 --- a/src/lib/api/amd_api_noti.cc +++ b/src/lib/api/amd_api_noti.cc @@ -15,15 +15,139 @@ */ #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>> 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(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(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( + 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( + 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(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 index 00000000..47479698 --- /dev/null +++ b/src/lib/api/amd_api_noti.hh @@ -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 +#include + +#include +#include +#include +#include +#include + +#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_; + }; + + using NotiCb = std::function; + + 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 +std::tuple GetNotiArgs(const Noti::Msg& msg) { + return std::make_tuple(static_cast(msg.GetArg1()), + static_cast(msg.GetArg2()), reinterpret_cast(msg.GetArg3())); +} + +} // namespace api +} // namespace amd; + +#endif // LIB_API_AMD_API_NOTI_HH_ \ No newline at end of file