#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
using void_two_params_pointer = std::function<void(bool, int)>;
void Function(bool _is_value, int _value) { cout << "전역 함수" << _is_value << " " << _value << endl; }
class Callback
{
std::vector<void_two_params_pointer> m_vec_callbacks;
public:
template<class T> void Add_callback(T* const _object, void(T::* const _member_fn)(bool, int))
{
using namespace std::placeholders;
m_vec_callbacks.emplace_back(std::bind(_member_fn, _object, _1, _2));
}
void Add_callback(void_two_params_pointer _fn) { m_vec_callbacks.emplace_back(_fn); }
void Call_callbacks(bool _first_val, int _second_val)
{
for (const auto& cb : m_vec_callbacks)
cb(_first_val, _second_val);
}
};
class Test_callback
{
Callback* p_callback = nullptr;
public:
Test_callback()
{
// 함수 포인터 바인딩
p_callback = new Callback();
p_callback->Add_callback(this, &Test_callback::MyFunction);
p_callback->Add_callback(&Function);
}
Callback* Get_ptr() { return p_callback; }
void MyFunction(bool _is_value, int _value) { cout << "Test_callback 멤버 함수 " << _is_value << " " << _value << endl; }
};
void main()
{
Test_callback callback;
callback.Get_ptr()->Call_callbacks(true, 100);
}