How to wrap multiple derived template class with boost

  boost, c++, derived-class, templates, wrapper

I need to wrap with boost some template classes that are derived one from another.
First, I have a template class ValueAndUnit whose partial code is:

template <class T>
class ValueAndUnit
{
public:
    /** brief Give access to template parameter representing unit */
    typedef T unit_type;
    ValueAndUnit<T>& setValue( double value, T unit );
}

Thanks to this post
Wrap C++ template class with boost python I was able to wrap it. Here is the code for that and it’s working.

template<typename T>
class ValueAndUnitWrap : public ValueAndUnit<T>, public wrapper<ValueAndUnit<T> > {
public:
    ValueAndUnit<T>& setValue( double value, T unit ){ return this->get_override( "setValue" )( ); }
};

template<typename T>
void export_ValueAndUnit()
{
    class_<ValueAndUnitWrap<T>, boost::noncopyable>( "ValueAndUnit", no_init )
        .def( "setValue", &ValueAndUnit<T>::setValue, return_internal_reference<>() )
        ;
}
BOOST_PYTHON_MODULE( exemple ){
    enum_<WeightUnit>( "WeightUnit" )
        .value( "WeightUnit_kg", WeightUnit::WeightUnit_kg )
        .value( " WeightUnit_pound", WeightUnit::WeightUnit_pound )
        ;

    export_ValueAndUnit <WeightUnit>();
    class_<WeightInfo, bases<ValueAndUnit<WeightUnit>> >( "WeightInfo", init<double, WeightUnit >() )
        ;
};

Now I have this ImmutableValueUnitAndTime class which is derived from the previous template class ValueAndUnit (only partial code here).

template <class T>
class ImmutableValueUnitAndTime : public ValueAndUnit<T>
{
    typedef ValueAndUnit<T> baseClass;

public:
ImmutableValueUnitAndTime(const TimeUnit& when, double value, T unit);
}

TimeUnit used in there is another class (part of code):

class TimeUnit
{
    friend std::ostream& operator<<(std::ostream &os, const TimeUnit& var);  
public:
    TimeUnit();
static TimeUnit fromMinutes( int in_total_minutes, const TimeUnit* pReference = NULL );
}

The next derived class is ValueUnitTimeAndDuration, deriving from the previous one (partial code also):

    template <class T>
    class ValueUnitTimeAndDuration : public ImmutableValueUnitAndTime<T>
    {
        typedef ImmutableValueUnitAndTime<T> baseClass;

    public:
            /** brief Default empty constructor */
        ValueUnitTimeAndDuration();

        ValueUnitTimeAndDuration(const ValueUnitTimeAndDuration<T>& copyFrom) = default;
}

And the final one which I need to wrap isTimeBasedInfo (also partial code):

enum class TimeBasedUnit
{
    TimeBasedUnit_mU_min,
    TimeBasedUnit_U_min,
    TimeBasedUnit_U_hour
};


class TimeBasedInfo : public ValueUnitTimeAndDuration<TimeBasedUnit>
{
    typedef ValueUnitTimeAndDuration<TimeBasedUnit> baseClass;
public:
    /** brief Default empty constructor */
    TimeBasedInfo();
   static TimeBasedUnit& userPreferenceUnit();
}

I didn’t find any information in the boost documentation to help me understand how to wrap this inherited template class isTimeBasedInfo, and I cannot figure out how to adapt what I already did for one template class to this multi inheritance class. Does anyone has a clue? Thanks a lot!

Source: Windows Questions C++

LEAVE A COMMENT