8ab6bda1c138afd2910335da2e33e6656b7953ec
[platform/core/uifw/dali-adaptor.git] / adaptors / common / singleton-service-impl.cpp
1 /*
2  * Copyright (c) 2014 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 // CLASS HEADER
19 #include <singleton-service-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
25 #if defined(DEBUG_ENABLED)
26 #include <tizen-logging.h>
27 Debug::Filter* gSingletonServiceLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_SINGLETON_SERVICE" );
28
29 // Need to define own macro as the log function is not installed when this object is created so no logging is shown with DALI_LOG_INFO at construction and destruction
30 #define DALI_LOG_SINGLETON_SERVICE_DIRECT(level, message)                        \
31     if(gSingletonServiceLogFilter && gSingletonServiceLogFilter->IsEnabledFor(level)) { std::string string(message); Dali::TizenPlatform::LogMessage( Debug::DebugInfo, string );  }
32
33 #define DALI_LOG_SINGLETON_SERVICE(level, format, args...) DALI_LOG_INFO(gSingletonServiceLogFilter, level, format, ## args )
34
35 #else
36
37 #define DALI_LOG_SINGLETON_SERVICE_DIRECT(level, message)
38 #define DALI_LOG_SINGLETON_SERVICE(level, format, args...)
39
40 #endif
41
42 namespace Dali
43 {
44
45 namespace Internal
46 {
47
48 namespace Adaptor
49 {
50
51 // @todo Start using pthread_key_create if we want to avoid leaking the SingletonService on shutdown
52 namespace
53 {
54 __thread SingletonService * gSingletonService = 0;
55 } // unnamed namespace
56
57 Dali::SingletonService SingletonService::New()
58 {
59   Dali::SingletonService singletonService( new SingletonService );
60   return singletonService;
61 }
62
63 Dali::SingletonService SingletonService::Get()
64 {
65   Dali::SingletonService singletonService;
66   if ( gSingletonService )
67   {
68     singletonService = Dali::SingletonService( gSingletonService );
69   }
70   return singletonService;
71 }
72
73 void SingletonService::Register( const std::type_info& info, BaseHandle singleton )
74 {
75   if( singleton )
76   {
77     DALI_LOG_SINGLETON_SERVICE( Debug::General, "Singleton Added: %s\n", info.name() );
78     mSingletonContainer.insert( SingletonPair( info.name(), singleton ) );
79   }
80 }
81
82 void SingletonService::UnregisterAll( )
83 {
84   mSingletonContainer.clear();
85 }
86
87 BaseHandle SingletonService::GetSingleton( const std::type_info& info ) const
88 {
89   BaseHandle object;
90
91   SingletonConstIter iter = mSingletonContainer.find(info.name());
92   if( iter != mSingletonContainer.end() )
93   {
94     object = ( *iter ).second;
95   }
96
97   return object;
98 }
99
100 SingletonService::SingletonService()
101 : mSingletonContainer()
102 {
103   // Can only have one instance of SingletonService
104   DALI_ASSERT_ALWAYS( !gSingletonService && "Only one instance of SingletonService is allowed");
105
106   gSingletonService = this;
107
108   DALI_LOG_SINGLETON_SERVICE_DIRECT( Debug::Concise, "SingletonService Created\n" );
109 }
110
111 SingletonService::~SingletonService()
112 {
113   gSingletonService = 0;
114
115   DALI_LOG_SINGLETON_SERVICE_DIRECT( Debug::Concise, "SingletonService Destroyed\n" );
116 }
117
118 } // namespace Adaptor
119
120 } // namespace Internal
121
122 } // namespace Dali
123