Refactor watch control API 48/283648/2
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 1 Nov 2022 02:46:43 +0000 (02:46 +0000)
committerHwankyu Jhun <h.jhun@samsung.com>
Tue, 1 Nov 2022 02:50:26 +0000 (02:50 +0000)
The watch control API is refactored using C++ language.

Change-Id: I474c034f4998bfdf8483406ba157d3b97dbb4ade
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/aul_watch_control.c [deleted file]
src/aul_watch_control.cc [new file with mode: 0644]

diff --git a/src/aul_watch_control.c b/src/aul_watch_control.c
deleted file mode 100644 (file)
index 6d06bef..0000000
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * Copyright (c) 2019 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.
- */
-
-#define _GNU_SOURCE
-#include <stdio.h>
-#include <stdlib.h>
-
-#include <glib.h>
-
-#include "aul.h"
-#include "aul_api.h"
-#include "aul_util.h"
-#include "aul_watch_control.h"
-#include "aul_watch_control_internal.h"
-
-struct aul_watch_control_s {
-       aul_watch_control_cb callback;
-       void *user_data;
-};
-
-static GList *__controls;
-
-API int aul_watch_control_add_handler(aul_watch_control_cb callback,
-               void *user_data, aul_watch_control_h *handle)
-{
-       struct aul_watch_control_s *control;
-
-       if (!callback || !handle) {
-               _E("Invalid parameter");
-               return AUL_R_EINVAL;
-       }
-
-       control = calloc(1, sizeof(struct aul_watch_control_s));
-       if (!control) {
-               _E("Out of memory");
-               return AUL_R_ENOMEM;
-       }
-
-       control->callback = callback;
-       control->user_data = user_data;
-
-       __controls = g_list_append(__controls, control);
-
-       *handle = control;
-
-       return AUL_R_OK;
-}
-
-API int aul_watch_control_remove_handler(aul_watch_control_h handle)
-{
-       if (!handle || !g_list_find(__controls, handle)) {
-               _E("Invalid parameter");
-               return AUL_R_EINVAL;
-       }
-
-       __controls = g_list_remove(__controls, handle);
-       free(handle);
-
-       return AUL_R_OK;
-}
-
-static void __foreach_control_cb(gpointer data, gpointer user_data)
-{
-       struct aul_watch_control_s *control;
-       bundle *b = (bundle *)user_data;
-
-       control = (struct aul_watch_control_s *)data;
-       control->callback(b, control->user_data);
-}
-
-void aul_watch_control_invoke(bundle *b)
-{
-       if (!__controls)
-               return;
-
-       g_list_foreach(__controls, __foreach_control_cb, b);
-}
diff --git a/src/aul_watch_control.cc b/src/aul_watch_control.cc
new file mode 100644 (file)
index 0000000..d21936a
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2019 - 2022 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.
+ */
+
+#include "include/aul_watch_control.h"
+
+#include <algorithm>
+#include <list>
+#include <iterator>
+
+#include "aul_api.h"
+#include "aul_util.h"
+#include "aul_watch_control_internal.h"
+#include "include/aul.h"
+
+namespace {
+
+class WatchControl {
+ public:
+  WatchControl(aul_watch_control_cb cb, void* user_data)
+      : cb_(cb), user_data_(user_data) {
+  }
+
+  void Invoke(bundle* b) {
+    if (cb_)
+      cb_(b, user_data_);
+  }
+
+ private:
+  aul_watch_control_cb cb_;
+  void* user_data_;
+};
+
+std::list<WatchControl*> controls;
+
+}  // namespace
+
+extern "C" API int aul_watch_control_add_handler(aul_watch_control_cb callback,
+    void* user_data, aul_watch_control_h* handle) {
+  if (callback == nullptr || handle == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  auto* control = new (std::nothrow) WatchControl(callback, user_data);
+  if (control == nullptr) {
+    _E("Out of memory");
+    return AUL_R_ENOMEM;
+  }
+
+  controls.push_back(control);
+  *handle = static_cast<aul_watch_control_h>(control);
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_watch_control_remove_handler(
+    aul_watch_control_h handle) {
+  if (handle == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  auto* control = static_cast<WatchControl*>(handle);
+  auto found = std::find(controls.begin(), controls.end(), control);
+  if (found == controls.end()) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  controls.erase(found);
+  delete control;
+  return AUL_R_OK;
+}
+
+void aul_watch_control_invoke(bundle* b) {
+  if (controls.empty())
+    return;
+
+  for (auto control : controls)
+    control->Invoke(b);
+}