Added SingletonService, removed Singleton APIs from Adaptor & removed Application...
[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 <slp-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::SlpPlatform::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* )
63 {
64 }
65
66 boost::thread_specific_ptr< SingletonService > gSingletonService( &DummyCleanup );
67 } // unnamed namespace
68
69 Dali::SingletonService SingletonService::New()
70 {
71   Dali::SingletonService singletonService( new SingletonService );
72   return singletonService;
73 }
74
75 Dali::SingletonService SingletonService::Get()
76 {
77   Dali::SingletonService singletonService;
78   if ( gSingletonService.get() )
79   {
80     singletonService = Dali::SingletonService( gSingletonService.get() );
81   }
82   return singletonService;
83 }
84
85 void SingletonService::Register( const std::type_info& info, BaseHandle singleton )
86 {
87   if( singleton )
88   {
89     DALI_LOG_SINGLETON_SERVICE( Debug::General, "Singleton Added: %s\n", info.name() );
90     mSingletonContainer.insert( SingletonPair( info.name(), singleton ) );
91   }
92 }
93
94 BaseHandle SingletonService::GetSingleton( const std::type_info& info ) const
95 {
96   BaseHandle object;
97
98   SingletonConstIter iter = mSingletonContainer.find(info.name());
99   if( iter != mSingletonContainer.end() )
100   {
101     object = ( *iter ).second;
102   }
103
104   return object;
105 }
106
107 SingletonService::SingletonService()
108 : mSingletonContainer()
109 {
110   // Can only have one instance of SingletonService
111   DALI_ASSERT_ALWAYS( !gSingletonService.get() && "Only one instance of SingletonService is allowed");
112
113   gSingletonService.reset( this );
114
115   DALI_LOG_SINGLETON_SERVICE_DIRECT( Debug::Concise, "SingletonService Created\n" );
116 }
117
118 SingletonService::~SingletonService()
119 {
120   gSingletonService.release();
121   DALI_LOG_SINGLETON_SERVICE_DIRECT( Debug::Concise, "SingletonService Destroyed\n" );
122 }
123
124 } // namespace Adaptor
125
126 } // namespace Internal
127
128 } // namespace Dali