2 * Copyright (c) 2019 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>
24 #include <dali/integration-api/processor-interface.h>
37 Dali::IntrusivePtr<SingletonService> gSingletonService;
38 } // unnamed namespace
41 class SingletonService : public Dali::BaseObject
46 * Create a SingletonService.
47 * This should only be called once by the Application class.
48 * @return A newly created SingletonService.
50 static Dali::SingletonService New()
52 DALI_ASSERT_ALWAYS( 0 && "SingletonService New method used");
53 gSingletonService = Dali::IntrusivePtr<SingletonService>( new SingletonService() );
54 return Dali::SingletonService( gSingletonService.Get() );
58 * @copydoc Dali::SingletonService::Get()
60 static Dali::SingletonService Get()
62 Dali::SingletonService singletonService;
63 if ( !gSingletonService )
65 gSingletonService = Dali::IntrusivePtr<SingletonService>( new SingletonService() );
67 return Dali::SingletonService( gSingletonService.Get() );
71 * @copydoc Dali::SingletonService::Register()
73 void Register( const std::type_info& info, BaseHandle singleton )
77 mSingletonContainer.insert( SingletonPair( info.name(), singleton ) );
79 Integration::Processor* processor = dynamic_cast<Integration::Processor*>( &singleton.GetBaseObject() );
82 Integration::Core& core = mTestApplication->GetCore();
83 core.RegisterProcessor( *processor );
89 * @copydoc Dali::SingletonService::UnregisterAll()
93 mSingletonContainer.clear();
97 * @copydoc Dali::SingletonService::GetSingleton()
99 BaseHandle GetSingleton( const std::type_info& info ) const
103 SingletonConstIter iter = mSingletonContainer.find(info.name());
104 if( iter != mSingletonContainer.end() )
106 object = ( *iter ).second;
111 void SetApplication( Dali::TestApplication& testApplication )
113 mTestApplication = &testApplication;
119 * Private Constructor
120 * @see SingletonService::New()
123 : mSingletonContainer()
125 // Can only have one instance of SingletonService
126 DALI_ASSERT_ALWAYS( !gSingletonService && "Only one instance of SingletonService is allowed");
127 gSingletonService = this;
133 virtual ~SingletonService()
135 gSingletonService = 0;
139 SingletonService( const SingletonService& );
140 SingletonService& operator=( SingletonService& );
143 typedef std::pair<std::string, BaseHandle> SingletonPair;
144 typedef std::map<std::string, BaseHandle> SingletonContainer;
145 typedef SingletonContainer::const_iterator SingletonConstIter;
147 SingletonContainer mSingletonContainer; ///< The container to look up singleton by its type name
148 TestApplication* mTestApplication;
151 } // namespace Adaptor
152 } // namespace Internal
154 ////////////////////////////////////////////////////////////////////////////////////////////////////
156 inline Internal::Adaptor::SingletonService& GetImplementation(Dali::SingletonService& player)
158 DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
159 BaseObject& handle = player.GetBaseObject();
160 return static_cast<Internal::Adaptor::SingletonService&>(handle);
163 inline const Internal::Adaptor::SingletonService& GetImplementation(const Dali::SingletonService& player)
165 DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
166 const BaseObject& handle = player.GetBaseObject();
167 return static_cast<const Internal::Adaptor::SingletonService&>(handle);
170 SingletonService::SingletonService()
174 SingletonService SingletonService::New()
176 return Internal::Adaptor::SingletonService::New();
179 SingletonService SingletonService::Get()
181 return Internal::Adaptor::SingletonService::Get();
184 SingletonService::~SingletonService()
188 void SingletonService::Register( const std::type_info& info, BaseHandle singleton )
190 GetImplementation( *this ).Register( info, singleton );
193 void SingletonService::UnregisterAll()
195 GetImplementation( *this ).UnregisterAll();
198 BaseHandle SingletonService::GetSingleton( const std::type_info& info ) const
200 return GetImplementation( *this ).GetSingleton( info );
203 SingletonService::SingletonService( Internal::Adaptor::SingletonService* singletonService )
204 : BaseHandle( singletonService )
214 void SetApplication( Dali::SingletonService singletonService, TestApplication& testApplication )
216 GetImplementation( singletonService ).SetApplication( testApplication );