LiteFX::SharedObject
Base class for an object that can be shared.
Synopsis
Declared in <litefx/containers.hpp>
class SharedObject
: public std::enable_shared_from_this<SharedObject>
Description
This is an improved version of std::enable_shared_from_this that supports inheritance. When inheriting from this class, follow the same practices as you would for std::enable_shared_from_this: do not provide any public constructors; instead provide a private constructor and a publicly accessible static factory method, that returns a shared pointer.
Note that the above rule does not apply for objects that are stored within a PimplPtr, as those are handled correctly by the pointer implementation.
You may want to create objects by creating a static factory method that calls the protected SharedObject::create method. This has the advantage of allocating a single memory block for both, the object and the shared pointers control block. To do this, make sure to declare friendship to SharedObject::Allocator in your class, as shown in the example below.
Example
class Foo : public SharedObject {
friend struct SharedObject::Allocator<Foo>;
private:
explicit Foo(int a, std::string b) { }
public:
static inline auto create(int a, std::string b) {
return SharedObject::create<Foo>(a, b);
}
}
Base Classes
| Name | Description |
std::enable_shared_from_this<SharedObject> | |
Type Aliases
Member Functions
| Name | Description |
~SharedObject [destructor] [virtual] | Destroys the shared object. |
shared_from_this | Returns a shared pointer to the current object instance. |
weak_from_this | Returns a weak pointer to the current object instance. |
Protected Types
| Name | Description |
Allocator | An allocator used to allocate the shared object. |
Protected Member Functions
| Name | Description |
SharedObject [constructor] | Initializes a new shared object. |
operator= | Assignment operators |
Protected Static Member Functions
| Name | Description |
create | Generic factory method used to create instances of the shared object. |
Derived Classes
See Also
https://en.cppreference.com/w/cpp/memory/enable_shared_from_this