[Tizen] Calculrate screen position with RenderTask
[platform/core/uifw/dali-core.git] / dali / public-api / signals / connection-tracker.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
18 // CLASS HEADER
19 #include <dali/public-api/signals/connection-tracker.h>
20
21 // EXTERNAL INCLUDES
22 #include <unordered_map>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/signals/callback.h>
26 #include <dali/public-api/signals/signal-slot-observers.h>
27
28 namespace Dali
29 {
30 /**
31  * @brief Extra struct for callback base cache.
32  */
33 struct ConnectionTracker::Impl
34 {
35   Impl()  = default;
36   ~Impl() = default;
37
38   std::unordered_map<CallbackBase*, SlotObserver*> mCallbackCache;
39 };
40
41 ConnectionTracker::ConnectionTracker()
42 : mCacheImpl(new ConnectionTracker::Impl())
43 {
44 }
45
46 ConnectionTracker::~ConnectionTracker()
47 {
48   DisconnectAll();
49   delete mCacheImpl;
50 }
51
52 void ConnectionTracker::DisconnectAll()
53 {
54   // Iterate unordered list of CallbackBase / SlotObserver.
55   // Note that we don't need to keep order of ConnectionTracker::SignalConnected
56   for(auto iter = mCacheImpl->mCallbackCache.begin(), iterEnd = mCacheImpl->mCallbackCache.end(); iter != iterEnd; ++iter)
57   {
58     auto& callbackBase = iter->first;
59     auto& slotObserver = iter->second;
60
61     // Tell the signal that the slot is disconnected
62     slotObserver->SlotDisconnected(callbackBase);
63   }
64
65   mCacheImpl->mCallbackCache.clear();
66   mCacheImpl->mCallbackCache.rehash(0); ///< Note : unordered_map.clear() didn't deallocate memory.
67 }
68
69 void ConnectionTracker::SignalConnected(SlotObserver* slotObserver, CallbackBase* callback)
70 {
71   // We can assume that there is no duplicated callback come here
72   mCacheImpl->mCallbackCache[callback] = slotObserver;
73 }
74
75 void ConnectionTracker::SignalDisconnected(SlotObserver* slotObserver, CallbackBase* callback)
76 {
77   // Remove from CallbackBase / SlotObserver list
78   const bool isRemoved = mCacheImpl->mCallbackCache.erase(callback);
79   if(DALI_LIKELY(isRemoved))
80   {
81     // Disconnection complete
82     return;
83   }
84
85   DALI_ABORT("Callback lost in SignalDisconnected()");
86 }
87
88 std::size_t ConnectionTracker::GetConnectionCount() const
89 {
90   return mCacheImpl->mCallbackCache.size();
91 }
92
93 } // namespace Dali