Extension with factory

Discussion in 'Python' started by CarlosP, Dec 24, 2004.

  1. CarlosP

    CarlosP Guest

    Hi!

    I'm writing a C/C++ extension to python. I need to add two python types
    which will be implemented as C++ classes. One of the classes acts as
    (besides other functions) a factory for the other. Specifically, one is
    Package and the other Unit, the createUnit method of Package lets you
    create Units which in turn cannot be created independently (I mean from
    "outside" the Package). The python object structures are defined as:

    typedef struct {
    PyObject_HEAD
    Package* package;
    } CPackage

    typedef struct {
    PyObject_HEAD
    Unit* cunit;
    } CUnit

    And the python types structures are:

    static PyTypeObject CPackage_type = {
    .....
    }

    static PyTypeObject CUnit_type = {
    .....
    }

    So I'm providing a tp_new implementation for CPackage_type as usual.
    But I'm not sure how to cope with creation of CUnits. A Unit instance
    should be created by Package.createUnit in the C++ heap using the new
    operator. This instance should be wrapped as a CUnit and returned from
    CPackage_methods_createUnit.

    How should I do that? If I provided a tp_new for CUnit_type and invoked
    it from CPackage_methods_createUnit, the tp_new would take PyObjects as
    args and I would not be able to pass it the Unit* returned by
    Package.createUnit which should be asigned to CUnit->cunit to be
    wrapped as explained before. In pseudo code:

    PyObject* CPackage_methods_createUnit(PyObject *self, PyObject *args) {
    char *name;
    if (!PyArg_ParseTuple(args, "s", &name)) return NULL;
    Unit *unit = ((CPackage*)self)->cpackage->createUnit(name);
    return createCUnitFromUnit(unit);
    }

    Reformulating the question, how should I implement createCUnitFromUnit?
    Thank you in advance.
    Regards,
    Carlos
     
    CarlosP, Dec 24, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Medi Montaseri
    Replies:
    17
    Views:
    921
    Medi Montaseri
    Sep 3, 2003
  2. Anand
    Replies:
    3
    Views:
    921
    Tim Daneliuk
    Nov 8, 2003
  3. Christian Seberino
    Replies:
    3
    Views:
    1,221
    Christian Seberino
    Feb 5, 2004
  4. Patrick Stinson
    Replies:
    0
    Views:
    281
    Patrick Stinson
    Jun 9, 2004
  5. C#
    Replies:
    4
    Views:
    440
Loading...

Share This Page