arrp 0.0.15
Auto returning resource pool for modern C++
Loading...
Searching...
No Matches
siddiqsoft::arrp::scoped_resource< T > Class Template Reference

RAII wrapper for managing resource lifecycle with automatic return to pool. More...

#include <siddiqsoft/private/scoped_resource.hpp>

Public Member Functions

 scoped_resource ()=delete
 Default constructor is deleted.
 scoped_resource (T &&src, std::function< void(T &&)> &&f={})
 scoped_resource (const T &)=delete
 Copy constructor is deleted.
 scoped_resource (scoped_resource &&src) noexcept
scoped_resourceoperator= (scoped_resource &&src) noexcept
scoped_resourceoperator= (T &&src)
scoped_resourceoperator= (const scoped_resource &)=delete
 Copy assignment operator is deleted.
auto operator* () -> T &
 operator T& ()
void invalidate ()

Protected Attributes

m_rsrc {}
 The actual resource being wrapped.
std::function< void(T &&)> m_putback_callback {}
 Callback function to return the resource to the pool.
bool m_is_valid {false}
 Tracks whether the resource is valid and should be returned to pool.

Friends

template<typename U, typename SRT, uint8_t IC>
class resource_pool

Detailed Description

template<typename T>
requires NonNumericMoveConstructible<T>
class siddiqsoft::arrp::scoped_resource< T >

RAII wrapper for managing resource lifecycle with automatic return to pool.

Template Parameters
TThe resource type to wrap. Must be move-constructible and non-arithmetic.

The scoped_resource class implements the Resource Acquisition Is Initialization (RAII) pattern for managing resources that should be returned to a resource pool. It wraps a resource and automatically invokes a callback when the wrapper is destroyed, enabling automatic resource management without manual cleanup.

Key Features:

  • RAII Pattern: Automatically returns resources via destructor
  • Move Semantics: Supports move construction and move assignment for efficient transfers
  • Validity Tracking: Tracks whether a resource should be returned to the pool
  • Callback Support: Executes custom callback on destruction
  • Debug Support: Includes debug identifiers for tracking in DEBUG builds
  • Dereference Access: Provides operator* and explicit conversion for resource access
  • Invalidation: Allows explicit invalidation to prevent automatic return

Usage Pattern:

// Typical usage with resource_pool
{
auto resource = pool.checkout();
// Use the resource...
resource->doSomething();
// Automatically returned to pool when going out of scope
}
// Manual invalidation
{
auto resource = pool.checkout();
auto ptr = std::move(*resource);
resource.invalidate(); // Don't return the moved-out resource
}

Thread Safety:

  • Not thread-safe by itself; thread safety is provided by resource_pool
  • The callback should be thread-safe if called from multiple threads

Constraints:

  • Not copy-constructible or copy-assignable
  • Default constructor is deleted
  • Resources must be move-constructible and non-arithmetic
Note
This class is typically used internally by resource_pool
The callback is optional; if not provided, the resource is simply destroyed
Derived classes can override behavior by providing custom callbacks

Definition at line 134 of file scoped_resource.hpp.

Constructor & Destructor Documentation

◆ scoped_resource() [1/4]

template<typename T>
siddiqsoft::arrp::scoped_resource< T >::scoped_resource ( )
delete

Default constructor is deleted.

Resources must be explicitly constructed with a valid resource. This prevents accidental creation of invalid scoped_resource objects.

Note
Use the explicit constructor that takes a resource instead
Examples
/opt/azure-agent/_work/20/s/include/siddiqsoft/private/scoped_resource.hpp.

Referenced by operator=(), and scoped_resource().

◆ scoped_resource() [2/4]

template<typename T>
siddiqsoft::arrp::scoped_resource< T >::scoped_resource ( T && src,
std::function< void(T &&)> && f = {} )
inlineexplicit

Definition at line 203 of file scoped_resource.hpp.

◆ scoped_resource() [3/4]

template<typename T>
siddiqsoft::arrp::scoped_resource< T >::scoped_resource ( const T & )
explicitdelete

Copy constructor is deleted.

Resources are move-only to maintain clear ownership semantics. Copying would create ambiguity about which wrapper owns the resource and when it should be returned to the pool.

Note
Use move construction instead

References scoped_resource().

◆ scoped_resource() [4/4]

template<typename T>
siddiqsoft::arrp::scoped_resource< T >::scoped_resource ( scoped_resource< T > && src)
inlinenoexcept

Definition at line 246 of file scoped_resource.hpp.

◆ ~scoped_resource()

template<typename T>
siddiqsoft::arrp::scoped_resource< T >::~scoped_resource ( )
inline

Definition at line 389 of file scoped_resource.hpp.

Member Function Documentation

◆ invalidate()

template<typename T>
void siddiqsoft::arrp::scoped_resource< T >::invalidate ( )
inline

Definition at line 434 of file scoped_resource.hpp.

◆ operator T&()

template<typename T>
siddiqsoft::arrp::scoped_resource< T >::operator T& ( )
inline

Definition at line 359 of file scoped_resource.hpp.

◆ operator*()

template<typename T>
auto siddiqsoft::arrp::scoped_resource< T >::operator* ( ) -> T &
inline

Definition at line 357 of file scoped_resource.hpp.

◆ operator=() [1/3]

template<typename T>
scoped_resource & siddiqsoft::arrp::scoped_resource< T >::operator= ( const scoped_resource< T > & )
delete

Copy assignment operator is deleted.

Copy assignment is not allowed to maintain move-only semantics and prevent resource ownership ambiguity.

References m_rsrc, and scoped_resource().

◆ operator=() [2/3]

template<typename T>
scoped_resource & siddiqsoft::arrp::scoped_resource< T >::operator= ( scoped_resource< T > && src)
inlinenoexcept

Definition at line 278 of file scoped_resource.hpp.

◆ operator=() [3/3]

template<typename T>
scoped_resource & siddiqsoft::arrp::scoped_resource< T >::operator= ( T && src)
inline

Definition at line 323 of file scoped_resource.hpp.

◆ resource_pool

template<typename T>
template<typename U, typename SRT, uint8_t IC>
friend class resource_pool
friend

Definition at line 140 of file scoped_resource.hpp.

Member Data Documentation

◆ m_is_valid

template<typename T>
bool siddiqsoft::arrp::scoped_resource< T >::m_is_valid {false}
protected

Tracks whether the resource is valid and should be returned to pool.

Prevents returning uninitialized or moved-out resources

  • true: resource will be returned to pool on destruction
  • false: resource will NOT be returned to pool on destruction
Examples
/opt/azure-agent/_work/20/s/include/siddiqsoft/private/scoped_resource.hpp.

Definition at line 156 of file scoped_resource.hpp.

◆ m_putback_callback

template<typename T>
std::function<void(T&&)> siddiqsoft::arrp::scoped_resource< T >::m_putback_callback {}
protected

Callback function to return the resource to the pool.

Called by destructor when resource is valid. Typically returns the resource to the resource_pool for reuse.

Examples
/opt/azure-agent/_work/20/s/include/siddiqsoft/private/scoped_resource.hpp.

Definition at line 150 of file scoped_resource.hpp.

◆ m_rsrc

template<typename T>
T siddiqsoft::arrp::scoped_resource< T >::m_rsrc {}
protected

The actual resource being wrapped.

Stores the resource object that will be managed by this wrapper

Examples
/opt/azure-agent/_work/20/s/include/siddiqsoft/private/scoped_resource.hpp.

Definition at line 145 of file scoped_resource.hpp.

Referenced by operator=().


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