[dali_2.3.26] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / public-api / object / ref-object.h
1 #ifndef DALI_REF_OBJECT_H
2 #define DALI_REF_OBJECT_H
3
4 /*
5  * Copyright (c) 2022 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 // EXTERNAL INCLUDES
22 #include <atomic>
23 #include <cstdint> // uint32_t
24
25 // INTERNAL INCLUDES
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/public-api/common/intrusive-ptr.h>
28
29 namespace Dali
30 {
31 /**
32  * @addtogroup dali_core_object
33  * @{
34  */
35
36 class Value;
37
38 /**
39  * @brief Base class for reference counted objects.
40  *
41  * Typically this should be used with an intrusive pointer,
42  * instead of calling Reference() and Unreference() methods directly.
43  * @SINCE_1_0.0
44  */
45 class DALI_CORE_API RefObject
46 {
47 public:
48   /**
49    * @brief Increments the object's reference count.
50    * @SINCE_1_0.0
51    */
52   void Reference();
53
54   /**
55    * @brief Decrements the object's reference count.
56    *
57    * When the reference count drops to zero, the object will self-destruct.
58    * @SINCE_1_0.0
59    */
60   void Unreference();
61
62   /**
63    * @brief Retrieves the object's reference count.
64    *
65    * @SINCE_1_0.0
66    * @return The reference count
67    */
68   uint32_t ReferenceCount() const;
69
70 protected:
71   /**
72    * @brief Default constructor.
73    * @SINCE_1_0.0
74    */
75   RefObject();
76
77   /**
78    * @brief RefObject is intended as a base class.
79    *
80    * A RefObject may only be deleted when its reference count is zero.
81    * @SINCE_1_0.0
82    */
83   virtual ~RefObject();
84
85   /**
86    * @brief Copy constructor.
87    *
88    * The newly copied object will have a reference count of zero.
89    * @SINCE_1_0.0
90    * @param[in] rhs The object to copy
91    */
92   RefObject(const RefObject& rhs);
93
94   /**
95    * @brief Assignment operator.
96    *
97    * The newly copied object will have a reference count of zero.
98    * @SINCE_1_0.0
99    * @param[in] rhs The object to copy
100    * @return A reference to this
101    */
102   RefObject& operator=(const RefObject& rhs);
103
104   // Not movable
105
106   RefObject(RefObject&& rhs) = delete;            ///< Deleted move constructor
107   RefObject& operator=(RefObject&& rhs) = delete; ///< Deleted move assignment operator
108
109 private:
110   std::atomic_uint32_t mCount{0u}; ///< Reference count
111 };
112
113 /**
114  * @}
115  */
116 } // namespace Dali
117
118 #endif // DALI_REF_OBJECT_H