larroy / uvpp

C++11 libuv wrapper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

release mode dynamic_cast in callback.hpp

steve-lorimer opened this issue · comments

template<typename callback_t, typename ...A>
static typename std::result_of<callback_t(A...)>::type invoke(void* target, int cid, A&& ... args)
{
    auto x = dynamic_cast<internal::callback_object<callback_t>*>(reinterpret_cast<callbacks*>(target)->m_lut[cid].get());
    assert(x);
    return x->invoke(std::forward<A>(args)...);
}

it would be better to use a boost::polymorphic_downcast, or if you don't want the dependency on boost, add a helper function:

template <class Target, class Source>
inline Target polymorphic_downcast(Source* x)
{
    assert(dynamic_cast<Target>(x) == x);
    return static_cast<Target>(x);
}

template<typename callback_t, typename ...A>
static typename std::result_of<callback_t(A...)>::type invoke(void* target, int cid, A&& ... args)
{
    auto x = polymorphic_downcast<internal::callback_object<callback_t>*>(reinterpret_cast<callbacks*>(target)->m_lut[cid].get());
    return x->invoke(std::forward<A>(args)...);
}

Agreed, feel free to send a pull request.