taco-db  0.1.0
Public Member Functions | Private Attributes | List of all members
taco::ResourceGuard< T, Relinquish, FlagType, InvalidVal, class > Class Template Reference

ResourceGuard is used for automatically relinquishes some resource when it goes out of scope. More...

#include <utils/ResourceGuard.h>

Public Member Functions

 ResourceGuard ()
 
 ResourceGuard (T val)
 
 ~ResourceGuard ()
 
 ResourceGuard (const ResourceGuard &)=delete
 
ResourceGuardoperator= (const ResourceGuard &)=delete
 
 ResourceGuard (ResourceGuard &&other)
 
ResourceGuardoperator= (ResourceGuard &&other)
 
 operator T () const
 
Get () const
 
 operator bool () const
 
bool IsValid () const
 
void Reset ()
 Relinquishes the underlying resource and sets its to an invalid value. More...
 
Release ()
 Releases the underlying resource without relinquishing it and returns it to the caller. More...
 
constexpr bool operator== (T rhs) const
 
constexpr bool operator!= (T rhs) const
 

Private Attributes

m_val
 

Detailed Description

template<class T, class Relinquish, class FlagType = bool, FlagType InvalidVal = std::is_lvalue_reference<T>::value, class = typename std::enable_if< !std::is_same<FlagType, bool>::value && (std::is_same<FlagType, bool>::value || std::is_same<FlagType, T>::value) && std::is_trivially_default_constructible<Relinquish>::value >::type>
class taco::ResourceGuard< T, Relinquish, FlagType, InvalidVal, class >

ResourceGuard is used for automatically relinquishes some resource when it goes out of scope.

It is similar to std::unique_ptr for dynamic allocated memory, but it allows one to enable RAII idiom on other resources such as buffer frames.

The resource is denoted as a T typed value, (usually an integral or a pointer), and it may be reclaimed by calling the Relinquish::operator(). Some constraints apply: 1) T may not be a boolean value; 2) Relinquish must be trivially constrictible. The relinquish functor is not called if the stored value is an invalid value (see below).

The third type parameter FlagType and the fourth value parameter InvalidVal are used for denoting an invalid value. FlagType must be one of the following:

There is an implicit conversion from \t ResourceGuard<T, ...> to \t T so that it may be used in place of \t T in many places. Caution: be careful with double "free" if you directly call the relinquish function instead of ResourceGuard::reset() before it goes out of scope, unless the relinquish function specifically consider for that (e.g., BufferManager::UnpinPage()).

Another useful function is ResourceGuard::Release(), which does not relinquish the resource. Rather it transfers the resource back to the caller by setting ResourceGuard to an invalid value and will not invoke the relinquish functor upon destruction. This is useful when the caller wants to transfer the ownership of the underlying resource beyond the scope where the ResourceGuard is defined (e.g., passing a buffer id stored in ScopedBufferId into a function that will unpin it from inside).

Note: always prefer existing C++ standard library guard or type alias in specific components. E.g., use std::unique_ptr for memory resource; use taco::MutexGuard for std::mutex; use taco::ScopedBufferId in stoarge/BufferManager.h.

The main template implements the class when FlagType is T.

Constructor & Destructor Documentation

◆ ResourceGuard() [1/4]

template<class T , class Relinquish , class FlagType = bool, FlagType InvalidVal = std::is_lvalue_reference<T>::value, class = typename std::enable_if< !std::is_same<FlagType, bool>::value && (std::is_same<FlagType, bool>::value || std::is_same<FlagType, T>::value) && std::is_trivially_default_constructible<Relinquish>::value >::type>
taco::ResourceGuard< T, Relinquish, FlagType, InvalidVal, class >::ResourceGuard ( )
inline

◆ ResourceGuard() [2/4]

