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>
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::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();
68 void ConnectionTracker::SignalConnected(SlotObserver* slotObserver, CallbackBase* callback)
70 // We can assume that there is no duplicated callback come here
71 mCacheImpl->mCallbackCache[callback] = slotObserver;
74 void ConnectionTracker::SignalDisconnected(SlotObserver* slotObserver, CallbackBase* callback)
76 // Remove from CallbackBase / SlotObserver list
77 const bool isRemoved = mCacheImpl->mCallbackCache.erase(callback);
78 if(DALI_LIKELY(isRemoved))
80 // Disconnection complete
84 DALI_ABORT("Callback lost in SignalDisconnected()");
87 std::size_t ConnectionTracker::GetConnectionCount() const
89 return mCacheImpl->mCallbackCache.size();