Boost logo

Boost Users :

From: Johannes Stallkamp (jstallkamp_at_[hidden])
Date: 2008-03-31 06:00:43


Hello there,

Short version:
Are there any reasons, why base class access is currently not
allowed/implemented in boost::function (via target() function)?

Long version/explanation/scenario:

I have a class using boost::any which I use for data exchange between
different modules of my program. Some modules are able to process the
data if it contains "numbers" (in contrast to other arbitrary data
structures), ie. basically POD.

These modules do not necessarily bother what _exact_ type the provided
number is (some do, some don't) and might just use it as, e.g. double,
so they don't care what type was originally inserted into the boost::any
as long as they get a double-converted value out of it.

As boost::any only allows to get the original type out of it with
any_cast<T>, the class provides a set of conversion functions like

boost::function< int () > toInt;
boost::function< short () > toShort;
...
...

struct Converter_base {
     Converter_base( const boost::any* a ) : m_any(a) {};
     public:
         const boost::any* m_any;
};

template< typename Target, typename Source >
struct Converter : public Converter_base{
     Converter( const boost::any* a ) : Converter_base(a) {};
     T operator()() const {
       return boost::numeric_cast<Target>( * boost::any_cast<Source>(
Converter_base::m_any ) );
     };
};

template< typename SourceType >
void setValue ( SourceType& value ) {
        m_value = value // this is boost::any
        toInt = Converter< int, SourceType > ( &m_value );
        toShort = Converter< short, SourceType > ( &m_value );
        ......
}

This allows me to ask for the value converted to my desired type and
receiving an exception if the conversion is not possible (or produces
overflows). The problem is that the functor Converter is not
copy-constructible because of the pointer to boost::any.

So I have to take care of setting the correct pointer in the copy
constructor of my data exchange class. In order to get the pointer to
the converter functor and thus to the mep_any, I need to know
SourceType. But since the module that provides the data and the module
that consumes the data don't know about each other, the consumer doesn't
know SourceType and cannot compile the appropriate copy constructor.

Since mep_any is independent from the template arguments, I moved it to
a base class Converter_base and let Converter inherit from it.

Now, I want to access the functor via
        toInt.target<Converter_base>()
in order to access/update the mep_any. But target() returns 0 ....
I checked function/function_base.hpp and found:
     // DPG TBD: Since we're only storing a pointer, it's
     // possible that the user could ask for a base class or
     // derived class. Is that okay?

Are there any reasons, why base class access is currently not
allowed/implemented?

Sorry that the post got a bit long, but I hope at leas my intention got
somehow clear...

Johannes


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net