Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost Python - Overloaded Functions with default arguments problem

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

like image 658
Megatron Avatar asked May 18 '11 21:05

Megatron


People also ask

Can overloaded function have default arguments?

No you cannot overload functions on basis of value of the argument being passed, So overloading on the basis of value of default argument is not allowed either. You can only overload functions only on the basis of: Type of arguments. Number of arguments.

What is boost in Python?

The Boost Python Library is a framework for interfacing Python and C++. It allows you to quickly and seamlessly expose C++ classes functions and objects to Python, and vice-versa, using no special tools -- just your C++ compiler.


1 Answers

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).

like image 57
John Zwinck Avatar answered Sep 19 '22 12:09

John Zwinck