template<class T , class Relinquish , class FlagType = bool, FlagType InvalidVal = std::is_lvalue_reference<T>::value, class = typename std::enable_if< !std::is_same<FlagType, bool>::value && (std::is_same<FlagType, bool>::value || std::is_same<FlagType, T>::value) && std::is_trivially_default_constructible<Relinquish>::value >::type>
taco::ResourceGuard< T, Relinquish, FlagType, InvalidVal, class >::ResourceGuard ( val)
inline

◆ ~ResourceGuard()

template<class T , class Relinquish , class FlagType = bool, FlagType InvalidVal = std::is_lvalue_reference<T>::value, class = typename std::enable_if< !std::is_same<FlagType, bool>::value && (std::is_same<FlagType, bool>::value || std::is_same<FlagType, T>::value) && std::is_trivially_default_constructible<Relinquish>::value >::type>
taco::ResourceGuard< T, Relinquish, FlagType, InvalidVal, class >::~ResourceGuard ( )
inline

◆ ResourceGuard() [3/4]

template<class T , class Relinquish , class FlagType = bool, FlagType InvalidVal = std::is_lvalue_reference<T>::value, class = typename std::enable_if< !std::is_same<FlagType, bool>::value && (std::is_same<FlagType, bool>::value || std::is_same<FlagType, T>::value) && std::is_trivially_default_constructible<Relinquish>::value >::type>
taco::ResourceGuard< T, Relinquish, FlagType, InvalidVal, class >::ResourceGuard ( const ResourceGuard< T, Relinquish, FlagType, InvalidVal, class > &  )
delete

◆ ResourceGuard() [4/4]

template<class T , class Relinquish , class FlagType = bool, FlagType InvalidVal = std::is_lvalue_reference<T>::value, class = typename std::enable_if< !std::is_same<FlagType, bool>::value && (std::is_same<FlagType, bool>::value || std::is_same<FlagType, T>::value) && std::is_trivially_default_constructible<Relinquish>::value >::type>
taco::ResourceGuard< T, Relinquish, FlagType, InvalidVal, class >::ResourceGuard ( ResourceGuard< T, Relinquish, FlagType, InvalidVal, class > &&  other)
inline

Member Function Documentation

◆ Get()

template<class T , class Relinquish , class FlagType = bool, FlagType InvalidVal = std::is_lvalue_reference<T>::value, class = typename std::enable_if< !std::is_same<FlagType, bool>::value && (std::is_same<FlagType, bool>::value || std::is_same<FlagType, T>::value) && std::is_trivially_default_constructible<Relinquish>::value >::type>
T taco::ResourceGuard< T, Relinquish, FlagType, InvalidVal, class >::Get ( ) const
inline

◆ IsValid()

template<class T , class Relinquish , class FlagType = bool, FlagType InvalidVal = std::is_lvalue_reference<T>::value, class = typename std::enable_if< !std::is_same<FlagType, bool>::value && (std::is_same<FlagType, bool>::value || std::is_same<FlagType, T>::value) && std::is_trivially_default_constructible<Relinquish>::value >::type>
bool taco::ResourceGuard< T, Relinquish, FlagType, InvalidVal, class >::IsValid ( ) const
inline

◆ operator bool()

template<class T , class Relinquish , class FlagType = bool, FlagType InvalidVal = std::is_lvalue_reference<T>::value, class = typename std::enable_if< !std::is_same<FlagType, bool>::value && (std::is_same<FlagType, bool>::value || std::is_same<FlagType, T>::value) && std::is_trivially_default_constructible<Relinquish>::value >::type>
taco::ResourceGuard< T, Relinquish, FlagType, InvalidVal, class >::operator bool ( ) const
inline

◆ operator T()

template<class T , class Relinquish , class FlagType = bool, FlagType InvalidVal = std::is_lvalue_reference<T>::value, class = typename std::enable_if< !std::is_same<FlagType, bool>::value && (std::is_same<FlagType, bool>::value || std::is_same<FlagType, T>::value) && std::is_trivially_default_constructible<Relinquish>::value >::type>
taco::ResourceGuard< T, Relinquish, FlagType, InvalidVal, class >::operator T ( ) const
inline

