polybar/include/utils/scope.hpp

34 lines
573 B
C++
Raw Normal View History

2016-06-15 03:32:35 +00:00
#pragma once
#include "common.hpp"
2016-11-19 05:22:44 +00:00
POLYBAR_NS
2016-06-15 03:32:35 +00:00
namespace scope_util {
/**
* Creates a wrapper that will trigger given callback when
* leaving the object's scope (i.e, when it gets destroyed)
*
* Example usage:
2022-02-20 20:40:48 +00:00
* @code cpp
2016-06-15 03:32:35 +00:00
* {
2022-03-06 16:02:28 +00:00
* on_exit handler([]{ ... });
2016-06-15 03:32:35 +00:00
* ...
* }
2022-02-20 20:40:48 +00:00
* @endcode
2016-06-15 03:32:35 +00:00
*/
2022-03-06 16:02:28 +00:00
class on_exit {
public:
on_exit(const function<void(void)>& fn) : m_callback(fn) {}
virtual ~on_exit() {
m_callback();
}
protected:
2022-03-06 16:44:48 +00:00
function<void(void)> m_callback;
2022-03-06 16:02:28 +00:00
};
} // namespace scope_util
2016-06-15 03:32:35 +00:00
2016-11-19 05:22:44 +00:00
POLYBAR_NS_END