Extending Python with C++ singleton pattern using boost python lib
Benjamin Geer
benjamin.geer at btinternet.com
Wed Apr 25 21:40:46 EDT 2001
In article <9c0tdd02sa7 at news2.newsguy.com>, "Alex Martelli"
<aleaxit at yahoo.com> wrote:
> It's hard to avoid that: in general, it's VERY hard work to
> wrap/expose/handle non-canonical C++ objects (ones that can't be
> default-constructed, destructed, copied, assigned). Not just in/to
> Python or in/to Boost Python specifically: non canonical objects are Bad
> News in any substantial C++ system.
It seems to me that this might be true if you pass objects by value and/or
reference, but where's the difficulty if you only pass pointers? A
singleton can surely make itself available exactly as described in Gamma et. al,
via a static method that returns a pointer to the instance, with very
little effort required, either in the singleton or in its clients.
Copying and assigning pointers is considerably easier than writing copy
constructors and copy assignment methods...
If you want polymorphism, you're forced to use pointers or references,
but references have to refer to something that lives either on the heap
or on the stack. If it lives on the stack, you have all the problems of
copy constructors, etc. If it lives on the heap, you have to use
pointers anyway...
If you use garbage collection, there's no way to avoid pointers.
Finally, if you're using SWIG to wrap your Python extension, providing
pointers to your objects is the the most straighforward approach, unless
you want to write a lot of glue code by hand.
So why not make all your copy constructors private, use pointers for
everything, write singleton classes the way the Gang of Four suggest, and
make a singleton available in a Python module?
--
Benjamin Geer
http://www.btinternet.com/~benjamin.geer
More information about the Python-list
mailing list