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