#pragma once
#include <functional>
template<class T>
class GuardCls
{
public:
using param_t = typename std::conditional<std::is_reference<T>::value || std::is_pointer<T>::value, T, T&>::type;
using func_t = std::function<void(param_t)>;
GuardCls(param_t param, func_t&& clear) :m_param(param), m_clear(std::move(clear)) {}
GuardCls(param_t param, func_t&& init, func_t&& clear) :m_param(param), m_clear(std::move(clear))
{
init(m_param);
}
~GuardCls() { m_clear(m_param); }
GuardCls() = delete;
GuardCls(const GuardCls&) = delete;
GuardCls& operator=(const GuardCls&) = delete;
GuardCls(const GuardCls&&) = delete;
GuardCls& operator=(const GuardCls&&) = delete;
private:
param_t m_param;
func_t m_clear;
};
#include "GuardCls.hpp"
#include <iostream>
using namespace std;
#define FUNCLOG \
GuardCls<const char*> guardCls(__FUNCTION__, \
[](const char* func) {cout << "enter function:" << func << endl; }, \
[](const char* func) {cout << "leave function:" << func << endl; });
void TestFunction()
{
FUNCLOG;
cout << "record log..." << endl;
}
int main()
{
TestFunction();
return 0;
}