Back to index

nux  3.0.0
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | Static Private Attributes
nux::Trackable Class Reference

Base class of heap allocated objects. More...

#include <Object.h>

Inheritance diagram for nux::Trackable:
Inheritance graph
[legend]
Collaboration diagram for nux::Trackable:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 NUX_DECLARE_ROOT_OBJECT_TYPE (Trackable)
bool OwnsTheReference ()
 Test if object reference is owned.
bool IsHeapAllocated ()
 Test if object was allocated dynamically.
bool IsDynamic () const
 Test if object was allocated dynamically.
virtual bool Reference ()
 Increase the reference count.
virtual bool UnReference ()
 Decrease the reference count.
virtual bool SinkReference ()
 Mark the object as owned.
virtual bool Dispose ()
 Destroy and object that has a floating reference.
virtual int GetObjectSize ()
 Return the size of allocated for this object.
bool SetProperty (std::string const &name, const char *value)
 If the property was not able to be set with the value, the method returns false.
template<typename T >
bool SetProperty (std::string const &name, T const &value)
template<typename T >
GetProperty (std::string const &name, T *foo=0)
void AddProperty (std::string const &name, PropertyBase *property)

Static Public Member Functions

static std::new_handler set_new_handler (std::new_handler handler)
static void * operator new (size_t size)
static void * operator new (size_t size, void *ptr)
static void operator delete (void *ptr)

Protected Member Functions

 Trackable ()
virtual ~Trackable ()=0
void SetOwnedReference (bool b)

Protected Attributes

int _heap_allocated

Private Member Functions

 Trackable (const Trackable &)
Trackableoperator= (const Trackable &)

Private Attributes

bool _owns_the_reference
int _size_of_this_object

Static Private Attributes

static std::new_handler _new_current_handler = 0

Detailed Description

Base class of heap allocated objects.

Trackable does not implement reference counting. It only defines the API. It is up to the class that inherit from Trackable to implement the reference counting.

Definition at line 66 of file Object.h.


Constructor & Destructor Documentation

nux::Trackable::Trackable ( ) [protected]

Definition at line 86 of file Object.cpp.

nux::Trackable::~Trackable ( ) [protected, pure virtual]

Definition at line 93 of file Object.cpp.

  {
  }
nux::Trackable::Trackable ( const Trackable ) [private]

Member Function Documentation

void nux::Introspectable::AddProperty ( std::string const &  name,
PropertyBase property 
) [inline, inherited]

Definition at line 254 of file Property-inl.h.

{
  // check to see if it exists and if it does barf horribly as we can't
  // have two properties with the same name;
  properties_[name] = property;
}

Here is the caller graph for this function:

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

Destroy and object that has a floating reference.

Returns:
True if the object has been destroyed

Reimplemented in nux::Object.

Definition at line 112 of file Object.cpp.

  {
    return false;
  }
int nux::Trackable::GetObjectSize ( ) [virtual]

Return the size of allocated for this object.

Returns:
The size allocated for this object.

Definition at line 217 of file Object.cpp.

  {
    return _size_of_this_object;
  }
template<typename T >
T nux::Introspectable::GetProperty ( std::string const &  name,
T *  foo = 0 
) [inherited]

Definition at line 285 of file Property-inl.h.

{
  PropertyContainer::iterator i = properties_.find(name);
  if (i == properties_.end())
    return T();

  std::string s = i->second->GetSerializedValue();
  std::pair<T, bool> result = type::PropertyTrait<T>::from_string(s);
  // If this is called with a template type that the property does not
  // support nice conversion to, you'll get no error, but will get
  // a default constructed T.  We could use an exception here.
  return result.first;
}
bool nux::Trackable::IsDynamic ( ) const

Test if object was allocated dynamically.

Definition at line 201 of file Object.cpp.

  {
    // Get pointer to beginning of the memory occupied by this.
    const void *ptr = dynamic_cast<const void *> (this);

    // Search for ptr in allocation_list
#if defined(NUX_OS_WINDOWS) && !defined(NUX_VISUAL_STUDIO_2010)
    std::list<void*>::iterator i = std::find(GObjectStats._allocation_list.begin(),
      GObjectStats._allocation_list.end(), ptr);
#else
    auto i = std::find(GObjectStats._allocation_list.begin(),
                       GObjectStats._allocation_list.end(), ptr);
#endif
    return i != GObjectStats._allocation_list.end();
  }

