Adaptor refactor
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / singleton-service-impl.cpp
1 /*
2  * Copyright (c) 2017 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 <dali/internal/system/common/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 <dali/internal/system/common/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 namespace
52 {
53 thread_local SingletonService * gSingletonService = 0;
54 } // unnamed namespace
55
56 Dali::SingletonService SingletonService::New()
57 {
58   Dali::SingletonService singletonService( new SingletonService );
59   return singletonService;
60 }
61
62 Dali::SingletonService SingletonService::Get()
63 {
64   Dali::SingletonService singletonService;
65   if ( gSingletonService )
66   {
67     singletonService = Dali::SingletonService( gSingletonService );
68   }
69   return singletonService;
70 }
71
72 void SingletonService::Register( const std::type_info& info, BaseHandle singleton )
73 {
74   if( singleton )
75   {
76     DALI_LOG_SINGLETON_SERVICE( Debug::General, "Singleton Added: %s\n", info.name() );
77     mSingletonContainer.push_back( SingletonPair( info.name(), singleton ) );
78   }
79 }
80
81 void SingletonService::UnregisterAll( )
82 {
83   mSingletonContainer.clear();
84 }
85
86 BaseHandle SingletonService::GetSingleton( const std::type_info& info ) const
87 {
88   BaseHandle object;
89
90   const SingletonContainer::const_iterator end = mSingletonContainer.end();
91   for( SingletonContainer::const_iterator iter = mSingletonContainer.begin(); iter != end; ++iter )
92   {
93     // comparing the addresses as these are allocated statically per library
94     if( ( *iter ).first == info.name() )
95     {
96       object = ( *iter ).second;
97     }
98   }
99
100   return object;
101 }
102
103 SingletonService::SingletonService()
104 : mSingletonContainer()
105 {
106   // Can only have one instance of SingletonService
107   DALI_ASSERT_ALWAYS( !gSingletonService && "Only one instance of SingletonService is allowed");
108
109   gSingletonService = this;
110
111   DALI_LOG_SINGLETON_SERVICE_DIRECT( Debug::Concise, "SingletonService Created\n" );
112 }
113
114 SingletonService::~SingletonService()
115 {
116   gSingletonService = 0;
117
118   DALI_LOG_SINGLETON_SERVICE_DIRECT( Debug::Concise, "SingletonService Destroyed\n" );
119 }
120
121 } // namespace Adaptor
122
123 } // namespace Internal
124
125 } // namespace Dali
126