Fix surface deletion order issue
[platform/core/uifw/dali-core.git] / dali / internal / event / common / object-connector.h
1 #ifndef DALI_INTERNAL_OBJECT_CONNECTOR_H
2 #define DALI_INTERNAL_OBJECT_CONNECTOR_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/common/intrusive-ptr.h>
23
24 namespace Dali
25 {
26 namespace Internal
27 {
28 /**
29  * @brief Wrapper class which helps managing intrusive pointer assignments and Connect / Disconnect.
30  *
31  * Should be used with objects that implement Dali::Internal::Connectable
32  */
33 template<typename Object>
34 class ObjectConnector
35 {
36 public:
37   using ObjectPtr = IntrusivePtr<Object>;
38
39   /**
40    * @brief Default constructor.
41    */
42   ObjectConnector() = default;
43
44   /**
45    * @brief Destructor.
46    */
47   ~ObjectConnector() = default;
48
49   /**
50    * @brief Copy constructor
51    */
52   ObjectConnector(const ObjectConnector& connector)
53   : mObject(connector.mObject)
54   {
55   }
56
57   /**
58    * @brief Assignment operator
59    */
60   ObjectConnector& operator=(const ObjectConnector& connector)
61   {
62     this->mObject = connector.mObject;
63     return *this;
64   }
65
66   /**
67    * @brief Returns a smart pointer to the object
68    * @return a smart pointer to the object
69    */
70   ObjectPtr Get() const
71   {
72     return mObject;
73   }
74
75   /**
76    * @brief Assigns the object, calling Connect and Disconnect methods accordingly, taking onScene into account.
77    * @param [in] object smart pointer to a object
78    * @param [in] onScene whether the object is used on stage or not
79    */
80   void Set(Object& object, bool onScene)
81   {
82     if(mObject.Get() != &object)
83     {
84       // Disconnect from old object
85       if(mObject && onScene)
86       {
87         mObject->Disconnect();
88       }
89
90       mObject = &object;
91
92       // Connect to new object
93       if(mObject && onScene)
94       {
95         mObject->Connect();
96       }
97     }
98   }
99
100   /**
101    * @brief Manages connection reference count.
102    *
103    * Must be called from owner when connected to the scene.
104    */
105   void OnSceneConnect()
106   {
107     if(mObject)
108     {
109       mObject->Connect();
110     }
111   }
112
113   /**
114    * @brief Manages connection reference count.
115    *
116    * Must be called from owner when disconnecting from the scene.
117    */
118   void OnSceneDisconnect()
119   {
120     if(mObject)
121     {
122       mObject->Disconnect();
123     }
124   }
125
126 private:             //data
127   ObjectPtr mObject; ///< intrusive pointer to the Object. ObjectConnector owns this.
128 };
129
130 } // namespace Internal
131
132 } // namespace Dali
133
134 #endif // DALI_INTERNAL_OBJECT_CONNECTOR_H