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