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