libcosmos
Linux C++ System Programming Library
Loading...
Searching...
No Matches
cosmos::ResourceGuard< R > Class Template Reference

Helper class to guard arbitrary resources. More...

#include <utils.hxx>

Public Types

using CleanFunc = void (R)
 

Public Member Functions

 ResourceGuard (const ResourceGuard &)=delete
 
ResourceGuardoperator= (const ResourceGuard &)=delete
 
 ResourceGuard (R r, std::function< CleanFunc > cleaner)
 
void disarm ()
 

Protected Attributes

bool m_disarmed = false
 
m_res
 
std::function< CleanFunc > m_cleaner
 

Detailed Description

template<typename R>
class cosmos::ResourceGuard< R >

Helper class to guard arbitrary resources.

For non-heap resources (for which stdlib smart pointers should be used) a specialization of this type can be used which takes a custom cleanup function to be run during destruction.

Definition at line 71 of file utils.hxx.

Member Typedef Documentation

◆ CleanFunc

template<typename R >
using cosmos::ResourceGuard< R >::CleanFunc = void (R)

Definition at line 74 of file utils.hxx.

Constructor & Destructor Documentation

◆ ResourceGuard()

template<typename R >
cosmos::ResourceGuard< R >::ResourceGuard ( R r,
std::function< CleanFunc > cleaner )
inline

Definition at line 81 of file utils.hxx.

81 :
82 m_res{r},
83 m_cleaner{cleaner}
84 {}

◆ ~ResourceGuard()

template<typename R >
cosmos::ResourceGuard< R >::~ResourceGuard ( )
inline

Definition at line 86 of file utils.hxx.

86 {
87 if (!m_disarmed) {
88 m_cleaner(m_res);
89 }
90 }

Member Function Documentation

◆ disarm()

template<typename R >
void cosmos::ResourceGuard< R >::disarm ( )
inline

Definition at line 92 of file utils.hxx.

92{ m_disarmed = true; }

Member Data Documentation

◆ m_cleaner

template<typename R >
std::function<CleanFunc> cosmos::ResourceGuard< R >::m_cleaner
protected

Definition at line 98 of file utils.hxx.

◆ m_disarmed

template<typename R >
bool cosmos::ResourceGuard< R >::m_disarmed = false
protected

Definition at line 96 of file utils.hxx.

◆ m_res

template<typename R >
R cosmos::ResourceGuard< R >::m_res
protected

Definition at line 97 of file utils.hxx.


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