Author: vitalyisaev2. Date 2014-03-24 16:46:09, views: 827, Raw

Увеличить
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/assert.hpp>
#include <iostream>
#include <map>
 
template <typename Arg, typename Callback>
class CallbackSelector
{
    private:
        const std::map<Arg, Callback> Mapping;
 
    public:
        CallbackSelector(std::pair<Arg, Callback> _Mapping[]):
            Mapping(_Mapping, _Mapping + sizeof(_Mapping)/sizeof(std::pair<Arg, Callback>))
            {
                //BOOST_ASSERT(sizeof _Mapping)/(sizeof (std::pair<Arg, Callback>)) == 2);
                std::cout << "sizeof _Mapping " << sizeof _Mapping << std::endl;
                std::cout << "sizeof (std::pair<Arg, Callback>) " << sizeof (std::pair<std::string, boost::function<void(void)> >) << std::endl;
            };
};
 
void PamEvent()
{ std::cout << "\t" <<  __PRETTY_FUNCTION__  << std::endl; }
 
void DefaultEvent()
{ std::cout << "\t" <<  __PRETTY_FUNCTION__  << std::endl; }
 
int main(int argc, char** argv)
{
    std::pair<std::string, boost::function<void(void)> > _Mapping[] =
    {
        std::make_pair("pam:", boost::bind(&PamEvent)),
        std::make_pair("none", boost::bind(&DefaultEvent))
    };
    std::cout << "sizeof _Mapping " << sizeof _Mapping << std::endl;
    std::cout << "sizeof (std::pair<Arg, Callback>) " << sizeof (std::pair<std::string, boost::function<void(void)> >) << std::endl;
 
    CallbackSelector<std::string, boost::function<void(void)> > Selector(_Mapping);
}