mirror of
				https://github.com/Icinga/icinga2.git
				synced 2025-11-03 21:25:56 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			55 lines
		
	
	
		
			745 B
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			55 lines
		
	
	
		
			745 B
		
	
	
	
		
			C++
		
	
	
	
	
	
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
 | 
						|
 | 
						|
#ifndef DEFER
 | 
						|
#define DEFER
 | 
						|
 | 
						|
#include <functional>
 | 
						|
#include <utility>
 | 
						|
 | 
						|
namespace icinga
 | 
						|
{
 | 
						|
 | 
						|
/**
 | 
						|
 * An action to be executed at end of scope.
 | 
						|
 *
 | 
						|
 * @ingroup base
 | 
						|
 */
 | 
						|
class Defer
 | 
						|
{
 | 
						|
public:
 | 
						|
	inline
 | 
						|
	Defer(std::function<void()> func) : m_Func(std::move(func))
 | 
						|
	{
 | 
						|
	}
 | 
						|
 | 
						|
	Defer(const Defer&) = delete;
 | 
						|
	Defer(Defer&&) = delete;
 | 
						|
	Defer& operator=(const Defer&) = delete;
 | 
						|
	Defer& operator=(Defer&&) = delete;
 | 
						|
 | 
						|
	inline
 | 
						|
	~Defer()
 | 
						|
	{
 | 
						|
		if (m_Func) {
 | 
						|
			try {
 | 
						|
				m_Func();
 | 
						|
			} catch (...) {
 | 
						|
				// https://stackoverflow.com/questions/130117/throwing-exceptions-out-of-a-destructor
 | 
						|
			}
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	inline
 | 
						|
	void Cancel()
 | 
						|
	{
 | 
						|
		m_Func = nullptr;
 | 
						|
	}
 | 
						|
 | 
						|
private:
 | 
						|
	std::function<void()> m_Func;
 | 
						|
};
 | 
						|
 | 
						|
}
 | 
						|
 | 
						|
#endif /* DEFER */
 |