Boost logo

Boost :

Subject: [boost] Any interest in dynamic dispatching mechanism from "interface" to template functions ?
From: Raffi Enficiaud (raffi.enficiaud_at_[hidden])
Date: 2010-07-01 06:32:02


Hello all,

The idea behind is not new and consists in dispatching a call of an
interface function to the correct template instance, depending on the
runtime or compile time parameters. The field of application is (to
me) the release of static/dynamic libraries including functions with a
predefined
set of types combination. This way, the library (compiled once) can
easily be plugged to another framework or a script program (I
personally use it with boost.python).

An example of use could be (general idea, not a real code of course):
// --------------

template <class real_type>
int template_function(real_type const& r_class1, double param_d, int&
param_out)
{
   // ...
   return 0;
}

int interface_function(MyInterface const* i_class1, variant const&
param, variant& out)
{
   // return value
   int return_value;

   // constructs the dispatcher object with a reference to the values
passed to interface_function
   dispatcher<int, MyInterface const*, variant const&, variant&>
dispatch_object(return_value, i_class1, param, out);

   bool res = dispatch_object.calls_first(
       fusion::vector_tie(
         template_function < class_t1 >,
         template_function < class_t2 > //...
         )
       );
   if(res) return return_value;
   return -1;
}

// --------------

The dispatch_object tests for the convertibility of each instance ui
of the arguments Ui of the interface function, with the corresponding
argument Ti
of the template function. The type of matching is defined by external
objects, with a simple interface providing two methods: a
convertibility test and a conversion.
The types are "cleaned" in order to have a limited number of
conversion structures.

Currently this mechanism is used in a generic image processing
library. The images have all the same virtual interface and the
dispatch calls
the correct template function based on the concrete type of the image.
It also includes a "back conversion" for Ui's that are non-const
reference or pointers.
The dispatch mainly makes use of boost.fusion, boost.mpl and
boost.function_types.

I have checked a little bit the Boost.Vault but, correct me if I am
wrong, the current dispatching mechanisms do not address exactly this
issue.
The code is already open-source released under the boost license. I
can put some samples in the Vault if there is any interest.

Your comments are very welcome !
Kind regards,
Raffi Enficiaud


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk