nux-1.16.0
nux::Object Class Reference

The base class of Nux objects. More...

#include <NuxCore/Object.h>

Inheritance diagram for nux::Object:
nux::Trackable nux::Introspectable nux::ActionItem nux::CachedResourceData nux::FontTexture nux::InitiallyUnownedObject nux::IOpenGLResource nux::MenuBarItem nux::ResourceData nux::Timeline

List of all members.

Public Member Functions

 NUX_DECLARE_OBJECT_TYPE (BaseObject, Trackable)
 Object (bool OwnTheReference=true, NUX_FILE_LINE_PROTO)
 Constructor.
bool Reference ()
 Increase reference count.
bool UnReference ()
 Decrease reference count.
virtual bool SinkReference ()
 Mark the object as owned.
virtual bool Dispose ()
 Destroy and object that has a floating reference.
int GetReferenceCount () const
 Get the reference count of this object.
std::string GetAllocationLoation () const

Public Attributes

sigc::signal< void, Object * > object_destroyed
 Signal emitted immediately before the object is destroyed.

Protected Member Functions

virtual ~Object ()
 Private destructor.

Friends

class ObjectPtr
class ObjectWeakPtr
class ObjectStats

Detailed Description

The base class of Nux objects.

Definition at line 172 of file Object.h.


Member Function Documentation

bool nux::Object::Dispose ( ) [virtual]

Destroy and object that has a floating reference.

Returns:
True is the object has been destroyed

Reimplemented from nux::Trackable.

Definition at line 323 of file Object.cpp.

References UnReference().

  {
    // The intent of the Dispose call is to destroy objects with a float
    // reference (reference count is equal to 1 and the '_owns_the_reference'
    // flag is set to false). In Nux, only widgets object can have a floating
    // reference.  And widgets are only visible if added to the widget tree.
    // When an object with a floating reference is added to the widget tree,
    // it becomes "owned'. It looses it floating reference status but it still
    // has a reference count number of 1.  In practice, since widgets must be
    // added to the widget tree, there should never be a need to call Dispose
    // (except in a few cases).

    // Dispose() was designed to only destroy objects with floating
    // references, while UnReference() destroys objects that are "owned".
    // That is now relaxed. Dispose() calls UnReference().
    return UnReference();
  }
int nux::Object::GetReferenceCount ( ) const

Get the reference count of this object.

Returns:
The reference count of this object.

Definition at line 355 of file Object.cpp.

  {
    return reference_count_->GetValue();
  }
bool nux::Object::Reference ( ) [virtual]

Increase reference count.

Returns:
True if the object has successfully referenced.

Reimplemented from nux::Trackable.

Definition at line 252 of file Object.cpp.

References nux::Trackable::IsHeapAllocated(), and nux::Trackable::OwnsTheReference().

Referenced by nux::BaseWindow::SetEnterFocusInputArea(), nux::WindowThread::SetLayout(), and nux::Area::SetParentObject().

  {
    if (!IsHeapAllocated())
    {
      LOG_WARN(logger) << "Trying to reference an object that was not heap allocated."
                       << "\nObject allocated at: " << GetAllocationLoation();
      return false;
    }

    if (!OwnsTheReference())
    {
      SetOwnedReference(true);
      // The ref count remains at 1. Exit the method.
      return true;
    }

    reference_count_->Increment();
    return true;
  }
bool nux::Object::SinkReference ( ) [virtual]

Mark the object as owned.

If this object is not owned, calling SinkReference() as the same effect as calling Reference().

Returns:
True if the object was not owned previously

Reimplemented from nux::Trackable.

Definition at line 304 of file Object.cpp.

References nux::Trackable::IsHeapAllocated(), and nux::Trackable::OwnsTheReference().

  {
    if (!IsHeapAllocated())
    {
      LOG_WARN(logger) << "Trying to sink an object that was not heap allocated."
                       << "\nObject allocated at: " << GetAllocationLoation();
      return false;
    }

    if (!OwnsTheReference())
    {
      SetOwnedReference(true);
      // The ref count remains at 1. Exit the method.
      return true;
    }

    return false;
  }
bool nux::Object::UnReference ( ) [virtual]

Decrease reference count.

Returns:
True if the object reference count has reached 0 and the object has been destroyed.

Reimplemented from nux::Trackable.

Definition at line 272 of file Object.cpp.

References nux::Trackable::IsHeapAllocated().

Referenced by Dispose(), nux::GraphicsEngine::GraphicsEngine(), nux::BaseWindow::SetEnterFocusInputArea(), nux::ScrollView::SetVScrollBar(), nux::WindowThread::ThreadDtor(), and nux::Area::UnParentObject().

  {
    if (!IsHeapAllocated())
    {
      LOG_WARN(logger) << "Trying to un-reference an object that was not heap allocated."
                       << "\nObject allocated at: " << GetAllocationLoation();
      return false;
    }

    if (objectptr_count_->GetValue() == reference_count_->GetValue())
    {
      // There are ObjectPtr's hosting this object. Release all of them to
      // destroy this object.  This prevent from calling UnReference () many
      // times and destroying the object when there are ObjectPtr's hosting
      // it.  This method should not be called directly in that case.
      LOG_WARN(logger) << "There are ObjectPtr hosting this object. "
                       << "Release all of them to destroy this object. "
                       << "\nObject allocated at: " << GetAllocationLoation();
      return false;
    }

    reference_count_->Decrement();

    if (reference_count_->GetValue() == 0)
    {
      Destroy();
      return true;
    }

    return false;
  }

The documentation for this class was generated from the following files:
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends