473,385 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Extension with factory

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

Jul 18 '05 #1
0 1096

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
4
by: Eric | last post by:
Perhaps this question has been posed before (I'd be surprised if it hasn't) but I just gotta know... Is it possible to combine the Singleton and Factory Method design patterns in the same class?...
10
by: Chris Croughton | last post by:
What do people call their factory functions? 'new' is not an option (yes, it can be overloaded but has to return void*). The context is: class MyClass { public: // Factory functions...
6
by: Dave | last post by:
Hello all, Please see my question embedded in comment form below. Thanks, Dave #include <iostream> #include <boost/shared_ptr.hpp>
8
by: deko | last post by:
Which layer should a Factory class go in? DA - Data Access BL - Business Logic UI - User Interface ??
6
by: Nindi | last post by:
5 Files Singleton.h The Singleton Factory.h The factory creating new objects. The Base class of the hierachy stores a typelist identifying the signature of the constructors to be called...
8
by: googlegroups | last post by:
Hi, I need to parse a binary file produced by an embedded system, whose content consists in a set of events laid-out like this: <event 1<data 1<event 2<data 2... <event n<data n> Every...
1
by: Simon Woods | last post by:
Hi I have 2 classes which are both instantiated by shared factory methods and both implement a common interface. Public Class Factory Public Shared Function CreateMyClass1() As MyClass1...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.