Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    570
  • comments
    2427
  • views
    216941

Untitled

Sign in to follow this  
Mushu

328 views

So I'm designing the interface between Python<->C++. Now, there already is an interface. But you have to code your functions specifically so they match the typedef -

C++:
PyObject* ( *func )( PyObject*, PyObject* )

Which is bad, because we want to integrate Python directly into our program without having to specificially mark functions like THIS_IS_A_PYTHON_FUNCTION_LOL because that's silly.

So, what I've got in mind is an intermediate function which intercepts the call from Python, parses the tuple that the arguments come packed in, then calls the appropriate C-function.

The problem is the syntax. The only way I can figure out how to do it is to store the C-functions in a string-to-functor map. Which means, from python, you'd use something like -

Python:
module.call( "funcname", arg1, arg2, arg3, etc )

Which is, essentially, the same method I'm using to call Python functions from C++ -

C++:
python::call( "funcname", arg1, arg2, arg3, etc )

Now, ideally, you'd want to bind your functions directly to uniquely-named identifiers in Python (which can be done). The problem with this is the fact that the intermediate function needs to be called to resolve the arguments (from either direction, C->Python calls need to have their arguments packed into a tuple then passed to Python too).

Likewise, you also need to expose functionality between the two languages. This is done with the expose function -

Python:
module.expose( some_function, "func_name_to_bind_to" )

C++:
python::expose( some_functor, "func_name_to_bind_to" )

Again, its very symmetric, which is a good thing and a bad thing. Its good because its somewhat consistant. Its bad because it doesn't make full use of the power of Python; its essentially limited by the bounds of what C++ can do.

What I really need is a way to retrieve the name of the function being called from Python, so if, say -

Python:
module.somefunc(..)

Were called, I'd be able to retrieve the string somefunc. This would allow me to go beyond the call/expose pattern by actually adding functions into the module dictionary (which you can't really do in C++).

Anyway. I have to go implement all of this... after lunch. Mmmm I need lunch.
Sign in to follow this  


1 Comment


Recommended Comments

Guest Anonymous Poster

Posted

You might be able to do the string thing with some special method names. I'm not sure if that applies to modules, though... maybe it only works for classes.
I think that a better way to go would be to create a wrapper function and associate a closure with it. The closure would store a pointer to a functor or a function, which the wrapper would then call with the unpacked arguments. Check the docs.
Disclaimer: I've never actually tried this :P. Good luck!

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!