[dali_1.0.8] Merge branch 'tizen'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-singleton-service.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include "toolkit-singleton-service.h"
19
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>
24
25 namespace Dali
26 {
27
28
29 namespace Internal
30 {
31 namespace Adaptor
32 {
33
34 /**
35  * Stub for the SingletonService
36  */
37 class SingletonService : public Dali::BaseObject
38 {
39 public:
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;
44
45 private:
46   SingletonService();
47   virtual ~SingletonService();
48
49   // Undefined
50   SingletonService( const SingletonService& );
51   SingletonService& operator=( SingletonService& );
52
53 private:
54
55   typedef std::pair<std::string, BaseHandle> SingletonPair;
56   typedef std::map<std::string, BaseHandle>  SingletonContainer;
57   typedef SingletonContainer::const_iterator SingletonConstIter;
58
59   SingletonContainer mSingletonContainer; ///< The container to look up singleton by its type name
60
61   static Dali::SingletonService mToolkitSingletonService;
62 };
63
64 Dali::SingletonService SingletonService::mToolkitSingletonService;
65
66 Dali::SingletonService SingletonService::New()
67 {
68   return Get();
69 }
70
71 Dali::SingletonService SingletonService::Get()
72 {
73   if( ! mToolkitSingletonService )
74   {
75     mToolkitSingletonService = Dali::SingletonService( new Dali::Internal::Adaptor::SingletonService );
76   }
77   return mToolkitSingletonService;
78 }
79
80 void SingletonService::Register( const std::type_info& info, BaseHandle singleton )
81 {
82   if( singleton )
83   {
84     mSingletonContainer.insert( SingletonPair( info.name(), singleton ) );
85   }
86 }
87
88 BaseHandle SingletonService::GetSingleton( const std::type_info& info ) const
89 {
90   BaseHandle object;
91
92   SingletonConstIter iter = mSingletonContainer.find(info.name());
93   if( iter != mSingletonContainer.end() )
94   {
95     object = ( *iter ).second;
96   }
97
98   return object;
99 }
100
101 SingletonService::SingletonService()
102 : mSingletonContainer()
103 {
104 }
105
106 SingletonService::~SingletonService()
107 {
108 }
109
110 } // namespace Adaptor
111 } // namespace Internal
112
113 ////////////////////////////////////////////////////////////////////////////////////////////////////
114
115 Internal::Adaptor::SingletonService& GetImplementation(Dali::SingletonService& player)
116 {
117   DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
118
119   BaseObject& handle = player.GetBaseObject();
120
121   return static_cast<Internal::Adaptor::SingletonService&>(handle);
122 }
123
124 const Internal::Adaptor::SingletonService& GetImplementation(const Dali::SingletonService& player)
125 {
126   DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
127
128   const BaseObject& handle = player.GetBaseObject();
129
130   return static_cast<const Internal::Adaptor::SingletonService&>(handle);
131 }
132
133 SingletonService::SingletonService()
134 {
135 }
136
137 SingletonService SingletonService::Get()
138 {
139   return Internal::Adaptor::SingletonService::Get();
140 }
141
142 SingletonService::~SingletonService()
143 {
144 }
145
146 void SingletonService::Register( const std::type_info& info, BaseHandle singleton )
147 {
148   GetImplementation( *this ).Register( info, singleton );
149 }
150
151 BaseHandle SingletonService::GetSingleton( const std::type_info& info ) const
152 {
153   return GetImplementation( *this ).GetSingleton( info );
154 }
155
156 SingletonService::SingletonService( Internal::Adaptor::SingletonService* singletonService )
157 : BaseHandle( singletonService )
158 {
159 }
160
161 } // namespace Dali