Clean up the code to build successfully on macOS
[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) 2020 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
27 namespace Internal
28 {
29
30 /**
31  * @brief Wrapper class which helps managing intrusive pointer assignments and Connect / Disconnect.
32  *
33  * Should be used with objects that implement Dali::Internal::Connectable
34  */
35 template<typename Object>
36 class ObjectConnector
37 {
38 public:
39   using ObjectPtr = IntrusivePtr<Object>;
40
41   /**
42    * @brief Default constructor.
43    */
44   ObjectConnector() {}
45
46   /**
47    * @brief Destructor.
48    */
49   ~ObjectConnector() {}
50
51   /**
52    * @brief Copy constructor
53    */
54   ObjectConnector( const ObjectConnector& connector )
55   : mObject( connector.mObject )
56   {
57   }
58
59   /**
60    * @brief Assignment operator
61    */
62   ObjectConnector& operator=( const ObjectConnector& connector )
63   {
64     this->mObject = connector.mObject;
65     return *this;
66   }
67
68   /**
69    * @brief Returns a smart pointer to the object
70    * @return a smart pointer to the object
71    */
72   ObjectPtr Get() const
73   {
74     return mObject;
75   }
76
77   /**
78    * @brief Assigns the object, calling Connect and Disconnect methods accordingly, taking onScene into account.
79    * @param [in] object smart pointer to a object
80    * @param [in] onScene whether the object is used on stage or not
81    */
82   void Set( Object& object, bool onScene )
83   {
84     if ( mObject.Get() != &object )
85     {
86       // Disconnect from old object
87       if ( mObject && onScene )
88       {
89         mObject->Disconnect();
90       }
91
92       mObject = &object;
93
94       // Connect to new object
95       if ( mObject && onScene )
96       {
97         mObject->Connect();
98       }
99     }
100   }
101
102   /**
103    * @brief Manages connection reference count.
104    *
105    * Must be called from owner when connected to the scene.
106    */
107   void OnSceneConnect()
108   {
109     if ( mObject )
110     {
111       mObject->Connect();
112     }
113   }
114
115   /**
116    * @brief Manages connection reference count.
117    *
118    * Must be called from owner when disconnecting from the scene.
119    */
120   void OnSceneDisconnect()
121   {
122     if ( mObject )
123     {
124       mObject->Disconnect();
125     }
126   }
127
128 private: //data
129   ObjectPtr mObject;  ///< intrusive pointer to the Object. ObjectConnector owns this.
130
131 };
132
133 } // namespace Internal
134
135 } // namespace Dali
136
137 #endif // DALI_INTERNAL_OBJECT_CONNECTOR_H