[dali_2.3.48] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / events / actor-observer.cpp
1 /*
2  * Copyright (c) 2021 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/integration-api/debug.h>
20 #include <dali/internal/event/actors/actor-impl.h>
21 #include <dali/internal/event/events/actor-observer.h>
22
23 namespace Dali
24 {
25 namespace Internal
26 {
27 namespace
28 {
29 #if defined(DEBUG_ENABLED)
30 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ACTOR_OBSERVER");
31 #endif // defined(DEBUG_ENABLED)
32 } // namespace
33
34 ActorObserver::ActorObserver()
35 : ActorObserver(nullptr)
36 {
37 }
38
39 ActorObserver::ActorObserver(CallbackBase* callback)
40 : mActor(nullptr),
41   mActorDisconnected(false),
42   mRemoveCallback(callback)
43 {
44   DALI_LOG_TRACE_METHOD(gLogFilter);
45 }
46
47 ActorObserver::~ActorObserver()
48 {
49   DALI_LOG_TRACE_METHOD(gLogFilter);
50   SetActor(nullptr);
51
52   delete mRemoveCallback;
53 }
54
55 ActorObserver::ActorObserver(ActorObserver&& other) noexcept
56 : ActorObserver(nullptr)
57 {
58   operator=(std::move(other));
59 }
60
61 ActorObserver& ActorObserver::operator=(ActorObserver&& other) noexcept
62 {
63   if(this != &other)
64   {
65     SetActor(other.mActor);
66     mActorDisconnected = other.mActorDisconnected;
67     mRemoveCallback    = other.mRemoveCallback;
68     other.ResetActor();
69     other.mRemoveCallback = nullptr;
70   }
71   return *this;
72 }
73
74 Actor* ActorObserver::GetActor() const
75 {
76   return mActorDisconnected ? nullptr : mActor;
77 }
78
79 void ActorObserver::SetActor(Actor* actor)
80 {
81   DALI_LOG_TRACE_METHOD(gLogFilter);
82
83   if(mActor != actor)
84   {
85     ResetActor();
86
87     mActor = actor;
88
89     if(mActor)
90     {
91       mActor->AddObserver(*this);
92       DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Start Observing:            %p\n", mActor);
93     }
94   }
95
96   // Make sure this flag is unset (as we may have been disconnected if it's the same actor)
97   mActorDisconnected = false;
98 }
99
100 void ActorObserver::ResetActor()
101 {
102   if(mActor)
103   {
104     DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Stop Observing:             %p\n", mActor);
105     mActor->RemoveObserver(*this);
106     mActor             = nullptr;
107     mActorDisconnected = false;
108   }
109 }
110
111 void ActorObserver::SceneObjectAdded(Object& object)
112 {
113   DALI_LOG_TRACE_METHOD(gLogFilter);
114
115   if(mActor == &object)
116   {
117     mActorDisconnected = false;
118   }
119 }
120
121 void ActorObserver::SceneObjectRemoved(Object& object)
122 {
123   DALI_LOG_TRACE_METHOD(gLogFilter);
124
125   if(mActor == &object)
126   {
127     if(mRemoveCallback)
128     {
129       CallbackBase::Execute(*mRemoveCallback, mActor);
130     }
131
132     // do not call object.RemoveObserver here, object is currently iterating through observers
133     mActorDisconnected = true;
134   }
135 }
136
137 void ActorObserver::ObjectDestroyed(Object& object)
138 {
139   DALI_LOG_TRACE_METHOD(gLogFilter);
140
141   if(mActor == &object)
142   {
143     DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Stop Observing:             %p\n", mActor);
144     mActor = nullptr;
145   }
146 }
147
148 } // namespace Internal
149
150 } // namespace Dali