2 * Copyright (c) 2014 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/base-signal.h>
22 #include <algorithm> // remove_if
25 #include <dali/integration-api/debug.h>
30 const int INVALID_CALLBACK_INDEX = -1;
32 // Predicate for std::remove_if algorithm
33 bool IsNullPredicate(void* ptr)
38 } // unnamed namespace
43 BaseSignal::BaseSignal()
44 : mEmittingFlag( false )
48 BaseSignal::~BaseSignal()
50 // We can't assert in a destructor
53 DALI_LOG_ERROR( "Invalid destruction of Signal during Emit()\n" );
56 // The signal is being destroyed. We have to inform any slots
57 // that are connected, that the signal is dead.
58 const std::size_t count( mSignalConnections.Count() );
59 for( std::size_t i=0; i < count; i++ )
61 SignalConnection* connection = mSignalConnections[ i ];
63 // Note that values are set to NULL in DeleteConnection
66 connection->Disconnect( this );
71 mSignalConnections.Clear();
74 bool BaseSignal::Empty() const
76 return ( 0 == GetConnectionCount() );
79 std::size_t BaseSignal::GetConnectionCount() const
81 std::size_t count( 0 );
83 const std::size_t size( mSignalConnections.Count() );
84 for( std::size_t i = 0; i < size; ++i )
86 // Note that values are set to NULL in DeleteConnection
87 if ( NULL != mSignalConnections[i] )
96 void BaseSignal::Emit()
98 // Guards against nested Emit() calls
99 EmitGuard guard( mEmittingFlag ); // Guards against nested Emit() calls
100 if( guard.ErrorOccurred() )
105 // If more connections are added by callbacks, these are ignore until the next Emit()
106 // Note that mSignalConnections.Count() count cannot be reduced while iterating
107 const std::size_t initialCount( mSignalConnections.Count() );
109 for( std::size_t i = 0; i < initialCount; ++i )
111 CallbackBase* callback( GetCallback(i) );
113 // Note that connections will be set to NULL when disconnected
114 // This is preferable to reducing the connection count while iterating
117 CallbackBase::Execute( *callback );
121 // Cleanup NULL values from Connection container
122 CleanupConnections();
125 void BaseSignal::OnConnect( CallbackBase* callback )
127 DALI_ASSERT_ALWAYS( NULL != callback && "Invalid member function pointer passed to Connect()" );
129 int index = FindCallback( callback );
131 // Don't double-connect the same callback
132 if( INVALID_CALLBACK_INDEX == index )
134 // create a new signal connection object, to allow the signal to track the connection.
135 SignalConnection* connection = new SignalConnection( callback );
137 mSignalConnections.PushBack( connection );
146 void BaseSignal::OnDisconnect( CallbackBase* callback )
148 DALI_ASSERT_ALWAYS( NULL != callback && "Invalid member function pointer passed to Disconnect()" );
150 int index = FindCallback( callback );
152 if( index > INVALID_CALLBACK_INDEX )
154 DeleteConnection( index );
157 // call back is a temporary created to find which slot should be disconnected.
161 void BaseSignal::OnConnect( ConnectionTrackerInterface* tracker, CallbackBase* callback )
163 DALI_ASSERT_ALWAYS( NULL != tracker && "Invalid ConnectionTrackerInterface pointer passed to Connect()" );
164 DALI_ASSERT_ALWAYS( NULL != callback && "Invalid member function pointer passed to Connect()" );
166 int index = FindCallback( callback );
168 // Don't double-connect the same callback
169 if( INVALID_CALLBACK_INDEX == index )
171 // create a new signal connection object, to allow the signal to track the connection.
172 SignalConnection* connection = new SignalConnection( tracker, callback );
174 mSignalConnections.PushBack( connection );
176 // Let the connection tracker know that a connection between a signal and a slot has been made.
177 tracker->SignalConnected( this, callback );
186 void BaseSignal::OnDisconnect( ConnectionTrackerInterface* tracker, CallbackBase* callback )
188 DALI_ASSERT_ALWAYS( NULL != tracker && "Invalid ConnectionTrackerInterface pointer passed to Disconnect()" );
189 DALI_ASSERT_ALWAYS( NULL != callback && "Invalid member function pointer passed to Disconnect()" );
191 int index = FindCallback( callback );
193 if( index > INVALID_CALLBACK_INDEX )
195 // temporary pointer to disconnected callback
196 CallbackBase* disconnectedCallback = mSignalConnections[index]->GetCallback();
198 // close the signal side connection first.
199 DeleteConnection( index );
201 // close the slot side connection
202 tracker->SignalDisconnected( this, disconnectedCallback );
205 // call back is a temporary created to find which slot should be disconnected.
209 // for SlotObserver::SlotDisconnected
210 void BaseSignal::SlotDisconnected( CallbackBase* callback )
212 const std::size_t count( mSignalConnections.Count() );
213 for( std::size_t i=0; i < count; ++i )
215 const CallbackBase* connectionCallback = GetCallback( i );
217 // Pointer comparison i.e. SignalConnection contains pointer to same callback instance
218 if( connectionCallback &&
219 connectionCallback == callback )
221 DeleteConnection( i );
223 // Disconnection complete
228 DALI_ASSERT_ALWAYS( false && "Callback lost in SlotDisconnected()" );
231 CallbackBase* BaseSignal::GetCallback( std::size_t connectionIndex ) const
233 DALI_ASSERT_ALWAYS( connectionIndex < mSignalConnections.Count() && "GetCallback called with invalid index" );
235 CallbackBase* callback( NULL );
237 SignalConnection* connection( mSignalConnections[ connectionIndex ] );
239 // Note that values are set to NULL in DeleteConnection
242 callback = connection->GetCallback();
248 int BaseSignal::FindCallback( CallbackBase* callback )
250 int index( INVALID_CALLBACK_INDEX );
252 // A signal can have multiple slots connected to it.
253 // We need to search for the slot which has the same call back function (if it's static)
254 // Or the same object / member function (for non-static)
255 const std::size_t count( mSignalConnections.Count() );
256 for( std::size_t i=0; i < count; ++i )
258 const CallbackBase* connectionCallback = GetCallback( i );
260 // Note that values are set to NULL in DeleteConnection
261 if( connectionCallback &&
262 ( *connectionCallback == *callback ) )
272 void BaseSignal::DeleteConnection( std::size_t connectionIndex )
274 DALI_ASSERT_ALWAYS( connectionIndex < mSignalConnections.Count() && "DeleteConnection called with invalid index" );
277 SignalConnection* connection( mSignalConnections[ connectionIndex ] );
280 // IMPORTANT - do not remove from items from mSignalConnections, set to NULL instead.
281 // Signal Emit() methods require that connection count is not reduced while iterating
282 // i.e. DeleteConnection can be called from within callbacks, while iterating through mSignalConnections.
283 mSignalConnections[ connectionIndex ] = NULL;
286 void BaseSignal::CleanupConnections()
288 const std::size_t total = mSignalConnections.Count();
289 // only do something if there are items
292 std::size_t index = 0;
293 // process the whole vector
294 for( std::size_t i = 0; i < total; ++i )
296 if( mSignalConnections[ index ] == NULL )
298 // items will be moved so don't increase index (erase will decrease the count of vector)
299 mSignalConnections.Erase( mSignalConnections.Begin() + index );
303 // increase to next element
310 // BaseSignal::EmitGuard
312 BaseSignal::EmitGuard::EmitGuard( bool& flag )
322 // mFlag is NULL when Emit() is called during Emit()
323 DALI_LOG_ERROR( "Cannot call Emit() from inside Emit()" );
327 BaseSignal::EmitGuard::~EmitGuard()
335 bool BaseSignal::EmitGuard::ErrorOccurred()
337 // mFlag is NULL when Emit() is called during Emit()
338 return (NULL == mFlag);