0335955527e310b159369c0ac07797a6f576bf83
[platform/core/uifw/dali-core.git] / dali / internal / event / common / thread-local-storage.h
1 #ifndef DALI_INTERNAL_THREAD_LOCAL_STORAGE_H
2 #define DALI_INTERNAL_THREAD_LOCAL_STORAGE_H
3
4 /*
5  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/common/vector-wrapper.h>
23
24 // INTERNAL INCLUDES
25 #include <dali/devel-api/common/singleton-service.h>
26 #include <dali/internal/event/common/stage-def.h>
27 #include <dali/internal/event/common/scene-impl.h>
28
29 namespace Dali
30 {
31
32 struct Vector2;
33
34 namespace Integration
35 {
36 class PlatformAbstraction;
37 }
38
39 namespace Internal
40 {
41
42 class Core;
43 class NotificationManager;
44 class ShaderFactory;
45 class GestureEventProcessor;
46 class RelayoutController;
47 class ObjectRegistry;
48 class EventThreadServices;
49
50 namespace SceneGraph
51 {
52 class UpdateManager;
53 }
54
55 /**
56  * Class to store a pointer to core in thread local storage.
57  *
58  */
59 class ThreadLocalStorage : public Dali::BaseObject
60 {
61 public:
62
63   /**
64    * Constructor
65    * Creates the TLS and adds a pointer to core
66    * @param [in] core reference to core
67    */
68   ThreadLocalStorage(Core* core);
69
70   /**
71    * Remove core pointer.
72    * Prevents the core pointer being automatically deleted when the thread exits.
73    */
74   void Remove();
75
76   /**
77    * Get the TLS
78    * @return reference to the TLS
79    */
80   static ThreadLocalStorage& Get();
81
82   /**
83    * @copydoc Dali::SingletonService::Get()
84    */
85   static Dali::SingletonService GetSingletonService();
86
87   /**
88    * Checks if the TLS has been created
89    * @return if the TLS has been created
90    */
91   static bool Created();
92
93   /**
94    * Get a pointer to the TLS or NULL if not initialized
95    * @return pointer to the TLS
96    */
97   static ThreadLocalStorage* GetInternal();
98
99   /**
100    * get platform abstraction
101    * @return reference to core
102    */
103   Dali::Integration::PlatformAbstraction& GetPlatformAbstraction();
104
105   /**
106    * Retrieve the update manager
107    * @return reference to update manager
108    */
109   SceneGraph::UpdateManager& GetUpdateManager();
110
111   /**
112    * Returns the Notification Manager
113    * @return reference to the Notification Manager
114    */
115   NotificationManager& GetNotificationManager();
116
117   /**
118    * Returns the Shader Factory
119    * @return reference to the Shader Factory
120    */
121   ShaderFactory& GetShaderFactory();
122
123   /**
124    * Returns the current stage.
125    * @return A pointer to the current stage.
126    */
127   StagePtr GetCurrentStage();
128
129   /**
130    * Returns the gesture event processor.
131    * @return A reference to the gesture event processor.
132    */
133   GestureEventProcessor& GetGestureEventProcessor();
134
135   /**
136    * Return the relayout controller
137    * @Return Return a reference to the relayout controller
138    */
139   RelayoutController& GetRelayoutController();
140
141   /**
142    * Returns the Object registry.
143    * @return A reference to the Object registry
144    */
145   ObjectRegistry& GetObjectRegistry();
146
147   /**
148    * @brief Gets the event thread services.
149    * @return A reference to the event thread services
150    */
151   EventThreadServices& GetEventThreadServices();
152
153   /**
154    * @brief Gets the property notification manager.
155    * @return A reference to the property notification manager
156    */
157   PropertyNotificationManager& GetPropertyNotificationManager();
158
159   /**
160    * @brief Gets the animation play list.
161    * @return A reference to the animation play list
162    */
163   AnimationPlaylist& GetAnimationPlaylist();
164
165   /**
166    * Add a Scene to the Core.
167    * This is only used by the Scene to add itself to the core when the Scene is created.
168    * @param[in] scene The Scene.
169    */
170   void AddScene( Scene* scene );
171
172   /**
173    * Remove a Scene from the Core.
174    * This is only used by the Scene to remove itself from the core when the Scene is destroyed.
175    * @param[in] scene The Scene.
176    */
177   void RemoveScene( Scene* scene );
178
179   /**
180    * @copydoc Dali::SingletonService::Register()
181    */
182   void Register( const std::type_info& info, BaseHandle singleton );
183
184   /**
185    * @copydoc Dali::SingletonService::UnregisterAll()
186    */
187   void UnregisterAll();
188
189   /**
190    * @copydoc Dali::SingletonService::GetSingleton()
191    */
192   BaseHandle GetSingleton( const std::type_info& info ) const;
193
194 private:
195
196   /**
197    * Virtual Destructor
198    */
199   virtual ~ThreadLocalStorage();
200
201   // Undefined
202   ThreadLocalStorage( const ThreadLocalStorage& );
203   ThreadLocalStorage& operator=( ThreadLocalStorage& );
204
205 private:
206
207   Core* mCore;                                              ///< reference to core
208
209   // using the address of the type name string as compiler will allocate these once per library
210   // and we don't support un/re-loading of dali libraries while singleton service is alive
211   typedef std::pair< const char*, BaseHandle> SingletonPair;
212   typedef std::vector< SingletonPair >  SingletonContainer;
213   typedef SingletonContainer::const_iterator SingletonConstIter;
214
215   SingletonContainer mSingletonContainer; ///< The container to look up singleton by its type name
216
217 };
218
219 } // namespace Internal
220
221 // Helpers for public-api forwarding methods
222
223 inline Internal::ThreadLocalStorage& GetImplementation(Dali::SingletonService& service)
224 {
225   DALI_ASSERT_ALWAYS( service && "SingletonService handle is empty" );
226
227   BaseObject& handle = service.GetBaseObject();
228
229   return static_cast<Internal::ThreadLocalStorage&>(handle);
230 }
231
232 inline const Internal::ThreadLocalStorage& GetImplementation(const Dali::SingletonService& service)
233 {
234   DALI_ASSERT_ALWAYS( service && "SingletonService handle is empty" );
235
236   const BaseObject& handle = service.GetBaseObject();
237
238   return static_cast<const Internal::ThreadLocalStorage&>(handle);
239 }
240
241 } // namespace Dali
242
243 #endif // DALI_INTERNAL_THREAD_LOCAL_STORAGE_H