ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> cpp-sig
cpp-sig
[C++-sig] fat class decoration
by Mike Rovner other posts by this author
Oct 23 2003 3:33PM messages near this date
[C++-sig] Exporting STL List to Python | [C++-sig] Re: Extension DLLs nonfunctioning with MSVC 7.1 (beta)
Hi,

I solicit a criticism and possible better solutions to the following
problem.

I have a nonmodifyable C++ class

class T
{
   public:
     Color* getColor(const char* name);
     void     setColor(const char* name, const Color* value);

     Line*  getLine(const char* name);
     void     setLine(const char* name, const Line* value);

     //and so on
};
with a bunch of classes Color, Line, etc.

I want pretend in Python interface that T has bunch of attributes 'color',
'line', etc.
that behave like mappings each, i.e. I want to be able to say in Python:

t=T()
t.color["a"]=Color(...)
print t.line["b"]

For that purpose I created helper classes and generator functions

class TColorHelper
{
    T& _t;
public:
    TColorHelper(T& t) : _t(t) {}
    Color* get(const char* name) { return _t.getColor(name); }
    void     set(const char* name, const Color* value)
{_t.setColor(name,value);}
};
TColorHelper color(T& t) { return TColorHelper(t); }

class TLineHelper {...}; line(){...} //and so on for each property

wrap them:

 class_<TColorHelper> ("TColorHelper", no_init)
  .def("__getitem__", &TColorHelper::get, return_internal_ref<> ())
  .def("__setitem__", &TColorHelper::set)
  ;
//...
class_<T> ("T")
  .add_property("color", color)
  .add_property("line", line)
  ;

which works fairly well.

So what do think about it?
Maybe you have had similar problem and solved it differently.
I appreciate your comments.

Thanks,
Mike




_______________________________________________
C++-sig mailing list
C++-sig@[...].org
http://mail.python.org/mailman/listinfo/c++-sig

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved