2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/public-api/signals/connection-tracker.h>
22 #include <unordered_map>
25 #include <dali/public-api/signals/callback.h>
26 #include <dali/public-api/signals/signal-slot-observers.h>
31 * @brief Extra struct for callback base cache.
33 struct ConnectionTracker::Impl
38 std::unordered_map<CallbackBase*, SlotObserver*> mCallbackCache;
41 ConnectionTracker::ConnectionTracker()
42 : mCacheImpl(new ConnectionTracker::Impl())
46 ConnectionTracker::~ConnectionTracker()
52 void ConnectionTracker::DisconnectAll()
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)
58 auto& callbackBase = iter->first;
59 auto& slotObserver = iter->second;
61 // Tell the signal that the slot is disconnected
62 slotObserver->SlotDisconnected(callbackBase);
65 mCacheImpl->mCallbackCache.clear();
66 mCacheImpl->mCallbackCache.rehash(0); ///< Note : unordered_map.clear() didn't deallocate memory.
69 void ConnectionTracker::SignalConnected(SlotObserver* slotObserver, CallbackBase* callback)
71 // We can assume that there is no duplicated callback come here
72 mCacheImpl->mCallbackCache[callback] = slotObserver;
75 void ConnectionTracker::SignalDisconnected(SlotObserver* slotObserver, CallbackBase* callback)
77 // Remove from CallbackBase / SlotObserver list
78 const bool isRemoved = mCacheImpl->mCallbackCache.erase(callback);
79 if(DALI_LIKELY(isRemoved))
81 // Disconnection complete
85 DALI_ABORT("Callback lost in SignalDisconnected()");
88 std::size_t ConnectionTracker::GetConnectionCount() const
90 return mCacheImpl->mCallbackCache.size();