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.
18 #include "toolkit-singleton-service.h"
20 #include <dali/public-api/common/dali-common.h>
21 #include <dali/public-api/object/base-object.h>
22 #include <dali/devel-api/common/map-wrapper.h>
23 #include <dali/public-api/signals/dali-signal.h>
35 * Stub for the SingletonService
37 class SingletonService : public Dali::BaseObject
40 static Dali::SingletonService New();
41 static Dali::SingletonService Get();
42 void Register( const std::type_info& info, BaseHandle singleton );
44 BaseHandle GetSingleton( const std::type_info& info ) const;
48 virtual ~SingletonService();
51 SingletonService( const SingletonService& );
52 SingletonService& operator=( SingletonService& );
56 typedef std::pair<std::string, BaseHandle> SingletonPair;
57 typedef std::map<std::string, BaseHandle> SingletonContainer;
58 typedef SingletonContainer::const_iterator SingletonConstIter;
60 SingletonContainer mSingletonContainer; ///< The container to look up singleton by its type name
62 static Dali::SingletonService mToolkitSingletonService;
65 Dali::SingletonService SingletonService::mToolkitSingletonService;
67 Dali::SingletonService SingletonService::New()
72 Dali::SingletonService SingletonService::Get()
74 if( ! mToolkitSingletonService )
76 mToolkitSingletonService = Dali::SingletonService( new Dali::Internal::Adaptor::SingletonService );
78 return mToolkitSingletonService;
81 void SingletonService::Register( const std::type_info& info, BaseHandle singleton )
85 mSingletonContainer.insert( SingletonPair( info.name(), singleton ) );
89 void SingletonService::UnregisterAll()
91 mSingletonContainer.clear();
94 BaseHandle SingletonService::GetSingleton( const std::type_info& info ) const
98 SingletonConstIter iter = mSingletonContainer.find(info.name());
99 if( iter != mSingletonContainer.end() )
101 object = ( *iter ).second;
107 SingletonService::SingletonService()
108 : mSingletonContainer()
112 SingletonService::~SingletonService()
116 } // namespace Adaptor
117 } // namespace Internal
119 ////////////////////////////////////////////////////////////////////////////////////////////////////
121 Internal::Adaptor::SingletonService& GetImplementation(Dali::SingletonService& player)
123 DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
125 BaseObject& handle = player.GetBaseObject();
127 return static_cast<Internal::Adaptor::SingletonService&>(handle);
130 const Internal::Adaptor::SingletonService& GetImplementation(const Dali::SingletonService& player)
132 DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
134 const BaseObject& handle = player.GetBaseObject();
136 return static_cast<const Internal::Adaptor::SingletonService&>(handle);
139 SingletonService::SingletonService()
143 SingletonService SingletonService::New()
145 return Internal::Adaptor::SingletonService::New();
148 SingletonService SingletonService::Get()
150 return Internal::Adaptor::SingletonService::Get();
153 SingletonService::~SingletonService()
157 void SingletonService::Register( const std::type_info& info, BaseHandle singleton )
159 GetImplementation( *this ).Register( info, singleton );
162 void SingletonService::UnregisterAll()
164 GetImplementation( *this ).UnregisterAll();
167 BaseHandle SingletonService::GetSingleton( const std::type_info& info ) const
169 return GetImplementation( *this ).GetSingleton( info );
172 SingletonService::SingletonService( Internal::Adaptor::SingletonService* singletonService )
173 : BaseHandle( singletonService )