LiteFX 0.4.1.2025
Computer Graphics Engine
Loading...
Searching...
No Matches
LiteFX::SharedObject Class Reference

Base class for an object that can be shared. More...

#include <containers.hpp>

Inherits std::enable_shared_from_this< SharedObject >.

Inherited by LiteFX::Rendering::Backends::DirectX12BottomLevelAccelerationStructure::DirectX12BottomLevelAccelerationStructureImpl, LiteFX::Graphics::Blitter< TBackend >, LiteFX::Rendering::IBuffer, LiteFX::Rendering::ICommandBuffer, LiteFX::Rendering::ICommandQueue, LiteFX::Rendering::IDescriptorSetLayout, LiteFX::Rendering::IFrameBuffer, LiteFX::Rendering::IGraphicsAdapter, LiteFX::Rendering::IGraphicsDevice, LiteFX::Rendering::IGraphicsFactory, LiteFX::Rendering::IImage, LiteFX::Rendering::IIndexBufferLayout, LiteFX::Rendering::IInputAssembler, LiteFX::Rendering::IPipelineLayout, LiteFX::Rendering::IRasterizer, LiteFX::Rendering::IRenderPass, LiteFX::Rendering::ISampler, LiteFX::Rendering::IShaderProgram, LiteFX::Rendering::IVertexBufferLayout, LiteFX::Rendering::TimingEvent, and LiteFX::Rendering::Backends::VulkanBottomLevelAccelerationStructure::VulkanBottomLevelAccelerationStructureImpl.

Classes

struct  Allocator
 An allocator used to allocate the shared object. More...
 

Public Member Functions

virtual ~SharedObject () noexcept=default
 Destroys the shared object.
 
template<typename TSelf >
auto shared_from_this (this TSelf &&self) noexcept
 Returns a shared pointer to the current object instance.
 
template<typename TSelf >
auto weak_from_this (this TSelf &&self) noexcept -> WeakPtr< std::remove_reference_t< TSelf > >
 Returns a weak pointer to the current object instance.
 

Protected Member Functions

 SharedObject () noexcept=default
 Initializes a new shared object.
 
 SharedObject (SharedObject &&) noexcept=default
 
 SharedObject (const SharedObject &)=default
 
SharedObjectoperator= (SharedObject &&) noexcept=default
 
SharedObjectoperator= (const SharedObject &)=default
 

Static Protected Member Functions

template<typename T , typename... TArgs>
static auto create (TArgs &&... args) -> SharedPtr< T >
 Generic factory method used to create instances of the shared object.
 

Detailed Description

Base class for an object that can be shared.

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 SharedAllocator in your class, as shown in the example below.

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);
}
}
Definition common.h:14
Base class for an object that can be shared.
Definition containers.hpp:1075
An allocator used to allocate the shared object.
Definition containers.hpp:1098

Constructor & Destructor Documentation

◆ SharedObject() [1/3]

LiteFX::SharedObject::SharedObject ( )
protecteddefaultnoexcept

Initializes a new shared object.

◆ SharedObject() [2/3]

LiteFX::SharedObject::SharedObject ( SharedObject && )
protecteddefaultnoexcept

◆ SharedObject() [3/3]

LiteFX::SharedObject::SharedObject ( const SharedObject & )
protecteddefault

◆ ~SharedObject()

virtual LiteFX::SharedObject::~SharedObject ( )
virtualdefaultnoexcept

Destroys the shared object.

Member Function Documentation

◆ create()

template<typename T , typename... TArgs>
static auto LiteFX::SharedObject::create ( TArgs &&... args) -> SharedPtr<T>
inlinestaticprotected

Generic factory method used to create instances of the shared object.

Template Parameters
TThe type of the class that inherits from SharedObject.
TArgsThe types of the arguments passed to the shared object's constructor.
Parameters
argsThe arguments that are forwarded to the shared object's constructor.
Returns
A shared pointer of the shared object.
See also
Allocator

◆ operator=() [1/2]

SharedObject & LiteFX::SharedObject::operator= ( const SharedObject & )
protecteddefault

◆ operator=() [2/2]

SharedObject & LiteFX::SharedObject::operator= ( SharedObject && )
protecteddefaultnoexcept

◆ shared_from_this()

template<typename TSelf >
auto LiteFX::SharedObject::shared_from_this ( this TSelf && self)
inlinenoexcept

Returns a shared pointer to the current object instance.

◆ weak_from_this()

template<typename TSelf >
auto LiteFX::SharedObject::weak_from_this ( this TSelf && self) -> WeakPtr<std::remove_reference_t<TSelf>>
inlinenoexcept

Returns a weak pointer to the current object instance.