FX::FXCodeToPythonCode< vector, length > Class Template Reference
[Python Support]

#include <FXCodeToPythonCode.h>

List of all members.


Detailed Description

template<template< typename, int > class vector, int length>
class FX::FXCodeToPythonCode< vector, length >

Allows you to convert calls to C++ code to calls to Python code.

I had the idea for this in the shower yesterday and it really does show the power of TnFOX's generic tools. This class lets you create an array of code entry points of your choice of parameters & return which can be attached to code objects within Python. This ability solves one major failing of Boost.Python which prevents GUI list item sorting in FOX - the inability to set python code as the sort function. No doubt there are many other uses too, which is why this class has been made public.

Upon instantiation, this class creates length code entry points attached to length python code objects. You allocate a code entry, set it to its corresponding python code object and pass the returned function pointer to whichever C++ class. All calls made by the C++ class to that function pointer will now call the python code object. Asking to allocate for an already existing code object returns the original function pointer ie; it's a once off allocation. Because the tables must be fixed in size, you must take care not to have more than the maximum total code objects than the table can hold.

FXCodeToPythonCode is a policy-driven class whereby you must provide a policy specifying what kind of code we are vectoring:

template<typename base, int no> struct myVector : public base
{
    typedef Generic::TL::create<returnType, firstParType &firstPar, secondParType &secondPar>::value functionSpec;
    static returnType function(firstParType &firstPar, secondParType &secondPar)
    {
        return call<returnType>(getParent()->lookupVector(no).ptr(), boost::ref(firstPar), boost::ref(secondPar));
    }
};
ie; the type of the vector as functionSpec and the vector itself following the same prototype. This should call boost::python::extract() if there is a return value to extract, otherwise you can safely throw away the return.

Your first reaction will likely be to wonder why the duplication? Unfortunately the current ISO C++ spec does not let you obtain function types except via template argument deduction and while you could wrap the policy creation in such, it gets rapidly very complicated for very little benefit. The above I believe is a good balance.

See also:
FX::FXPython

Definition at line 79 of file FXCodeToPythonCode.h.

Public Member Functions

boost::python::object & lookupVector (int idx)
 FXCodeToPythonCode ()
vectorSpec allocate (boost::python::api::object *code)
bool deallocate (boost::python::api::object *code)
bool deallocate (vectorSpec spec)


The documentation for this class was generated from the following file:
(C) 2002-2009 Niall Douglas. Some parts (C) to assorted authors.
Generated on Fri Nov 20 18:32:29 2009 for TnFOX by doxygen v1.4.7