이 얼마나 아름다운 걸작인가



#include <iostream>

#include <string>

#include <map>

#include <vector>

#include <algorithm>

#include <functional>

using namespace std;

using void_fn_ptr = std::function<void(void)>;


void Function(bool _is_value, int _value)

{

    cout << "전역 함수" << _is_value << " " << _value << endl;

}


class Caller

{

    std::vector<std::function<void(bool, int)>> callbacks_;


public:


    template<class T> void addCallback(T* const object, void(T::* const mf)(bool, int))

    {

        using namespace std::placeholders;

        callbacks_.emplace_back(std::bind(mf, object, _1, _2));

    }


    void addCallback(void(* const fun)(bool, int))

    {

        callbacks_.emplace_back(fun);

    }


    void callCallbacks(bool firstval, int secondval)

    {

        for (const auto& cb : callbacks_)

             cb(firstval, secondval);

    }

};


class Callee

{

    Caller* ptr;


public:


    Callee()

    {

        ptr = new Caller();

        //then, somewhere in Callee, to add the callback, given a pointer to Caller `ptr`

        ptr->addCallback(this, &Callee::MyFunction);


        //or to add a call back to a regular function

        ptr->addCallback(&Function);

    }


    Caller* Get_ptr()

    {

        return ptr;

    }


    void MyFunction(bool _is_value, int _value)

    {

        cout << "Callee 멤버 함수 " << _is_value << " " << _value << endl;

    }

};


void main()

{

    Callee callee;


    callee.Get_ptr()->callCallbacks(true, 100);

}