2 * Copyright (c) 2018 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-toolkit/public-api/dali-toolkit-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>
36 Dali::IntrusivePtr<SingletonService> gSingletonService;
37 } // unnamed namespace
40 class SingletonService : public Dali::BaseObject
45 * Create a SingletonService.
46 * This should only be called once by the Application class.
47 * @return A newly created SingletonService.
49 static Dali::SingletonService New()
51 DALI_ASSERT_ALWAYS( 0 && "SingletonService New method used");
52 gSingletonService = Dali::IntrusivePtr<SingletonService>( new SingletonService() );
53 return Dali::SingletonService( gSingletonService.Get() );
57 * @copydoc Dali::SingletonService::Get()
59 static Dali::SingletonService Get()
61 Dali::SingletonService singletonService;
62 if ( !gSingletonService )
64 gSingletonService = Dali::IntrusivePtr<SingletonService>( new SingletonService() );
66 return Dali::SingletonService( gSingletonService.Get() );
70 * @copydoc Dali::SingletonService::Register()
72 void Register( const std::type_info& info, BaseHandle singleton )
76 mSingletonContainer.insert( SingletonPair( info.name(), singleton ) );
78 Integration::Processor* processor = dynamic_cast<Integration::Processor*>( &singleton.GetBaseObject() );
81 Integration::Core& core = mTestApplication->GetCore();
82 core.RegisterProcessor( *processor );
88 * @copydoc Dali::SingletonService::UnregisterAll()
92 mSingletonContainer.clear();
96 * @copydoc Dali::SingletonService::GetSingleton()
98 BaseHandle GetSingleton( const std::type_info& info ) const
102 SingletonConstIter iter = mSingletonContainer.find(info.name());
103 if( iter != mSingletonContainer.end() )
105 object = ( *iter ).second;
110 void SetApplication( Dali::TestApplication& testApplication )
112 mTestApplication = &testApplication;
118 * Private Constructor
119 * @see SingletonService::New()
122 : mSingletonContainer()
124 // Can only have one instance of SingletonService
125 DALI_ASSERT_ALWAYS( !gSingletonService && "Only one instance of SingletonService is allowed");
126 gSingletonService = this;
132 virtual ~SingletonService()
134 gSingletonService = 0;
138 SingletonService( const SingletonService& );
139 SingletonService& operator=( SingletonService& );
142 typedef std::pair<std::string, BaseHandle> SingletonPair;
143 typedef std::map<std::string, BaseHandle> SingletonContainer;
144 typedef SingletonContainer::const_iterator SingletonConstIter;
146 SingletonContainer mSingletonContainer; ///< The container to look up singleton by its type name
147 TestApplication* mTestApplication;
150 } // namespace Adaptor
151 } // namespace Internal
153 ////////////////////////////////////////////////////////////////////////////////////////////////////
155 inline Internal::Adaptor::SingletonService& GetImplementation(Dali::SingletonService& player)
157 DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
158 BaseObject& handle = player.GetBaseObject();
159 return static_cast<Internal::Adaptor::SingletonService&>(handle);
162 inline const Internal::Adaptor::SingletonService& GetImplementation(const Dali::SingletonService& player)
164 DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
165 const BaseObject& handle = player.GetBaseObject();
166 return static_cast<const Internal::Adaptor::SingletonService&>(handle);
169 SingletonService::SingletonService()
173 SingletonService SingletonService::New()
175 return Internal::Adaptor::SingletonService::New();
178 SingletonService SingletonService::Get()
180 return Internal::Adaptor::SingletonService::Get();
183 SingletonService::~SingletonService()
187 void SingletonService::Register( const std::type_info& info, BaseHandle singleton )
189 GetImplementation( *this ).Register( info, singleton );
192 void SingletonService::UnregisterAll()
194 GetImplementation( *this ).UnregisterAll();
197 BaseHandle SingletonService::GetSingleton( const std::type_info& info ) const
199 return GetImplementation( *this ).GetSingleton( info );
202 SingletonService::SingletonService( Internal::Adaptor::SingletonService* singletonService )
203 : BaseHandle( singletonService )
213 void SetApplication( Dali::SingletonService singletonService, TestApplication& testApplication )
215 GetImplementation( singletonService ).SetApplication( testApplication );