Merge "Change "SLP" to "Tizen"" into tizen
[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 <boost/thread/tss.hpp>
23 #include <dali/integration-api/debug.h>
24
25 // INTERNAL INCLUDES
26
27 #if defined(DEBUG_ENABLED)
28 #include <tizen-logging.h>
29 Debug::Filter* gSingletonServiceLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_SINGLETON_SERVICE" );
30
31 // 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
32 #define DALI_LOG_SINGLETON_SERVICE_DIRECT(level, message)                        \
33     if(gSingletonServiceLogFilter && gSingletonServiceLogFilter->IsEnabledFor(level)) { std::string string(message); Dali::TizenPlatform::LogMessage( Debug::DebugInfo, string );  }
34
35 #define DALI_LOG_SINGLETON_SERVICE(level, format, args...) DALI_LOG_INFO(gSingletonServiceLogFilter, level, format, ## args )
36
37 #else
38
39 #define DALI_LOG_SINGLETON_SERVICE_DIRECT(level, message)
40 #define DALI_LOG_SINGLETON_SERVICE(level, format, args...)
41
42 #endif
43
44 namespace Dali
45 {
46
47 namespace Internal
48 {
49
50 namespace Adaptor
51 {
52
53 namespace
54 {
55
56 /*
57  * Dummy cleanup function required as boost::thread_specific_ptr requires access to destructor but
58  * we should not make the destructor of SingletonService public as it is a ref-counted object.
59  *
60  * We do not expect this to be called as we only release the pointer, and not reset.
61  */
62 void DummyCleanup( SingletonService* service )
63 {
64   if ( service )
65   {
66     service->UnregisterAll();
67   }
68 }
69
70 boost::thread_specific_ptr< SingletonService > gSingletonService( &DummyCleanup );
71 } // unnamed namespace
72
73 Dali::SingletonService SingletonService::New()
74 {
75   Dali::SingletonService singletonService( new SingletonService );
76   return singletonService;
77 }
78
79 Dali::SingletonService SingletonService::Get()
80 {
81   Dali::SingletonService singletonService;
82   if ( gSingletonService.get() )
83   {
84     singletonService = Dali::SingletonService( gSingletonService.get() );
85   }
86   return singletonService;
87 }
88
89 void SingletonService::Register( const std::type_info& info, BaseHandle singleton )
90 {
91   if( singleton )
92   {
93     DALI_LOG_SINGLETON_SERVICE( Debug::General, "Singleton Added: %s\n", info.name() );
94     mSingletonContainer.insert( SingletonPair( info.name(), singleton ) );
95   }
96 }
97
98 void SingletonService::UnregisterAll( )
99 {
100   mSingletonContainer.clear();
101 }
102
103 BaseHandle SingletonService::GetSingleton( const std::type_info& info ) const
104 {
105   BaseHandle object;
106
107   SingletonConstIter iter = mSingletonContainer.find(info.name());
108   if( iter != mSingletonContainer.end() )
109   {
110     object = ( *iter ).second;
111   }
112
113   return object;
114 }
115
116 SingletonService::SingletonService()
117 : mSingletonContainer()
118 {
119   // Can only have one instance of SingletonService
120   DALI_ASSERT_ALWAYS( !gSingletonService.get() && "Only one instance of SingletonService is allowed");
121
122   gSingletonService.reset( this );
123
124   DALI_LOG_SINGLETON_SERVICE_DIRECT( Debug::Concise, "SingletonService Created\n" );
125 }
126
127 SingletonService::~SingletonService()
128 {
129   gSingletonService.release();
130   DALI_LOG_SINGLETON_SERVICE_DIRECT( Debug::Concise, "SingletonService Destroyed\n" );
131 }
132
133 } // namespace Adaptor
134
135 } // namespace Internal
136
137 } // namespace Dali