Added missing newline chars to logging commands
[platform/core/uifw/dali-core.git] / dali / public-api / signals / base-signal.cpp
index 58ea72a..8d759e7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -29,12 +29,6 @@ namespace
 
 const int INVALID_CALLBACK_INDEX = -1;
 
-// Predicate for std::remove_if algorithm
-bool IsNullPredicate(void* ptr)
-{
-  return ptr == NULL;
-}
-
 } // unnamed namespace
 
 namespace Dali
@@ -55,7 +49,8 @@ BaseSignal::~BaseSignal()
 
   // The signal is being destroyed. We have to inform any slots
   // that are connected, that the signal is dead.
-  for( std::size_t i=0; i < mSignalConnections.size(); i++ )
+  const std::size_t count( mSignalConnections.Count() );
+  for( std::size_t i=0; i < count; i++ )
   {
     SignalConnection* connection = mSignalConnections[ i ];
 
@@ -67,7 +62,7 @@ BaseSignal::~BaseSignal()
     }
   }
 
-  mSignalConnections.clear();
+  mSignalConnections.Clear();
 }
 
 bool BaseSignal::Empty() const
@@ -79,7 +74,7 @@ std::size_t BaseSignal::GetConnectionCount() const
 {
   std::size_t count( 0 );
 
-  const std::size_t size( mSignalConnections.size() );
+  const std::size_t size( mSignalConnections.Count() );
   for( std::size_t i = 0; i < size; ++i )
   {
     // Note that values are set to NULL in DeleteConnection
@@ -102,8 +97,8 @@ void BaseSignal::Emit()
   }
 
   // If more connections are added by callbacks, these are ignore until the next Emit()
-  // Note that mSignalConnections.size() count cannot be reduced while iterating
-  const std::size_t initialCount( mSignalConnections.size() );
+  // Note that mSignalConnections.Count() count cannot be reduced while iterating
+  const std::size_t initialCount( mSignalConnections.Count() );
 
   for( std::size_t i = 0; i < initialCount; ++i )
   {
@@ -133,7 +128,7 @@ void BaseSignal::OnConnect( CallbackBase* callback )
     // create a new signal connection object, to allow the signal to track the connection.
     SignalConnection* connection = new SignalConnection( callback );
 
-    mSignalConnections.push_back( connection );
+    mSignalConnections.PushBack( connection );
   }
   else
   {
@@ -170,7 +165,7 @@ void BaseSignal::OnConnect( ConnectionTrackerInterface* tracker, CallbackBase* c
     // create a new signal connection object, to allow the signal to track the connection.
     SignalConnection* connection = new SignalConnection( tracker, callback );
 
-    mSignalConnections.push_back( connection );
+    mSignalConnections.PushBack( connection );
 
     // Let the connection tracker know that a connection between a signal and a slot has been made.
     tracker->SignalConnected( this, callback );
@@ -208,7 +203,8 @@ void BaseSignal::OnDisconnect( ConnectionTrackerInterface* tracker, CallbackBase
 // for SlotObserver::SlotDisconnected
 void BaseSignal::SlotDisconnected( CallbackBase* callback )
 {
-  for( std::size_t i=0; i < mSignalConnections.size(); ++i )
+  const std::size_t count( mSignalConnections.Count() );
+  for( std::size_t i=0; i < count; ++i )
   {
     const CallbackBase* connectionCallback = GetCallback( i );
 
@@ -228,7 +224,7 @@ void BaseSignal::SlotDisconnected( CallbackBase* callback )
 
 CallbackBase* BaseSignal::GetCallback( std::size_t connectionIndex ) const
 {
-  DALI_ASSERT_ALWAYS( connectionIndex < mSignalConnections.size() && "GetCallback called with invalid index" );
+  DALI_ASSERT_ALWAYS( connectionIndex < mSignalConnections.Count() && "GetCallback called with invalid index" );
 
   CallbackBase* callback( NULL );
 
@@ -250,8 +246,8 @@ int BaseSignal::FindCallback( CallbackBase* callback )
   // A signal can have multiple slots connected to it.
   // We need to search for the slot which has the same call back function (if it's static)
   // Or the same object / member function (for non-static)
-
-  for( std::size_t i=0; i < mSignalConnections.size(); ++i )
+  const std::size_t count( mSignalConnections.Count() );
+  for( std::size_t i=0; i < count; ++i )
   {
     const CallbackBase* connectionCallback = GetCallback( i );
 
@@ -269,7 +265,7 @@ int BaseSignal::FindCallback( CallbackBase* callback )
 
 void BaseSignal::DeleteConnection( std::size_t connectionIndex )
 {
-  DALI_ASSERT_ALWAYS( connectionIndex < mSignalConnections.size() && "DeleteConnection called with invalid index" );
+  DALI_ASSERT_ALWAYS( connectionIndex < mSignalConnections.Count() && "DeleteConnection called with invalid index" );
 
   // delete the object
   SignalConnection* connection( mSignalConnections[ connectionIndex ] );
@@ -283,11 +279,26 @@ void BaseSignal::DeleteConnection( std::size_t connectionIndex )
 
 void BaseSignal::CleanupConnections()
 {
-  // Move NULL pointers to the end...
-  std::vector< SignalConnection* >::iterator endIter = remove_if( mSignalConnections.begin(), mSignalConnections.end(), IsNullPredicate );
-
-  // ...and remove them
-  mSignalConnections.erase( endIter, mSignalConnections.end() );
+  const std::size_t total = mSignalConnections.Count();
+  // only do something if there are items
+  if( total > 0 )
+  {
+    std::size_t index = 0;
+    // process the whole vector
+    for( std::size_t i = 0; i < total; ++i )
+    {
+      if( mSignalConnections[ index ] == NULL )
+      {
+        // items will be moved so don't increase index (erase will decrease the count of vector)
+        mSignalConnections.Erase( mSignalConnections.Begin() + index );
+      }
+      else
+      {
+        // increase to next element
+        ++index;
+      }
+    }
+  }
 }
 
 // BaseSignal::EmitGuard
@@ -303,7 +314,7 @@ BaseSignal::EmitGuard::EmitGuard( bool& flag )
   else
   {
     // mFlag is NULL when Emit() is called during Emit()
-    DALI_LOG_ERROR( "Cannot call Emit() from inside Emit()" );
+    DALI_LOG_ERROR( "Cannot call Emit() from inside Emit()\n" );
   }
 }