135a9602e6f76444fb4f48e3175b5e2ac2349d37
[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 <dali/devel-api/adaptor-framework/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   }
79
80   /**
81    * @copydoc Dali::SingletonService::UnregisterAll()
82    */
83   void UnregisterAll()
84   {
85     mSingletonContainer.clear();
86   }
87
88   /**
89    * @copydoc Dali::SingletonService::GetSingleton()
90    */
91   BaseHandle GetSingleton( const std::type_info& info ) const
92   {
93     BaseHandle object;
94
95     SingletonConstIter iter = mSingletonContainer.find(info.name());
96     if( iter != mSingletonContainer.end() )
97     {
98       object = ( *iter ).second;
99     }
100     return object;
101   }
102
103 private:
104
105   /**
106    * Private Constructor
107    * @see SingletonService::New()
108    */
109   SingletonService()
110   : mSingletonContainer()
111   {
112     // Can only have one instance of SingletonService
113     DALI_ASSERT_ALWAYS( !gSingletonService && "Only one instance of SingletonService is allowed");
114     gSingletonService = this;
115   }
116
117   /**
118    * Virtual Destructor
119    */
120   virtual ~SingletonService()
121   {
122     gSingletonService = 0;
123   }
124
125   // Undefined
126   SingletonService( const SingletonService& );
127   SingletonService& operator=( SingletonService& );
128
129 private:
130   typedef std::pair<std::string, BaseHandle> SingletonPair;
131   typedef std::map<std::string, BaseHandle>  SingletonContainer;
132   typedef SingletonContainer::const_iterator SingletonConstIter;
133
134   SingletonContainer mSingletonContainer; ///< The container to look up singleton by its type name
135 };
136
137 } // namespace Adaptor
138 } // namespace Internal
139
140 ////////////////////////////////////////////////////////////////////////////////////////////////////
141
142 inline Internal::Adaptor::SingletonService& GetImplementation(Dali::SingletonService& player)
143 {
144   DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
145   BaseObject& handle = player.GetBaseObject();
146   return static_cast<Internal::Adaptor::SingletonService&>(handle);
147 }
148
149 inline const Internal::Adaptor::SingletonService& GetImplementation(const Dali::SingletonService& player)
150 {
151   DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
152   const BaseObject& handle = player.GetBaseObject();
153   return static_cast<const Internal::Adaptor::SingletonService&>(handle);
154 }
155
156 SingletonService::SingletonService()
157 {
158 }
159
160 SingletonService SingletonService::New()
161 {
162   return Internal::Adaptor::SingletonService::New();
163 }
164
165 SingletonService SingletonService::Get()
166 {
167   return Internal::Adaptor::SingletonService::Get();
168 }
169
170 SingletonService::~SingletonService()
171 {
172 }
173
174 void SingletonService::Register( const std::type_info& info, BaseHandle singleton )
175 {
176   GetImplementation( *this ).Register( info, singleton );
177 }
178
179 void SingletonService::UnregisterAll()
180 {
181   GetImplementation( *this ).UnregisterAll();
182 }
183
184 BaseHandle SingletonService::GetSingleton( const std::type_info& info ) const
185 {
186   return GetImplementation( *this ).GetSingleton( info );
187 }
188
189 SingletonService::SingletonService( Internal::Adaptor::SingletonService* singletonService )
190 : BaseHandle( singletonService )
191 {
192 }
193
194 } // namespace Dali