Here is the caller graph for this function:

Test if object was allocated dynamically.

Definition at line 191 of file Object.cpp.

  {
    if (_heap_allocated == -1)
    {
      _heap_allocated = (IsDynamic () ? 1 : 0);
    }

    return (_heap_allocated == 1 ? true : false);
  }

Here is the call graph for this function:

Here is the caller graph for this function:

void nux::Trackable::operator delete ( void *  ptr) [static]

Definition at line 178 of file Object.cpp.

  {
    ObjectStats::AllocationList::iterator i = std::find (GObjectStats._allocation_list.begin(), GObjectStats._allocation_list.end(), ptr);

    if (i != GObjectStats._allocation_list.end() )
    {
      GObjectStats._total_allocated_size -= NUX_STATIC_CAST (Trackable *, ptr)->_size_of_this_object;
      --GObjectStats._number_of_objects;
      GObjectStats._allocation_list.erase (i);
      ::operator delete (ptr);
    }
  }
void * nux::Trackable::operator new ( size_t  size) [static]

Definition at line 140 of file Object.cpp.

  {
    // Set the new_handler for this call
    std::new_handler global_handler  = std::set_new_handler (_new_current_handler);

    // If allocation fails _new_current_handler is called, if specified,
    // otherwise the global new_handler is called.
    void *ptr;

    try
    {
      ptr = ::operator new (size);

      GObjectStats._allocation_list.push_front (ptr);
      NUX_STATIC_CAST (Trackable *, ptr)->_size_of_this_object = size;
      GObjectStats._total_allocated_size += size;
      ++GObjectStats._number_of_objects;
    }
    catch (std::bad_alloc &)
    {
      std::set_new_handler (global_handler);
      throw;
    }

    //  Reset gloabal new_handler
    std::set_new_handler (global_handler);
    return ptr;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

void * nux::Trackable::operator new ( size_t  size,
void *  ptr 
) [static]

Definition at line 171 of file Object.cpp.

  {
    return ::operator new (size, ptr);
  }

Here is the call graph for this function:

Trackable& nux::Trackable::operator= ( const Trackable ) [private]

Test if object reference is owned.

Definition at line 117 of file Object.cpp.

  {
    return _owns_the_reference;
  }

Here is the caller graph for this function:

bool nux::Trackable::Reference ( ) [virtual]

Increase the reference count.

Reimplemented in nux::Object.

Definition at line 97 of file Object.cpp.

  {
    return false;
  }
std::new_handler nux::Trackable::set_new_handler ( std::new_handler  handler) [static]

Definition at line 133 of file Object.cpp.

  {
    std::new_handler old_handler = _new_current_handler;
    _new_current_handler = handler;
    return old_handler;
  }

Here is the caller graph for this function:

void nux::Trackable::SetOwnedReference ( bool  b) [protected]

Definition at line 122 of file Object.cpp.

  {
    if (_owns_the_reference == true)
    {
      LOG_DEBUG(logger) << "Do not change the ownership if is already set to true!" << "\n";
      return;
    }

    _owns_the_reference = b;
  }

Here is the caller graph for this function:

bool nux::Introspectable::SetProperty ( std::string const &  name,
const char *  value 
) [inline, inherited]

If the property was not able to be set with the value, the method returns false.

Definition at line 262 of file Property-inl.h.

{
  PropertyContainer::iterator i = properties_.find(name);
  if (i == properties_.end())
    return false;
  else
    return i->second->SetValue(value);
}
template<typename T >
bool nux::Introspectable::SetProperty ( std::string const &  name,
T const &  value 
) [inherited]

Definition at line 273 of file Property-inl.h.

{
  PropertyContainer::iterator i = properties_.find(name);
  if (i == properties_.end())
    return false;
  else
  {
    return i->second->SetValue(type::PropertyTrait<T>::to_string(value));
  }
}
bool nux::Trackable::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 in nux::Object.

Definition at line 107 of file Object.cpp.

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

Decrease the reference count.

Returns:
True if the object has been destroyed

Reimplemented in nux::Object.

Definition at line 102 of file Object.cpp.

  {
    return false;
  }

Member Data Documentation

Definition at line 158 of file Object.h.

std::new_handler nux::Trackable::_new_current_handler = 0 [static, private]

Definition at line 165 of file Object.h.

Definition at line 167 of file Object.h.

Definition at line 168 of file Object.h.


The documentation for this class was generated from the following files: