DSSignalBroker: add skeleton codes
[platform/core/uifw/libds.git] / src / DSSignal / DSSignalBroker.cpp
1 /*
2 * Copyright © 2020 Samsung Electronics co., Ltd. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <DSSignalBroker.h>
25
26 namespace display_server
27 {
28
29 std::mutex DSSignalBroker::__mutex;
30 DSSignalBroker* DSSignalBroker::__signalBroker { nullptr };
31
32 DSSignalBroker::DSSignalBroker(DSObject *parent)
33 {
34 }
35
36 DSSignalBroker::~DSSignalBroker()
37 {
38 }
39
40 DSSignalBroker *DSSignalBroker::getInstance()
41 {
42         std::lock_guard<std::mutex> tLock(__mutex);
43
44         if (!__signalBroker)
45         {
46                 __signalBroker = new DSSignalBroker(new DSObject);
47                 DSLOG_INF("DSSignalBroker", "DSSignalBroker instance has been created !");
48         }
49
50         return __signalBroker;
51 }
52
53 void DSSignalBroker::releaseInstance()
54 {
55         std::lock_guard<std::mutex> tLock(__mutex);
56
57         if (__signalBroker && __signalBroker->__signalHash.empty())
58         {
59                 delete __signalBroker;
60                 __signalBroker = nullptr;
61                 DSLOG_INF("DSSignalBroker",     "DSSignalBroker instance has been removed !");
62         }
63 }
64
65 bool DSSignalBroker::registerCallback(std::string signalStr, DSObject *slot, std::function<void(void *)> func)
66 {
67         if (!__signalBroker) return false;
68
69         if (__signalBroker->__signalHash.find(signalStr) == __signalBroker->__signalHash.end())
70                 {
71                         DSLOG_INF("DSSignalBroker","registerCallback:: cannot find signal, add new one %s", signalStr.c_str());
72
73                         __signalBroker->__signalHash[signalStr] = new DSSignal<void *>();
74                 }
75
76         __signalBroker->__signalHash[signalStr]->connect(slot, func);
77
78         return true;
79 }
80
81 bool DSSignalBroker::deregisterCallback(std::string signalStr, DSObject *slot, std::function<void(void *)> func)
82 {
83         if (!__signalBroker) return false;
84
85         if (__signalBroker->__signalHash.find(signalStr) == __signalBroker->__signalHash.end())
86                 return false;
87
88         // TODO: have to impl disconnect method in DSSignal
89         //__signalBroker->__signalHash[signalStr]->disconnect(slot, func);
90
91         return true;
92 }
93
94 bool DSSignalBroker::emitSignal(std::string signalStr, void *arg)
95 {
96         if (!__signalBroker) return false;
97
98         if (__signalBroker->__signalHash.find(signalStr) == __signalBroker->__signalHash.end())
99                 {
100                         DSLOG_INF("DSSignalBroker","emitSignal:: cannot find signal, add new one %s", signalStr.c_str());
101
102                         __signalBroker->__signalHash[signalStr] = new DSSignal<void *>();
103                 }
104
105         __signalBroker->__signalHash[signalStr]->emit(arg);
106
107         return true;
108 }
109
110 } // end of namespace display_server