Assert when proprety notification is used in a thread other than the main thread
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / constrainer.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/internal/event/animation/constrainer.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/event/animation/constraint-source-impl.h>
23 #include <dali/public-api/animation/constraint.h>
24
25 namespace Dali
26 {
27 namespace Internal
28 {
29 Constrainer::Constrainer()
30 : Object(nullptr) // we don't have our own scene object
31 {
32 }
33
34 Constrainer::~Constrainer()
35 {
36   //Remove all the constraints created by the object
37   uint32_t         tag = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(this)); // taking 32bits of this as tag
38   const ObjectIter end = mObservedObjects.End();
39   for(ObjectIter iter = mObservedObjects.Begin(); iter != end; ++iter)
40   {
41     //Remove Constrainer from the observers list of the object
42     (*iter)->RemoveObserver(*this);
43
44     //Remove constraints
45     (*iter)->RemoveConstraints(tag);
46   }
47 }
48
49 void Constrainer::ObjectDestroyed(Object& object)
50 {
51   //Remove object from the list of observed
52   const ObjectIter end = mObservedObjects.End();
53   for(ObjectIter iter = mObservedObjects.Begin(); iter != end; ++iter)
54   {
55     if(*iter == &object)
56     {
57       mObservedObjects.Erase(iter);
58       return;
59     }
60   }
61 }
62
63 void Constrainer::Remove(Dali::Handle& target)
64 {
65   uint32_t         tag    = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(this)); // taking 32bits of this as tag
66   Object&          object = GetImplementation(target);
67   const ObjectIter end    = mObservedObjects.End();
68   for(ObjectIter iter = mObservedObjects.Begin(); iter != end; ++iter)
69   {
70     if(*iter == &object)
71     {
72       //Stop observing the object
73       (*iter)->RemoveObserver(*this);
74
75       //Remove constraints created in the object
76       target.RemoveConstraints(tag);
77
78       //Remove object from the vector of observed objects
79       mObservedObjects.Erase(iter);
80     }
81   }
82 }
83
84 void Constrainer::Observe(Dali::Handle& handle)
85 {
86   Object& object = GetImplementation(handle);
87
88   //Add the object to the list of observed objects if it is not in it already
89   const ObjectIter end  = mObservedObjects.End();
90   ObjectIter       iter = mObservedObjects.Begin();
91   for(; iter != end; ++iter)
92   {
93     if(*iter == &object)
94     {
95       break;
96     }
97   }
98
99   if(iter == end)
100   {
101     //Start observing the object
102     object.AddObserver(*this);
103
104     //Add object in the observed objects vector
105     mObservedObjects.PushBack(&object);
106   }
107 }
108
109 } // namespace Internal
110
111 } // namespace Dali