Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Extension with factory

Reply
Thread Tools

Extension with factory

 
 
CarlosP
Guest
Posts: n/a
 
      12-24-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Abstract factory and Factory pattern C# ASP .Net 4 07-31-2008 03:22 PM
New extension? saw extension .emf is it safe to open Jer Computer Support 5 10-08-2005 04:43 PM
creating factory methods in C extension modules. Patrick Stinson Python 0 06-09-2004 05:50 PM
Unload extension modules when python22.dll unloads... [using C extension interpreter] Anand Python 3 11-08-2003 05:50 AM
Abstract Factory or Factory Method pattern question.... Medi Montaseri C++ 17 09-03-2003 06:50 AM



Advertisments