ROSE 0.11.145.147
Public Member Functions | List of all members
Sawyer::SharedObject Class Reference

Description

Base class for reference counted objects.

The SharedObject class is used in conjunction with SharedPointer to declare that objects of this type are allocated on the heap (only), are reference counted, and invoke "delete" on themselves when the reference count decreases to zero. Any object that is intended to be referenced by a SharedPointer must inherit directly or indirectly from SharedObject.

Here's an example that demonstrates some of the best practices:

// Forward declarations if necessary
class MyBaseClass;
typedef Sawyer::SharedPointer<MyBaseClass> MyBaseClassPtr;
// Class definition
class MyBaseClass: public SharedObject {
public:
protected:
// Hide the constructors so users don't accidentally create objects on the stack
MyBaseClass();
MyBaseClass(const MyBaseClass&);
public:
// Provide allocating wrappers around the constructors
static Ptr instance() {
return Ptr(new MyBaseClass);
}
static Ptr instance(const MyBaseClass &other) {
return Ptr(new MyBaseClass(other));
}
};
Base class for reference counted objects.
Reference-counting intrusive smart pointer.
See also
SharedPointer, SharedFromThis

Definition at line 64 of file SharedObject.h.

#include <Sawyer/SharedObject.h>

Inheritance diagram for Sawyer::SharedObject:
Inheritance graph
[legend]

Public Member Functions

 SharedObject ()
 Default constructor.
 
 SharedObject (const SharedObject &)
 Copy constructor.
 
virtual ~SharedObject ()
 Virtual destructor.
 
SharedObjectoperator= (const SharedObject &)
 Assignment.
 

Constructor & Destructor Documentation

◆ SharedObject() [1/2]

Sawyer::SharedObject::SharedObject ( )
inline

Default constructor.

Initializes the reference count to zero.

Definition at line 70 of file SharedObject.h.

◆ SharedObject() [2/2]

Sawyer::SharedObject::SharedObject ( const SharedObject )
inline

Copy constructor.

Shared objects are not typically copy-constructed, but we must support it anyway in case the user wants to copy-construct some shared object. The new object has a ref-count of zero.

Definition at line 76 of file SharedObject.h.

◆ ~SharedObject()

virtual Sawyer::SharedObject::~SharedObject ( )
inlinevirtual

Virtual destructor.

Verifies that the reference count is zero.

Definition at line 79 of file SharedObject.h.

Member Function Documentation

◆ operator=()

SharedObject & Sawyer::SharedObject::operator= ( const SharedObject )
inline

Assignment.

Assigning one object to another doesn't change the reference count or mutex of either object.

Definition at line 86 of file SharedObject.h.


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