Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release mode dynamic_cast in callback.hpp #3

Open
steve-lorimer opened this issue Dec 30, 2014 · 1 comment
Open

release mode dynamic_cast in callback.hpp #3

steve-lorimer opened this issue Dec 30, 2014 · 1 comment

Comments

@steve-lorimer
Copy link

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)...);
}
@larroy
Copy link
Owner

larroy commented Feb 24, 2015

Agreed, feel free to send a pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants