7

I have a class that has two functions, both of which take a different set of parameters and both of which have default arguments like so:

void PlaySound(const std::string &soundName, int channel = 0, bool UseStoredPath = true);

void PlaySound(FMOD::Sound* sound, int channel = 0);

I've found how to do default argument overloads from the tutorial here

http://www.boost.org/doc/libs/1_37_0/libs/python/doc/v2/overloads.html

as well as how to do function overloads taking different parameter types here

http://boost.2283326.n4.nabble.com/Boost-Python-def-and-member-function-overloads-td2659648.html

and I end up doing something like this...

BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(PlaySoundFromFile, Engine::PlaySound, 1, 3)
BOOST_PYTHON_MODULE(EngineModule)
{
    class_<Engine>("Engine")
        //Sound
        .def("PlaySound", static_cast< void(Engine::*)(std::string, int, bool)>(&Engine::PlaySound));
}

The problem is I really have no idea how to use them together at the same time. I'd like to avoid having to change my base class function definitions.

Can someone who's done this before, or knows how to do this help me out?

Thanks in advance

2

1 Answer 1

11

This works for me:

BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(
    PlaySoundFromFile, Engine::PlaySound, 1, 3)
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(
    PlaySoundFromFMOD, Engine::PlaySound, 1, 2)

BOOST_PYTHON_MODULE(EngineModule)
{
    class_<Engine>("Engine")
        .def("PlaySound", static_cast< void(Engine::*)
            (const std::string&, int, bool)>
            (&Engine::PlaySound), PlaySoundFromFile())
        .def("PlaySound", static_cast< void(Engine::*)
            (FMOD::Sound*, int)>
            (&Engine::PlaySound), PlaySoundFromFMOD())
    ;
}

The trick is that you need to tell each def() to use one of the overload specifiers (that seemed to be the biggest missing piece from what you had).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.