Conversion to Apache 2.0 license
[platform/core/uifw/dali-core.git] / dali / public-api / signals / connection-tracker.cpp
1 /*
2  * Copyright (c) 2014 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 #include <dali/public-api/signals/callback.h>
22 #include <dali/public-api/signals/signal-slot-observers.h>
23 #include <dali/public-api/signals/signal-slot-connections.h>
24
25 namespace Dali
26 {
27
28 namespace
29 {
30
31 const int INVALID_CALLBACK_INDEX = -1;
32
33 } // unnamed namespace
34
35
36 ConnectionTracker::ConnectionTracker()
37 {
38 }
39
40 ConnectionTracker::~ConnectionTracker()
41 {
42   DisconnectAll();
43 }
44
45 void ConnectionTracker::DisconnectAll()
46 {
47   std::size_t size = mConnections.size();
48
49   for( std::size_t i = 0; i< size; ++i )
50   {
51     SlotConnection* connection = mConnections[i];
52
53     // Tell the signal that the slot is disconnected
54     connection->GetSlotObserver()->SlotDisconnected( connection->GetCallback() );
55
56     delete connection;
57   }
58
59   mConnections.clear();
60 }
61
62 void ConnectionTracker::SignalConnected( SlotObserver* slotObserver, CallbackBase* callback )
63 {
64   SlotConnection* connection = new SlotConnection( slotObserver, callback );
65   mConnections.push_back( connection );
66 }
67
68 void ConnectionTracker::SignalDisconnected( SlotObserver* signal, CallbackBase* callback )
69 {
70   std::size_t size = mConnections.size();
71
72   for( std::size_t i = 0; i< size; ++i )
73   {
74     SlotConnection* connection = mConnections[i];
75
76     // Pointer comparison i.e. SignalConnection contains pointer to same callback instance
77     if( connection->GetCallback() == callback )
78     {
79       // Remove from connection list
80       mConnections.erase( mConnections.begin() + i );
81
82       // Delete connection
83       delete connection;
84
85       // Disconnection complete
86       return;
87     }
88   }
89
90   DALI_ASSERT_ALWAYS( false && "Callback lost in SignalDisconnected()" );
91 }
92
93 std::size_t ConnectionTracker::GetConnectionCount() const
94 {
95   return mConnections.size();
96 }
97
98 } // namespace Dali