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

Michael Wild themiwi at users.sourceforge.net
Tue Jan 31 20:52:45 CET 2012


On 01/31/2012 05:56 PM, Jim Bosch wrote:
> 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

Sorry, I think you'll have to expand a bit on that. Do you mean that I
can use enable_if/disable_if to select between the two implementations?

Michael


More information about the Cplusplus-sig mailing list