[C++-sig] Boost.Python code generator

Nicodemus nicodemus at globalite.com.br
Thu Dec 5 01:01:15 CET 2002


Hail to all.

A collegue and I are planning to port the C++ code base of our company 
to Python. After checking both swig and boost.python, we opted for 
boost, because of its flexibility and cleanness. 8)
After various tests, we felt that we could automatize the process, using 
a boost.python code generator. I would like to present our ideas to the 
boost.python readers to gather comments and suggestions.

We opted for the use of a "interface file" to expose the C++ class to 
our code generator. It's main design goals are:

- Simplicity
- Independent from header files (self-contained)
- Export classes that:
        * are templates
        * have virtual functions
        * have functions with default arguments
        * enums

Our motivations are:
- The interface file is simplier than write boost code directly
- It separates the code that exports the class from the module where the 
class will be exported (more on that later).
- It is not intrusive to the library being exported (unlike other 
solutions, like the use of "tags" in the header for exposing the functions).

We are planning to call it Pyste (Python Semiautomatic Type Exporter), 
but that can change in the future. 8)


The usage process would be as follows:

- The user creates a interface file for the header that he/she plans to 
export. So, for two header files declaring two classes, A.h and B.h, the 
user creates interface files A.pyi and B.pyi (examples later).

- The user runs in the command line:

    pyste -module test A.pyi B.pyi

This command generates one file, test.cpp, which contains boost code 
that exposes the test module and its classes to python.

- The user compiles the generated test.cpp as a dynamic library, as 
he/she would do with boost.python.


Here is a simple class to exemplify:

file A.h
------------------------------------------------------------------------
#include <string>
#include "B.h"

namespace test {

class A: public B
{
public:
    virtual void foo( int x );
    const std::string & getName() const;
    int value;
private:
    std::string s;
};

}
------------------------------------------------------------------------


The interface file would be as follows (the syntax is not fixed yet):

file A.pyi
------------------------------------------------------------------------
include test/A.h        # this will just change to '#include "test/A.h"'
                        # in the generated code, ie, the header
                        # will not be actually parsed by pyste

class test.A(base test.B)
    def foo (virtual void-int) # specify the return value and the parameters
    def getName (return internal_reference)

    attr value
------------------------------------------------------------------------


What do you think? Would it be of use to other people, in your opinion?
I would very much appreciate comments and suggestions.

Farewell,
Nicodemus.






More information about the Cplusplus-sig mailing list