[C++-sig] Boost.Python property definition question

Jim Bosch talljimbo at gmail.com
Tue Jan 31 17:56:54 CET 2012


On 01/31/2012 12:41 AM, Michael Wild wrote:
> On 01/30/2012 07:28 PM, Jim Bosch wrote:
>> On 01/30/2012 12:11 PM, Michael Wild wrote:
>>> That's what I've been referring to as "auxiliary" functions. If
>>> possible, I'd like to avoid them because I don't fancy writing hundreds
>>> of those... I could live with calling a template. However, so far I
>>> haven't been able to come up with one that is sufficiently easy to use...
>>>
>>> So far I came up with the this:
>>>
>>> template<class ArgType, class T, T&(C::*method)()>
>>> void set_helper(T&   self, ArgType&   arg)
>>> {
>>>     (self.*method)() = arg;
>>> }
>>>
>>> But it is a bit cumbersome to use, as it doesn't deduce its first two
>>> template arguments on its own. Is there any way to achieve that feat?
>>>
>>
>> If you're up for playing with Boost.FunctionTypes, you should be able to
>> write a wrapper for make_function that deduces those types for you:
>>
>> namespace bp = boost::python;
>>
>> template<typename A, typename T, A&(T::*method)()>
>> void set_helper(T&  self, A const&  arg) {
>>      (self.*method)() = arg;
>> }
>>
>> template<class F>
>> bp::object make_set_helper(F f) {
>>      // F is the member function type; use Boost.FunctionTypes to
>>      // figure out A and T, then...
>>      return bp::make_function(&set_helper<A,T,f>);
>> }
>>
>>
>> I'll leave it to you to fill in the details.  I haven't tried it myself,
>> but I think it would work.
>>
>>
>> Jim
>
> Thanks for that hint, I'll certainly take a look. The only think that
> would bother me with this solution is that I would need to disambiguate
> between the non-const and const functions with the casting operator again.
>

You might be able to address that by having two make_helper functions, 
using enable_if/disable_if to select one when the return type is const 
and the other when it isn't.  The one invoked for const returns would 
just call make_function directly to make a getter, while the non-const 
one would be like make_set_helper above.


Jim


More information about the Cplusplus-sig mailing list