Boost logo

Boost :

Subject: [boost] incomplete_smart_cast: a smart_cast to incomplete types
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2010-02-14 18:04:15


I have another user case:

template <class Final, typename Base=base_transaction_object>
class deep_transaction_object : public Base {
    Final* final() { return static_cast<Final*>(this); }
    const Final* final() const { return static_cast<Final const*>(this); }
public:
    static Final* make_cache(Final const* rhs, transaction& ) {
        return new Final(*rhs);
    }
    virtual base_transaction_object* make_cache(transaction& t) const {
        return new Final(*final());
    }
    virtual void delete_cache() {
        delete this;
    }
    virtual void copy_cache(base_transaction_object const & rhs) {
       *final() = *smart_cast<Final const*>(&rhs); // (1)
    }
};

deep_transaction_object is a mixin class that is be used as follows

class F : public deep_transaction_object<F> {...};

At the moment we use smart_cast in deep_transaction_object (1), F is not completly defined.
smart_cast is a template class that needs its class parameters to be completly defined, as is_virtual_base_of needs this.

In this context I can not check if Final is_virtual_base_of base_transaction_object. As F is a deep_transaction_object<F> which is a Base, the check could be limited to see if Base is_virtual_base_of base_transaction_object.

Thus I need a kind of incomplete_smart_cast which in addition has an intermidiate class used for the tests

        *final() = *incomplete_smart_cast<Final const *, Base const*>(&rhs);

incomplete_smart_cast<Target,Intermediate, Source> works like smart_cast except that instead of testing with Target,
it test with Intermediate to select the cast to apply to target

    incomplete_smart_cast<F*, I*>(ptr) = static_cast<F*,smart_cast<I*>(ptr)>

For the case of single inheritance this seems to too much to add, but with multiple inheritance the situation we can not build it from static_cast and smart_cast, we need to check if one of the intermediate classes is_virtual_base_of the base class.

For example if the mixin allows to inherit from two classes

template <class Final, class Base1, class Base1>
class deep_transaction_object2 : public Base1, public Base2 {
...
    virtual void copy_cache(base_transaction_object const & rhs) {
       *final() = *incomplete_smart_cast2<Final const*, Base1 const*, Base2 const*>(&rhs); // (1)
    }
};

Please let me know if my rationale is correct and if my use case is enough common to interest the Boost community.

Best
_____________________
Vicente Juan Botet Escribá


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