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/public-api/common/map-wrapper.h>
23 #include <dali/public-api/signals/dali-signal-v2.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 );
43 BaseHandle GetSingleton( const std::type_info& info ) const;
47 virtual ~SingletonService();
50 SingletonService( const SingletonService& );
51 SingletonService& operator=( SingletonService& );
55 typedef std::pair<std::string, BaseHandle> SingletonPair;
56 typedef std::map<std::string, BaseHandle> SingletonContainer;
57 typedef SingletonContainer::const_iterator SingletonConstIter;
59 SingletonContainer mSingletonContainer; ///< The container to look up singleton by its type name
61 static Dali::SingletonService mToolkitSingletonService;
64 Dali::SingletonService SingletonService::mToolkitSingletonService;
66 Dali::SingletonService SingletonService::New()
71 Dali::SingletonService SingletonService::Get()
73 if( ! mToolkitSingletonService )
75 mToolkitSingletonService = Dali::SingletonService( new Dali::Internal::Adaptor::SingletonService );
77 return mToolkitSingletonService;
80 void SingletonService::Register( const std::type_info& info, BaseHandle singleton )
84 mSingletonContainer.insert( SingletonPair( info.name(), singleton ) );
88 BaseHandle SingletonService::GetSingleton( const std::type_info& info ) const
92 SingletonConstIter iter = mSingletonContainer.find(info.name());
93 if( iter != mSingletonContainer.end() )
95 object = ( *iter ).second;
101 SingletonService::SingletonService()
102 : mSingletonContainer()
106 SingletonService::~SingletonService()
110 } // namespace Adaptor
111 } // namespace Internal
113 ////////////////////////////////////////////////////////////////////////////////////////////////////
115 Internal::Adaptor::SingletonService& GetImplementation(Dali::SingletonService& player)
117 DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
119 BaseObject& handle = player.GetBaseObject();
121 return static_cast<Internal::Adaptor::SingletonService&>(handle);
124 const Internal::Adaptor::SingletonService& GetImplementation(const Dali::SingletonService& player)
126 DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
128 const BaseObject& handle = player.GetBaseObject();
130 return static_cast<const Internal::Adaptor::SingletonService&>(handle);
133 SingletonService::SingletonService()
137 SingletonService SingletonService::Get()
139 return Internal::Adaptor::SingletonService::Get();
142 SingletonService::~SingletonService()
146 void SingletonService::Register( const std::type_info& info, BaseHandle singleton )
148 GetImplementation( *this ).Register( info, singleton );
151 BaseHandle SingletonService::GetSingleton( const std::type_info& info ) const
153 return GetImplementation( *this ).GetSingleton( info );
156 SingletonService::SingletonService( Internal::Adaptor::SingletonService* singletonService )
157 : BaseHandle( singletonService )