Move function definition to aul header
[platform/core/appfw/aul-1.git] / src / aul_watch_control.cc
1 /*
2  * Copyright (c) 2019 - 2022 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "include/aul_watch_control.h"
18
19 #include <algorithm>
20 #include <list>
21 #include <iterator>
22
23 #include "aul_api.h"
24 #include "aul_util.h"
25 #include "aul_watch_control_internal.h"
26 #include "include/aul.h"
27
28 namespace {
29
30 class WatchControl {
31  public:
32   WatchControl(aul_watch_control_cb cb, void* user_data)
33       : cb_(cb), user_data_(user_data) {
34   }
35
36   void Invoke(bundle* b) {
37     if (cb_)
38       cb_(b, user_data_);
39   }
40
41  private:
42   aul_watch_control_cb cb_;
43   void* user_data_;
44 };
45
46 std::list<WatchControl*> controls;
47
48 }  // namespace
49
50 extern "C" API int aul_watch_control_add_handler(aul_watch_control_cb callback,
51     void* user_data, aul_watch_control_h* handle) {
52   if (callback == nullptr || handle == nullptr) {
53     _E("Invalid parameter");
54     return AUL_R_EINVAL;
55   }
56
57   auto* control = new (std::nothrow) WatchControl(callback, user_data);
58   if (control == nullptr) {
59     _E("Out of memory");
60     return AUL_R_ENOMEM;
61   }
62
63   controls.push_back(control);
64   *handle = static_cast<aul_watch_control_h>(control);
65   return AUL_R_OK;
66 }
67
68 extern "C" API int aul_watch_control_remove_handler(
69     aul_watch_control_h handle) {
70   if (handle == nullptr) {
71     _E("Invalid parameter");
72     return AUL_R_EINVAL;
73   }
74
75   auto* control = static_cast<WatchControl*>(handle);
76   auto found = std::find(controls.begin(), controls.end(), control);
77   if (found == controls.end()) {
78     _E("Invalid parameter");
79     return AUL_R_EINVAL;
80   }
81
82   controls.erase(found);
83   delete control;
84   return AUL_R_OK;
85 }
86
87 void aul_watch_control_invoke(bundle* b) {
88   if (controls.empty())
89     return;
90
91   for (auto control : controls)
92     control->Invoke(b);
93 }