[C++-sig] [Boost.Python v3] Conversions and Registries

Dave Abrahams dave at boostpro.com
Thu Oct 6 10:31:23 CEST 2011


on Wed Oct 05 2011, Jim Bosch <talljimbo-AT-gmail.com> wrote:

> On 10/05/2011 11:30 AM, Dave Abrahams wrote:
>
>> on Wed Oct 05 2011, Jim Bosch<talljimbo-AT-gmail.com>  wrote:
>>
>>> With runtime conversions, I have to explicitly declare all the
>>> template parameter combinations I intend to use.
>>
>> Not really; a little metaprogramming makes it reasonably easy to
>> generate all those combinations.  You can also use compile-time triggers
>> to register runtime converters.  I'm happy to demonstrate if you like.
>
> The latter sounds more like what I'd want, though a brief
> demonstration would be great.  You're right in guessing that I don't
> really care whether it's a runtime or compile-time conversion.   The
> key is that I don't want to have to explicitly declare the
> conversions, even if I have some metaprogramming to make that easier -
> I'd like to only declare what's actually used, since that's
> potentially a much smaller number of declarations.

I'm not sure exactly what you have in mind when you say "declare what's
actually used," because you haven't said what counts as "usage."  That
said, I can give you an abstract description.

It's just a matter of being able to "hitch a ride" at its point-of-use:
arrange for some customization point to be instantiated during "use"
that you can customize elsewhere.  That customization then is where you
register the type.

For example, let's imagine that by saying a type T is used you mean it's
a parameter or return value of a wrapped function.  Then you might
designate this class template to be instantiated and its constructor
called:

  namespace boost { namespace python { namespace user_hooks {

  // users are encouraged to specialize templates in this namespace

  template <class T> 
  struct is_used
  {
      is_used() { /* do nothing by default */ }
  };

  }}}

In A user's wrapping code, she could make a partial specialization of this
class template:

  namespace boost { namespace python { namespace user_hooks {

  template <class U1, class U2, class U3>
  struct is_used<my_template<U1,U2,U3> >
  {
      is_used()
      {
          my_register_converter<U1,U2,U3>();
      }
  };

  }}}   

The other way to make customization points like this uses argument
dependent lookup and is usually less verbose, though brings with it
other sticky problems that you probably want to avoid.

> Doing something that's only a small modification to the current
> single-registry model is also very appealing from an
> ease-of-implementation standpoint too, and it would also be sufficient
> for my own needs.
>
> I'd like to see what Stefan's ideas are first, of course, and I should
> take a look at some of the code Niall has pointed me at to see if I
> can take some steps towards a design that would meet his needs as
> well.  But at the moment I'm inclined to go with something pretty
> similar to the current design to keep this problem from overshadowing
> and swallowing all the other things I'd like to go into the upgrade.

Good idea.

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com



More information about the Cplusplus-sig mailing list