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>
25 #include "toolkit-test-application.h"
38 Dali::IntrusivePtr<SingletonService> gSingletonService;
39 } // unnamed namespace
42 class SingletonService : public Dali::BaseObject
47 * Create a SingletonService.
48 * This should only be called once by the Application class.
49 * @return A newly created SingletonService.
51 static Dali::SingletonService New()
53 DALI_ASSERT_ALWAYS( 0 && "SingletonService New method used");
54 gSingletonService = Dali::IntrusivePtr<SingletonService>( new SingletonService() );
55 return Dali::SingletonService( gSingletonService.Get() );
59 * @copydoc Dali::SingletonService::Get()
61 static Dali::SingletonService Get()
63 Dali::SingletonService singletonService;
64 if ( !gSingletonService )
66 gSingletonService = Dali::IntrusivePtr<SingletonService>( new SingletonService() );
68 return Dali::SingletonService( gSingletonService.Get() );
72 * @copydoc Dali::SingletonService::Register()
74 void Register( const std::type_info& info, BaseHandle singleton )
78 mSingletonContainer.insert( SingletonPair( info.name(), singleton ) );
80 Integration::Processor* processor = dynamic_cast<Integration::Processor*>( &singleton.GetBaseObject() );
83 Integration::Core& core = mTestApplication->GetCore();
84 core.RegisterProcessor( *processor );
90 * @copydoc Dali::SingletonService::UnregisterAll()
94 mSingletonContainer.clear();
98 * @copydoc Dali::SingletonService::GetSingleton()
100 BaseHandle GetSingleton( const std::type_info& info ) const
104 SingletonConstIter iter = mSingletonContainer.find(info.name());
105 if( iter != mSingletonContainer.end() )
107 object = ( *iter ).second;
112 void SetApplication( Dali::ToolkitTestApplication& ToolkitTestApplication )
114 mTestApplication = &ToolkitTestApplication;
120 * Private Constructor
121 * @see SingletonService::New()
124 : mSingletonContainer()
126 // Can only have one instance of SingletonService
127 DALI_ASSERT_ALWAYS( !gSingletonService && "Only one instance of SingletonService is allowed");
128 gSingletonService = this;
134 virtual ~SingletonService()
136 gSingletonService = 0;
140 SingletonService( const SingletonService& );
141 SingletonService& operator=( SingletonService& );
144 typedef std::pair<std::string, BaseHandle> SingletonPair;
145 typedef std::map<std::string, BaseHandle> SingletonContainer;
146 typedef SingletonContainer::const_iterator SingletonConstIter;
148 SingletonContainer mSingletonContainer; ///< The container to look up singleton by its type name
149 ToolkitTestApplication* mTestApplication;
152 } // namespace Adaptor
153 } // namespace Internal
155 ////////////////////////////////////////////////////////////////////////////////////////////////////
157 inline Internal::Adaptor::SingletonService& GetImplementation(Dali::SingletonService& player)
159 DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
160 BaseObject& handle = player.GetBaseObject();
161 return static_cast<Internal::Adaptor::SingletonService&>(handle);
164 inline const Internal::Adaptor::SingletonService& GetImplementation(const Dali::SingletonService& player)
166 DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
167 const BaseObject& handle = player.GetBaseObject();
168 return static_cast<const Internal::Adaptor::SingletonService&>(handle);
171 SingletonService::SingletonService()
175 SingletonService SingletonService::New()
177 return Internal::Adaptor::SingletonService::New();
180 SingletonService SingletonService::Get()
182 return Internal::Adaptor::SingletonService::Get();
185 SingletonService::~SingletonService()
189 void SingletonService::Register( const std::type_info& info, BaseHandle singleton )
191 GetImplementation( *this ).Register( info, singleton );
194 void SingletonService::UnregisterAll()
196 GetImplementation( *this ).UnregisterAll();
199 BaseHandle SingletonService::GetSingleton( const std::type_info& info ) const
201 return GetImplementation( *this ).GetSingleton( info );
204 SingletonService::SingletonService( Internal::Adaptor::SingletonService* singletonService )
205 : BaseHandle( singletonService )
215 void SetApplication( Dali::SingletonService singletonService, ToolkitTestApplication& ToolkitTestApplication )
217 GetImplementation( singletonService ).SetApplication( ToolkitTestApplication );