◆ operator!=()

template<class T , class Relinquish , class FlagType = bool, FlagType InvalidVal = std::is_lvalue_reference<T>::value, class = typename std::enable_if< !std::is_same<FlagType, bool>::value && (std::is_same<FlagType, bool>::value || std::is_same<FlagType, T>::value) && std::is_trivially_default_constructible<Relinquish>::value >::type>
constexpr bool taco::ResourceGuard< T, Relinquish, FlagType, InvalidVal, class >::operator!= ( rhs) const
inlineconstexpr

◆ operator=() [1/2]

template<class T , class Relinquish , class FlagType = bool, FlagType InvalidVal = std::is_lvalue_reference<T>::value, class = typename std::enable_if< !std::is_same<FlagType, bool>::value && (std::is_same<FlagType, bool>::value || std::is_same<FlagType, T>::value) && std::is_trivially_default_constructible<Relinquish>::value >::type>
ResourceGuard& taco::ResourceGuard< T, Relinquish, FlagType, InvalidVal, class >::operator= ( const ResourceGuard< T, Relinquish, FlagType, InvalidVal, class > &  )
delete

◆ operator=() [2/2]

template<class T , class Relinquish , class FlagType = bool, FlagType InvalidVal = std::is_lvalue_reference<T>::value, class = typename std::enable_if< !std::is_same<FlagType, bool>::value && (std::is_same<FlagType, bool>::value || std::is_same<FlagType, T>::value) && std::is_trivially_default_constructible<Relinquish>::value >::type>
ResourceGuard& taco::ResourceGuard< T, Relinquish, FlagType, InvalidVal, class >::operator= ( ResourceGuard< T, Relinquish, FlagType, InvalidVal, class > &&  other)
inline

◆ operator==()

template<class T , class Relinquish , class FlagType = bool, FlagType InvalidVal = std::is_lvalue_reference<T>::value, class = typename std::enable_if< !std::is_same<FlagType, bool>::value && (std::is_same<FlagType, bool>::value || std::is_same<FlagType, T>::value) && std::is_trivially_default_constructible<Relinquish>::value >::type>
constexpr bool taco::ResourceGuard< T, Relinquish, FlagType, InvalidVal, class >::operator== ( rhs) const
inlineconstexpr

◆ Release()

template<class T , class Relinquish , class FlagType = bool, FlagType InvalidVal = std::is_lvalue_reference<T>::value, class = typename std::enable_if< !std::is_same<FlagType, bool>::value && (std::is_same<FlagType, bool>::value || std::is_same<FlagType, T>::value) && std::is_trivially_default_constructible<Relinquish>::value >::type>
T taco::ResourceGuard< T, Relinquish, FlagType, InvalidVal, class >::Release ( )
inline

Releases the underlying resource without relinquishing it and returns it to the caller.

The ResourceGuard is set to an invalid state after this call.

◆ Reset()

template<class T , class Relinquish , class FlagType = bool, FlagType InvalidVal = std::is_lvalue_reference<T>::value, class = typename std::enable_if< !std::is_same<FlagType, bool>::value && (std::is_same<FlagType, bool>::value || std::is_same<FlagType, T>::value) && std::is_trivially_default_constructible<Relinquish>::value >::type>
void taco::ResourceGuard< T, Relinquish, FlagType, InvalidVal, class >::Reset ( )
inline

Relinquishes the underlying resource and sets its to an invalid value.

Member Data Documentation

◆ m_val

template<class T , class Relinquish , class FlagType = bool, FlagType InvalidVal = std::is_lvalue_reference<T>::value, class = typename std::enable_if< !std::is_same<FlagType, bool>::value && (std::is_same<FlagType, bool>::value || std::is_same<FlagType, T>::value) && std::is_trivially_default_constructible<Relinquish>::value >::type>
T taco::ResourceGuard< T, Relinquish, FlagType, InvalidVal, class >::m_val
private

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