2 * Copyright (c) 2016 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 <dali/devel-api/adaptor-framework/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>
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 ) );
81 * @copydoc Dali::SingletonService::UnregisterAll()
85 mSingletonContainer.clear();
89 * @copydoc Dali::SingletonService::GetSingleton()
91 BaseHandle GetSingleton( const std::type_info& info ) const
95 SingletonConstIter iter = mSingletonContainer.find(info.name());
96 if( iter != mSingletonContainer.end() )
98 object = ( *iter ).second;
106 * Private Constructor
107 * @see SingletonService::New()
110 : mSingletonContainer()
112 // Can only have one instance of SingletonService
113 DALI_ASSERT_ALWAYS( !gSingletonService && "Only one instance of SingletonService is allowed");
114 gSingletonService = this;
120 virtual ~SingletonService()
122 gSingletonService = 0;
126 SingletonService( const SingletonService& );
127 SingletonService& operator=( SingletonService& );
130 typedef std::pair<std::string, BaseHandle> SingletonPair;
131 typedef std::map<std::string, BaseHandle> SingletonContainer;
132 typedef SingletonContainer::const_iterator SingletonConstIter;
134 SingletonContainer mSingletonContainer; ///< The container to look up singleton by its type name
137 } // namespace Adaptor
138 } // namespace Internal
140 ////////////////////////////////////////////////////////////////////////////////////////////////////
142 inline Internal::Adaptor::SingletonService& GetImplementation(Dali::SingletonService& player)
144 DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
145 BaseObject& handle = player.GetBaseObject();
146 return static_cast<Internal::Adaptor::SingletonService&>(handle);
149 inline const Internal::Adaptor::SingletonService& GetImplementation(const Dali::SingletonService& player)
151 DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
152 const BaseObject& handle = player.GetBaseObject();
153 return static_cast<const Internal::Adaptor::SingletonService&>(handle);
156 SingletonService::SingletonService()
160 SingletonService SingletonService::New()
162 return Internal::Adaptor::SingletonService::New();
165 SingletonService SingletonService::Get()
167 return Internal::Adaptor::SingletonService::Get();
170 SingletonService::~SingletonService()
174 void SingletonService::Register( const std::type_info& info, BaseHandle singleton )
176 GetImplementation( *this ).Register( info, singleton );
179 void SingletonService::UnregisterAll()
181 GetImplementation( *this ).UnregisterAll();
184 BaseHandle SingletonService::GetSingleton( const std::type_info& info ) const
186 return GetImplementation( *this ).GetSingleton( info );
189 SingletonService::SingletonService( Internal::Adaptor::SingletonService* singletonService )
190 : BaseHandle( singletonService )