iceoryx_hoofs 2.0.5
Loading...
Searching...
No Matches
Public Member Functions | List of all members
iox::cxx::GenericRAII Class Reference

The GenericRAII class is a simple helper class to apply the C++ RAII idiom quickly. You set 2 functions, one which is called in the constructor and another function is called in the destructor which can be useful when handling resources. More...

#include <iceoryx_hoofs/cxx/generic_raii.hpp>

Public Member Functions

 GenericRAII (const std::function< void()> &cleanupFunction) noexcept
 constructor which creates GenericRAII that calls only the cleanupFunction on destruction
 
 GenericRAII (const function_ref< void()> &initFunction, const std::function< void()> &cleanupFunction) noexcept
 constructor which calls initFunction and stores the cleanupFunction which will be called in the destructor
 
 ~GenericRAII () noexcept
 calls m_cleanupFunction callable if it was set in the constructor
 
 GenericRAII (const GenericRAII &)=delete
 
GenericRAIIoperator= (const GenericRAII &)=delete
 
 GenericRAII (GenericRAII &&rhs) noexcept
 move constructor which moves a generic raii object without calling the cleanupFunction
 
GenericRAIIoperator= (GenericRAII &&rhs) noexcept
 move assignment which moves a generic raii object without calling the cleanupFunction
 

Detailed Description

The GenericRAII class is a simple helper class to apply the C++ RAII idiom quickly. You set 2 functions, one which is called in the constructor and another function is called in the destructor which can be useful when handling resources.

// This example leads to a console output of:
// hello world
// I am doing stuff
// goodbye
void someFunc() {
auto raii{[](){ std::cout << "hello world\n"; },
[](){ std::cout << "goodbye\n"; }};
std::cout << "I am doing stuff\n";
// raii goes out of scope here and the cleanupFunction is called in the
// destructor
}

Constructor & Destructor Documentation

◆ GenericRAII() [1/2]

iox::cxx::GenericRAII::GenericRAII ( const std::function< void()> &  cleanupFunction)
explicitnoexcept

constructor which creates GenericRAII that calls only the cleanupFunction on destruction

Parameters
[in]cleanupFunctioncallable which will be called in the destructor

◆ GenericRAII() [2/2]

iox::cxx::GenericRAII::GenericRAII ( const function_ref< void()> &  initFunction,
const std::function< void()> &  cleanupFunction 
)
noexcept

constructor which calls initFunction and stores the cleanupFunction which will be called in the destructor

Parameters
[in]initFunctioncallable which will be called in the constructor
[in]cleanupFunctioncallable which will be called in the destructor

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