[Tizen] Add codes for Dali Windows Backend
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / singleton-service-impl.cpp
1 /*
2  * Copyright (c) 2018 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 #include <dali/integration-api/core.h>
24 #include <dali/integration-api/adaptor.h>
25 #include <dali/internal/adaptor/common/adaptor-impl.h>
26
27 // INTERNAL INCLUDES
28 #if defined(DEBUG_ENABLED)
29 #include <dali/internal/system/common/logging.h>
30 Debug::Filter* gSingletonServiceLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_SINGLETON_SERVICE" );
31
32 // 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
33 #define DALI_LOG_SINGLETON_SERVICE_DIRECT(level, message)                        \
34     if(gSingletonServiceLogFilter && gSingletonServiceLogFilter->IsEnabledFor(level)) { std::string string(message); Dali::TizenPlatform::LogMessage( Debug::DebugInfo, string );  }
35
36 #define DALI_LOG_SINGLETON_SERVICE(level, format, ...) DALI_LOG_INFO(gSingletonServiceLogFilter, level, format, ## __VA_ARGS__ )
37 #else
38
39 #define DALI_LOG_SINGLETON_SERVICE_DIRECT(level, message)
40 #define DALI_LOG_SINGLETON_SERVICE(level, format, ...)
41
42 #endif
43
44 namespace Dali
45 {
46
47 namespace Internal
48 {
49
50 namespace Adaptor
51 {
52
53 namespace
54 {
55 thread_local SingletonService * gSingletonService = 0;
56 } // unnamed namespace
57
58 Dali::SingletonService SingletonService::New()
59 {
60   Dali::SingletonService singletonService( new SingletonService );
61   return singletonService;
62 }
63
64 Dali::SingletonService SingletonService::Get()
65 {
66   Dali::SingletonService singletonService;
67   if ( gSingletonService )
68   {
69     singletonService = Dali::SingletonService( gSingletonService );
70   }
71   return singletonService;
72 }
73
74 void SingletonService::Register( const std::type_info& info, BaseHandle singleton )
75 {
76   if( singleton )
77   {
78     DALI_LOG_SINGLETON_SERVICE( Debug::General, "Singleton Added: %s\n", info.name() );
79     mSingletonContainer.push_back( SingletonPair( info.name(), singleton ) );
80
81     Integration::Processor* processor = dynamic_cast<Integration::Processor*>( &singleton.GetBaseObject() );
82     if( processor )
83     {
84       Dali::Adaptor& adaptor = Dali::Adaptor::Get();
85       Dali::Internal::Adaptor::Adaptor& adaptorImpl = Adaptor::GetImplementation( adaptor );
86       Integration::Core& core = adaptorImpl.GetCore();
87       core.RegisterProcessor( *processor );
88     }
89   }
90 }
91
92 void SingletonService::UnregisterAll( )
93 {
94   mSingletonContainer.clear();
95 }
96
97 BaseHandle SingletonService::GetSingleton( const std::type_info& info ) const
98 {
99   BaseHandle object;
100
101   const SingletonContainer::const_iterator end = mSingletonContainer.end();
102   for( SingletonContainer::const_iterator iter = mSingletonContainer.begin(); iter != end; ++iter )
103   {
104     // comparing the addresses as these are allocated statically per library
105     if( ( *iter ).first == info.name() )
106     {
107       object = ( *iter ).second;
108     }
109   }
110
111   return object;
112 }
113
114 SingletonService::SingletonService()
115 : mSingletonContainer()
116 {
117   // Can only have one instance of SingletonService
118   DALI_ASSERT_ALWAYS( !gSingletonService && "Only one instance of SingletonService is allowed");
119
120   gSingletonService = this;
121
122   DALI_LOG_SINGLETON_SERVICE_DIRECT( Debug::Concise, "SingletonService Created\n" );
123 }
124
125 SingletonService::~SingletonService()
126 {
127   gSingletonService = 0;
128
129   DALI_LOG_SINGLETON_SERVICE_DIRECT( Debug::Concise, "SingletonService Destroyed\n" );
130 }
131
132 } // namespace Adaptor
133
134 } // namespace Internal
135
136 } // namespace Dali