[Tizen](ATSPI) squashed implementation
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-singleton-service.cpp
1 /*
2  * Copyright (c) 2019 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-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"
26
27 namespace Dali
28 {
29
30
31 namespace Internal
32 {
33 namespace Adaptor
34 {
35
36 namespace
37 {
38 Dali::IntrusivePtr<SingletonService> gSingletonService;
39 } // unnamed namespace
40
41
42 class SingletonService : public Dali::BaseObject
43 {
44 public:
45
46   /**
47    * Create a SingletonService.
48    * This should only be called once by the Application class.
49    * @return A newly created SingletonService.
50    */
51   static Dali::SingletonService New()
52   {
53     DALI_ASSERT_ALWAYS( 0 && "SingletonService New method used");
54     gSingletonService = Dali::IntrusivePtr<SingletonService>( new SingletonService() );
55     return Dali::SingletonService( gSingletonService.Get() );
56   }
57
58   /**
59    * @copydoc Dali::SingletonService::Get()
60    */
61   static Dali::SingletonService Get()
62   {
63     Dali::SingletonService singletonService;
64     if ( !gSingletonService )
65     {
66       gSingletonService = Dali::IntrusivePtr<SingletonService>( new SingletonService() );
67     }
68     return Dali::SingletonService( gSingletonService.Get() );
69   }
70
71   /**
72    * @copydoc Dali::SingletonService::Register()
73    */
74   void Register( const std::type_info& info, BaseHandle singleton )
75   {
76     if( singleton )
77     {
78       mSingletonContainer.insert( SingletonPair( info.name(), singleton ) );
79
80       Integration::Processor* processor = dynamic_cast<Integration::Processor*>( &singleton.GetBaseObject() );
81       if( processor )
82       {
83         Integration::Core& core = mTestApplication->GetCore();
84         core.RegisterProcessor( *processor );
85       }
86     }
87   }
88
89   /**
90    * @copydoc Dali::SingletonService::UnregisterAll()
91    */
92   void UnregisterAll()
93   {
94     mSingletonContainer.clear();
95   }
96
97   /**
98    * @copydoc Dali::SingletonService::GetSingleton()
99    */
100   BaseHandle GetSingleton( const std::type_info& info ) const
101   {
102     BaseHandle object;
103
104     SingletonConstIter iter = mSingletonContainer.find(info.name());
105     if( iter != mSingletonContainer.end() )
106     {
107       object = ( *iter ).second;
108     }
109     return object;
110   }
111
112   void SetApplication( Dali::ToolkitTestApplication& ToolkitTestApplication )
113   {
114     mTestApplication = &ToolkitTestApplication;
115   }
116
117 private:
118
119   /**
120    * Private Constructor
121    * @see SingletonService::New()
122    */
123   SingletonService()
124   : mSingletonContainer()
125   {
126     // Can only have one instance of SingletonService
127     DALI_ASSERT_ALWAYS( !gSingletonService && "Only one instance of SingletonService is allowed");
128     gSingletonService = this;
129   }
130
131   /**
132    * Virtual Destructor
133    */
134   virtual ~SingletonService()
135   {
136     gSingletonService = 0;
137   }
138
139   // Undefined
140   SingletonService( const SingletonService& );
141   SingletonService& operator=( SingletonService& );
142
143 private:
144   typedef std::pair<std::string, BaseHandle> SingletonPair;
145   typedef std::map<std::string, BaseHandle>  SingletonContainer;
146   typedef SingletonContainer::const_iterator SingletonConstIter;
147
148   SingletonContainer mSingletonContainer; ///< The container to look up singleton by its type name
149   ToolkitTestApplication* mTestApplication;
150 };
151
152 } // namespace Adaptor
153 } // namespace Internal
154
155 ////////////////////////////////////////////////////////////////////////////////////////////////////
156
157 inline Internal::Adaptor::SingletonService& GetImplementation(Dali::SingletonService& player)
158 {
159   DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
160   BaseObject& handle = player.GetBaseObject();
161   return static_cast<Internal::Adaptor::SingletonService&>(handle);
162 }
163
164 inline const Internal::Adaptor::SingletonService& GetImplementation(const Dali::SingletonService& player)
165 {
166   DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
167   const BaseObject& handle = player.GetBaseObject();
168   return static_cast<const Internal::Adaptor::SingletonService&>(handle);
169 }
170
171 SingletonService::SingletonService()
172 {
173 }
174
175 SingletonService SingletonService::New()
176 {
177   return Internal::Adaptor::SingletonService::New();
178 }
179
180 SingletonService SingletonService::Get()
181 {
182   return Internal::Adaptor::SingletonService::Get();
183 }
184
185 SingletonService::~SingletonService()
186 {
187 }
188
189 void SingletonService::Register( const std::type_info& info, BaseHandle singleton )
190 {
191   GetImplementation( *this ).Register( info, singleton );
192 }
193
194 void SingletonService::UnregisterAll()
195 {
196   GetImplementation( *this ).UnregisterAll();
197 }
198
199 BaseHandle SingletonService::GetSingleton( const std::type_info& info ) const
200 {
201   return GetImplementation( *this ).GetSingleton( info );
202 }
203
204 SingletonService::SingletonService( Internal::Adaptor::SingletonService* singletonService )
205 : BaseHandle( singletonService )
206 {
207 }
208
209 } // namespace Dali
210
211
212 namespace Test
213 {
214
215 void SetApplication( Dali::SingletonService singletonService, ToolkitTestApplication& ToolkitTestApplication )
216 {
217   GetImplementation( singletonService ).SetApplication( ToolkitTestApplication );
218 }
219
220 } // Test