2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <singleton-service-impl.h>
22 #include <boost/thread/tss.hpp>
23 #include <dali/integration-api/debug.h>
27 #if defined(DEBUG_ENABLED)
28 #include <slp-logging.h>
29 Debug::Filter* gSingletonServiceLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_SINGLETON_SERVICE" );
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 ); }
35 #define DALI_LOG_SINGLETON_SERVICE(level, format, args...) DALI_LOG_INFO(gSingletonServiceLogFilter, level, format, ## args )
39 #define DALI_LOG_SINGLETON_SERVICE_DIRECT(level, message)
40 #define DALI_LOG_SINGLETON_SERVICE(level, format, args...)
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.
60 * We do not expect this to be called as we only release the pointer, and not reset.
62 void DummyCleanup( SingletonService* )
66 boost::thread_specific_ptr< SingletonService > gSingletonService( &DummyCleanup );
67 } // unnamed namespace
69 Dali::SingletonService SingletonService::New()
71 Dali::SingletonService singletonService( new SingletonService );
72 return singletonService;
75 Dali::SingletonService SingletonService::Get()
77 Dali::SingletonService singletonService;
78 if ( gSingletonService.get() )
80 singletonService = Dali::SingletonService( gSingletonService.get() );
82 return singletonService;
85 void SingletonService::Register( const std::type_info& info, BaseHandle singleton )
89 DALI_LOG_SINGLETON_SERVICE( Debug::General, "Singleton Added: %s\n", info.name() );
90 mSingletonContainer.insert( SingletonPair( info.name(), singleton ) );
94 BaseHandle SingletonService::GetSingleton( const std::type_info& info ) const
98 SingletonConstIter iter = mSingletonContainer.find(info.name());
99 if( iter != mSingletonContainer.end() )
101 object = ( *iter ).second;
107 SingletonService::SingletonService()
108 : mSingletonContainer()
110 // Can only have one instance of SingletonService
111 DALI_ASSERT_ALWAYS( !gSingletonService.get() && "Only one instance of SingletonService is allowed");
113 gSingletonService.reset( this );
115 DALI_LOG_SINGLETON_SERVICE_DIRECT( Debug::Concise, "SingletonService Created\n" );
118 SingletonService::~SingletonService()
120 gSingletonService.release();
121 DALI_LOG_SINGLETON_SERVICE_DIRECT( Debug::Concise, "SingletonService Destroyed\n" );
124 } // namespace Adaptor
126 } // namespace Internal