/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2016 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.
*
*/
-#include "toolkit-singleton-service.h"
+#include <dali/devel-api/adaptor-framework/singleton-service.h>
#include <dali/public-api/common/dali-common.h>
#include <dali/public-api/object/base-object.h>
-#include <dali/public-api/common/map-wrapper.h>
+#include <dali/devel-api/common/map-wrapper.h>
#include <dali/public-api/signals/dali-signal.h>
namespace Dali
namespace Adaptor
{
-/**
- * Stub for the SingletonService
- */
-class SingletonService : public Dali::BaseObject
+namespace
{
-public:
- static Dali::SingletonService New();
- static Dali::SingletonService Get();
- void Register( const std::type_info& info, BaseHandle singleton );
- BaseHandle GetSingleton( const std::type_info& info ) const;
-
-private:
- SingletonService();
- virtual ~SingletonService();
-
- // Undefined
- SingletonService( const SingletonService& );
- SingletonService& operator=( SingletonService& );
-
-private:
+Dali::IntrusivePtr<SingletonService> gSingletonService;
+} // unnamed namespace
- typedef std::pair<std::string, BaseHandle> SingletonPair;
- typedef std::map<std::string, BaseHandle> SingletonContainer;
- typedef SingletonContainer::const_iterator SingletonConstIter;
- SingletonContainer mSingletonContainer; ///< The container to look up singleton by its type name
+class SingletonService : public Dali::BaseObject
+{
+public:
- static Dali::SingletonService mToolkitSingletonService;
-};
+ /**
+ * Create a SingletonService.
+ * This should only be called once by the Application class.
+ * @return A newly created SingletonService.
+ */
+ static Dali::SingletonService New()
+ {
+ DALI_ASSERT_ALWAYS( 0 && "SingletonService New method used");
+ gSingletonService = Dali::IntrusivePtr<SingletonService>( new SingletonService() );
+ return Dali::SingletonService( gSingletonService.Get() );
+ }
-Dali::SingletonService SingletonService::mToolkitSingletonService;
+ /**
+ * @copydoc Dali::SingletonService::Get()
+ */
+ static Dali::SingletonService Get()
+ {
+ Dali::SingletonService singletonService;
+ if ( !gSingletonService )
+ {
+ gSingletonService = Dali::IntrusivePtr<SingletonService>( new SingletonService() );
+ }
+ return Dali::SingletonService( gSingletonService.Get() );
+ }
-Dali::SingletonService SingletonService::New()
-{
- return Get();
-}
+ /**
+ * @copydoc Dali::SingletonService::Register()
+ */
+ void Register( const std::type_info& info, BaseHandle singleton )
+ {
+ if( singleton )
+ {
+ mSingletonContainer.insert( SingletonPair( info.name(), singleton ) );
+ }
+ }
-Dali::SingletonService SingletonService::Get()
-{
- if( ! mToolkitSingletonService )
+ /**
+ * @copydoc Dali::SingletonService::UnregisterAll()
+ */
+ void UnregisterAll()
{
- mToolkitSingletonService = Dali::SingletonService( new Dali::Internal::Adaptor::SingletonService );
+ mSingletonContainer.clear();
}
- return mToolkitSingletonService;
-}
-void SingletonService::Register( const std::type_info& info, BaseHandle singleton )
-{
- if( singleton )
+ /**
+ * @copydoc Dali::SingletonService::GetSingleton()
+ */
+ BaseHandle GetSingleton( const std::type_info& info ) const
{
- mSingletonContainer.insert( SingletonPair( info.name(), singleton ) );
+ BaseHandle object;
+
+ SingletonConstIter iter = mSingletonContainer.find(info.name());
+ if( iter != mSingletonContainer.end() )
+ {
+ object = ( *iter ).second;
+ }
+ return object;
}
-}
-BaseHandle SingletonService::GetSingleton( const std::type_info& info ) const
-{
- BaseHandle object;
+private:
- SingletonConstIter iter = mSingletonContainer.find(info.name());
- if( iter != mSingletonContainer.end() )
+ /**
+ * Private Constructor
+ * @see SingletonService::New()
+ */
+ SingletonService()
+ : mSingletonContainer()
{
- object = ( *iter ).second;
+ // Can only have one instance of SingletonService
+ DALI_ASSERT_ALWAYS( !gSingletonService && "Only one instance of SingletonService is allowed");
+ gSingletonService = this;
}
- return object;
-}
+ /**
+ * Virtual Destructor
+ */
+ virtual ~SingletonService()
+ {
+ gSingletonService = 0;
+ }
-SingletonService::SingletonService()
-: mSingletonContainer()
-{
-}
+ // Undefined
+ SingletonService( const SingletonService& );
+ SingletonService& operator=( SingletonService& );
-SingletonService::~SingletonService()
-{
-}
+private:
+ typedef std::pair<std::string, BaseHandle> SingletonPair;
+ typedef std::map<std::string, BaseHandle> SingletonContainer;
+ typedef SingletonContainer::const_iterator SingletonConstIter;
+
+ SingletonContainer mSingletonContainer; ///< The container to look up singleton by its type name
+};
} // namespace Adaptor
} // namespace Internal
////////////////////////////////////////////////////////////////////////////////////////////////////
-Internal::Adaptor::SingletonService& GetImplementation(Dali::SingletonService& player)
+inline Internal::Adaptor::SingletonService& GetImplementation(Dali::SingletonService& player)
{
DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
-
BaseObject& handle = player.GetBaseObject();
-
return static_cast<Internal::Adaptor::SingletonService&>(handle);
}
-const Internal::Adaptor::SingletonService& GetImplementation(const Dali::SingletonService& player)
+inline const Internal::Adaptor::SingletonService& GetImplementation(const Dali::SingletonService& player)
{
DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
-
const BaseObject& handle = player.GetBaseObject();
-
return static_cast<const Internal::Adaptor::SingletonService&>(handle);
}
{
}
+SingletonService SingletonService::New()
+{
+ return Internal::Adaptor::SingletonService::New();
+}
+
SingletonService SingletonService::Get()
{
return Internal::Adaptor::SingletonService::Get();
GetImplementation( *this ).Register( info, singleton );
}
+void SingletonService::UnregisterAll()
+{
+ GetImplementation( *this ).UnregisterAll();
+}
+
BaseHandle SingletonService::GetSingleton( const std::type_info& info ) const
{
return GetImplementation( *this ).GetSingleton( info );