2
* Copyright 2010 Inalogic Inc.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License version 3, as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the applicable version of the GNU Lesser General Public
12
* License for more details.
14
* You should have received a copy of both the GNU Lesser General Public
15
* License version 3 along with this program. If not, see
16
* <http://www.gnu.org/licenses/>
18
* Authored by: Jay Taoko <jay.taoko_AT_gmail_DOT_com>
33
class IntrusiveWeakSP;
35
// #if defined(NUX_DEBUG)
36
#define NUX_FILE_LINE_PROTO const char* __Nux_FileName__=__FILE__, int __Nux_LineNumber__ = __LINE__
37
#define NUX_FILE_LINE_DECL const char* __Nux_FileName__, int __Nux_LineNumber__
38
#define NUX_FILE_LINE_PARAM __Nux_FileName__, __Nux_LineNumber__
39
#define NUX_TRACKER_LOCATION __FILE__, __LINE__
41
// #define NUX_FILE_LINE_PROTO int __Nux_Dummy__ = 0xD0DECADE
42
// #define NUX_FILE_LINE_DECL int __Nux_Dummy__
43
// #define NUX_FILE_LINE_PARAM __Nux_Dummy__
44
// #define NUX_TRACKER_LOCATION 0xD0DECADE
49
NUX_DECLARE_GLOBAL_OBJECT (ObjectStats, GlobalSingletonInitializer);
51
class AllocationList : public std::list<void *>
58
AllocationList _allocation_list;
59
int _total_allocated_size; //! Total allocated memory size in bytes.
60
int _number_of_objects; //! Number of allocated objects;
62
#define GObjectStats NUX_GLOBAL_OBJECT_INSTANCE(nux::ObjectStats)
64
//! Base class of heap allocated objects.
66
Trackable does not implement reference counting. It only defines the API. It is up
67
to the class that inherit from Trackable to implement the reference counting.
72
NUX_DECLARE_ROOT_OBJECT_TYPE (Trackable);
73
//! Test if object reference is owned.
75
@return True if the object reference is owned.
77
bool OwnsTheReference();
79
//! Test if object was allocated dynamically.
81
@return True if the object was allocated dynamically.
83
bool IsHeapAllocated() const;
85
//! Test if object was allocated dynamically.
87
@return True if the object was allocated dynamically.
89
bool IsDynamic() const;
91
//! Increase the reference count.
93
Widget are typically created and added to containers. It is decided that when widgets are created, they should have a floating reference
94
and their reference count is set to 1.
96
Button* button = new Button(); // button ref_cout = 1, floating = true;
97
container->AddButton(button); // button has a floating reference; when container call button->ref() the ref count
98
// of button remains at 1 but the floating reference is set to false. From now on,
99
// calling button->ref will always increase the ref count (since button no longer has a floating reference).
102
It is best to pair calls to ref() with unref() when it comes to widgets. So if a widget was not added to a container and so it still has a
103
floating reference, then call Dispose(). Dispose does some sanity check; it verifies that:
106
If these conditions are verified, dispose will cause the object to be destroyed.
107
Calling unref() on an object that has a floating reference will trigger a warning/error in order to invite the
108
developer. The developer can either ref the object first before calling unref or simply not create the widget since it
109
does not appear to have been used.
111
During development it often happen that one forget to dispose an object with a floating reference.
112
Assuming that all functions that receive a reference counted object properly call ref on the object and that the compiler
113
can detect unused variables, then the developer should have a way to detect reference counted objects that are not owned.
114
It is up to the developer to properly handle these objects.
116
virtual void Reference();
118
//! Decrease the reference count.
120
@return True if the object has been destroyed
122
virtual bool UnReference();
124
//! Mark the object as owned.
126
@return True if the object was not owned previously
128
virtual bool SinkReference();
130
//! Destroy and object that has a floating reference.
132
If this object is not owned, calling SinkReference() as the same effect as calling Reference().
133
@return True if the object has been destroyed
135
virtual bool Dispose();
137
static std::new_handler set_new_handler (std::new_handler handler);
138
static void *operator new (size_t size);
140
#if (__GNUC__ < 4 && __GNUC_MINOR__ < 4)
141
static void *operator new (size_t size, void *ptr);
144
static void operator delete (void *ptr);
148
virtual ~Trackable() = 0;
149
void SetOwnedReference (bool b);
152
Trackable (const Trackable &);
153
Trackable &operator= (const Trackable &);
155
// class AllocationList : public std::list<void *>
159
// ~AllocationList();
162
//static AllocationList m_allocation_list;
163
static std::new_handler m_new_current_handler;
164
// static int m_total_allocated_size; //! Total allocated memory size in bytes.
165
// static int m_number_of_objects; //! Number of allocated objects;
167
bool m_owns_the_reference;
168
int m_size_of_this_object;
170
//template<typename T> friend class Pointer;
173
//! The base class of Nux objects.
174
class Object: public Trackable
177
NUX_DECLARE_OBJECT_TYPE (BaseObject, Trackable);
180
Object (bool OwnTheReference = true, NUX_FILE_LINE_PROTO);
181
//! Increase reference count.
183
//! Decrease reference count.
185
@return True if the object reference count has reached 0 and the object has been destroyed.
189
//! Mark the object as owned.
191
@return True if the object was not owned previously
193
virtual bool SinkReference();
195
//! Destroy and object that has a floating reference.
197
If this object is not owned, calling SinkReference() as the same effect as calling Reference().
198
@return True is the object has been destroyed
200
virtual bool Dispose();
202
//! Returns true if the object was allocated on the heap.
204
@return True if the object was allocated on the heap.
206
bool IsHeapAllocated();
208
//! Get the reference count of this object.
210
@return The reference count of this object.
212
int GetReferenceCount () const;
214
//! Get the weak reference count of this object.
216
@return The weak reference count of this object.
218
int GetWeakReferenceCount () const;
220
NThreadSafeCounter *m_reference_count; //!< Reference count.
221
NThreadSafeCounter *m_weak_reference_count; //!< Weak reference count.
224
//! Private destructor.
226
Private destructor. Ensure that Object cannot be created on the stack (only on the heap), but objects that inherits
227
from Object can stil be created on the stack or on the heap.
232
void IncrementWeakCounter();
233
void DecrementWeakCounter();
236
//! Destroy the object.
239
Object (const Object &);
240
Object &operator = (const Object &);
243
//#if defined(NUX_DEBUG)
244
NString m_allocation_file_name;
245
int m_allocation_line_number;
248
template <typename T>
249
friend class IntrusiveSP;
251
template <typename T>
252
friend class IntrusiveWeakSP;
253
friend class ObjectStats;
258
#endif // NUXOBJECT_H