1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-10-27 05:23:39 -04:00
polybar/include/utils/scope.hpp

34 lines
573 B
C++
Raw Normal View History

2016-06-14 23:32:35 -04:00
#pragma once
#include "common.hpp"
2016-11-19 00:22:44 -05:00
POLYBAR_NS
2016-06-14 23:32:35 -04: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 15:40:48 -05:00
* @code cpp
2016-06-14 23:32:35 -04:00
* {
2022-03-06 11:02:28 -05:00
* on_exit handler([]{ ... });
2016-06-14 23:32:35 -04:00
* ...
* }
2022-02-20 15:40:48 -05:00
* @endcode
2016-06-14 23:32:35 -04:00
*/
2022-03-06 11:02:28 -05:00
class on_exit {
public:
on_exit(const function<void(void)>& fn) : m_callback(fn) {}
virtual ~on_exit() {
m_callback();
}
protected:
2022-03-06 11:44:48 -05:00
function<void(void)> m_callback;
2022-03-06 11:02:28 -05:00
};
} // namespace scope_util
2016-06-14 23:32:35 -04:00
2016-11-19 00:22:44 -05:00
POLYBAR_NS_END