1c49d6d88019fca09d0902c76b525c9d7aff63b8
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / singleton-service-impl.h
1 #ifndef __DALI_INTERNAL_SINGLETON_SERVICE_H__
2 #define __DALI_INTERNAL_SINGLETON_SERVICE_H__
3
4 /*
5  * Copyright (c) 2017 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/object/base-object.h>
23 #include <dali/public-api/common/vector-wrapper.h>
24
25 // INTERNAL INCLUDES
26 #include <dali/devel-api/adaptor-framework/singleton-service.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 namespace Adaptor
35 {
36
37 class SingletonService : public Dali::BaseObject
38 {
39 public:
40
41   /**
42    * Create a SingletonService.
43    * This should only be called once by the Application class.
44    * @return A newly created SingletonService.
45    */
46   static Dali::SingletonService New();
47
48   /**
49    * @copydoc Dali::SingletonService::Get()
50    */
51   static Dali::SingletonService Get();
52
53   /**
54    * @copydoc Dali::SingletonService::Register()
55    */
56   void Register( const std::type_info& info, BaseHandle singleton );
57
58   /**
59    * @copydoc Dali::SingletonService::UnregisterAll()
60    */
61   void UnregisterAll();
62
63   /**
64    * @copydoc Dali::SingletonService::GetSingleton()
65    */
66   BaseHandle GetSingleton( const std::type_info& info ) const;
67
68 private:
69
70   /**
71    * Private Constructor
72    * @see SingletonService::New()
73    */
74   SingletonService();
75
76   /**
77    * Virtual Destructor
78    */
79   virtual ~SingletonService();
80
81   // Undefined
82   SingletonService( const SingletonService& );
83   SingletonService& operator=( SingletonService& );
84
85 private:
86
87   // using the address of the type name string as compiler will allocate these once per library
88   // and we don't support un/re-loading of dali libraries while singleton service is alive
89   typedef std::pair< const char*, BaseHandle> SingletonPair;
90   typedef std::vector< SingletonPair >  SingletonContainer;
91   typedef SingletonContainer::const_iterator SingletonConstIter;
92
93   SingletonContainer mSingletonContainer; ///< The container to look up singleton by its type name
94 };
95
96 } // namespace Adaptor
97
98 } // namespace Internal
99
100 // Helpers for public-api forwarding methods
101
102 inline Internal::Adaptor::SingletonService& GetImplementation(Dali::SingletonService& player)
103 {
104   DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
105
106   BaseObject& handle = player.GetBaseObject();
107
108   return static_cast<Internal::Adaptor::SingletonService&>(handle);
109 }
110
111 inline const Internal::Adaptor::SingletonService& GetImplementation(const Dali::SingletonService& player)
112 {
113   DALI_ASSERT_ALWAYS( player && "SingletonService handle is empty" );
114
115   const BaseObject& handle = player.GetBaseObject();
116
117   return static_cast<const Internal::Adaptor::SingletonService&>(handle);
118 }
119
120 } // namespace Dali
121
122 #endif // __DALI_INTERNAL_SINGLETON_SERVICE_H__