[C++-sig] Keyed properties?

Matthew Scouten (TT) Matthew.Scouten at tradingtechnologies.com
Tue Aug 4 15:44:12 CEST 2009


You can solve it pretty much the same way. Expose a function
__*etitem__. 

It would look something like this:

void set(thingy_t thingy&, int index, value_type& val){...}
value_type get(const thingy_t thingy&, int index ){...}

And then you export them in the class_ exposer:

class_< thingy_t >("thingy_t")
	.def("__getstate__", &get)
	.def("__setstate__", &set)

One wrinkle to watch out for: remember not to name the c++ functions
starting with '__'
Initial double underscores are reserved for the compiler.   


-----Original Message-----
From:
cplusplus-sig-bounces+matthew.scouten=tradingtechnologies.com at python.org
[mailto:cplusplus-sig-bounces+matthew.scouten=tradingtechnologies.com at py
thon.org] On Behalf Of Jakob van Santen
Sent: Tuesday, August 04, 2009 8:23 AM
To: cplusplus-sig at python.org
Subject: [C++-sig] Keyed properties?

Hello,

I'm in the process of hiding the unnecessarily verbose parts of a C++  
interface behind Python properties, and I've run in to a slight  
problem. We have a bunch of classes that expose private std::vectors  
via Get(int index) and Set(int index, value_type& val) methods.  
Ideally, I'd like to replace

thingy.GetATWDGain(1)
thingy.SetATWDGain(1, 254.5)

with

thingy.atwd_gain[1]
thingy.atwd_gain[1] = 254.5

I know how I would solve this in pure Python (something similar to  
this: http://code.activestate.com/recipes/440514/), but I'm wondering  
if there's a clever way to do this in Boost.Python without messing  
around with the library itself. If anyone has done this already, I'd  
be curious to know how.

Cheers,
Jakob van Santen
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig at python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig


More information about the Cplusplus-sig mailing list