2013-02-21 149 views
6

我有一個對象接口和派生對象可能希望支持的開放式接口集合。C++中接口的多重繼承

// An object 
class IObject 
{ 
    getAttribute() = 0 
} 

// A mutable object 
class IMutable 
{ 
    setAttribute() = 0 
} 

// A lockable object 
class ILockable 
{ 
    lock() = 0 
} 

// A certifiable object 
class ICertifiable 
{ 
    setCertification() = 0 
    getCertification() = 0 
} 

一些派生的對象可能是這樣的:

class Object1 : public IObject, public IMutable, public ILockable {} 
class Object2 : public IObject, public ILockable, public ICertifiable {} 
class Object3 : public IObject {} 

這裏是我的問題:有沒有寫功能,將只需要這些接口的某些組合的方式?例如:

void doSomething(magic_interface_combiner<IObject, IMutable, ILockable> object); 

doSomething(Object1()) // OK, all interfaces are available. 
doSomething(Object2()) // Compilation Failure, missing IMutable. 
doSomething(Object3()) // Compilation Failure, missing IMutable and ILockable. 

我發現的最接近的東西是boost :: mpl :: inherit。我有一些有限的成功,但它並不完全符合我的需要。

例如:

class Object1 : public boost::mpl::inherit<IObject, IMutable, ILockable>::type 
class Object2 : public boost::mpl::inherit<IObject, ILockable, ICertifiable>::type 
class Object3 : public IObject 

void doSomething(boost::mpl::inherit<IObject, ILockable>::type object); 

doSomething(Object1()) // Fails even though Object1 derives from IObject and ILockable. 
doSomething(Object2()) // Fails even though Object2 derives from IObject and ILockable. 

我認爲類似的boost :: MPL ::繼承的東西,但會產生與提供的類型的所有可能的排列組合可能會奏效繼承樹。

我也很好奇其他解決這個問題的方法。理想的情況是編譯時間檢查,而不是運行時(即沒有dynamic_cast)。

+2

你的意思是AND組合,或OR組合? – 2013-02-21 17:00:29

+1

糾正我,如果我錯了,但不是你的抽象函數需要被標記爲'虛擬'以及'= 0'? – ApproachingDarknessFish 2013-02-21 17:02:29

+2

我拿它使用模板而不是錯誤的答案? – 2013-02-21 17:02:40

回答

1

也許這不是最優雅的方式,因爲它與C++語法03

template <typename T, typename TInterface> 
void interface_checker(T& t) 
{ 
    TInterface& tIfClassImplementsInterface = static_cast<TInterface&>(t); 
} 

這是給你的伎倆的精神來完成。 現在你的情況:

template <typename T, typename TInterface1, typename TInterface2, typename TInterface3 > 
void magic_interface_combiner(T& t) 
{ 
    TInterface1& tIfClassImplementsInterface = static_cast<TInterface1&>(t); 
    TInterface2& tIfClassImplementsInterface = static_cast<TInterface2&>(t); 
    TInterface3& tIfClassImplementsInterface = static_cast<TInterface3&>(t); 
} 

我想它可以更明智的方式使用C++ 11個型特徵來完成。

2

您應該使用static_assert在函數內檢查類型:

#include <type_traits> 

template< typename T > 
void doSomething(const T& t) 
{ 
    static_assert(std::is_base_of<IObject,T>::value, "T does not satisfy IObject"); 
    static_assert(std::is_base_of<IMutable,T>::value, "T does not satisfy IMutable"); 

    // ... 
} 

,這將給很不錯的錯誤信息,告訴你哪些接口不滿意你。如果你需要重載函數,有一個版本是隻適用於某個接口的組合,你也可以使用enable_if

#include <type_traits> 

template< typename T, typename... Is > 
struct HasInterfaces; 

template< typename T > 
struct HasInterfaces<T> : std::true_type {}; 

template< typename T, typename I, typename... Is > 
struct HasInterfaces< T, I, Is... > 
    : std::integral_constant< bool, 
     std::is_base_of< I, T >::value && HasInterfaces< T, Is... >::value > {}; 

template< typename T > 
typename std::enable_if< HasInterfaces< T, IObject, IMutable >::value >::type 
doSomething(const T& t) 
{ 
    // ... 
} 

這將使得功能消失從過載時設置的接口要求沒有得到滿足。

+0

在pre-C++ 11語言中是否可以使用'static_assert'? – 2013-02-21 17:47:45

+0

是:[BOOST_STATIC_ASSERT_MSG(v,msg)](http://www.boost.org/doc/libs/1_53_0/doc/html/boost_staticassert.html) – 2013-02-21 17:50:19

3

可以使用遞歸可變參數繼承寫一個接口檢查類:

template<typename... Interfaces> 
struct check_interfaces; 
template<> 
struct check_interfaces<> { 
    template<typename T> check_interfaces(T *) {} 
}; 
template<typename Interface, typename... Interfaces> 
struct check_interfaces<Interface, Interfaces...>: 
public check_interfaces<Interfaces...> { 
    template<typename T> check_interfaces(T *t): 
     check_interfaces<Interfaces...>(t), i(t) {} 
    Interface *i; 
    operator Interface *() const { return i; } 
}; 

例如:

struct IObject { virtual int getAttribute() = 0; }; 
struct IMutable { virtual void setAttribute(int) = 0; }; 
struct ILockable { virtual void lock() = 0; }; 

void f(check_interfaces<IObject, IMutable> o) { 
    static_cast<IObject *>(o)->getAttribute(); 
    static_cast<IMutable *>(o)->setAttribute(99); 
} 

struct MutableObject: IObject, IMutable { 
    int getAttribute() { return 0; } 
    void setAttribute(int) {} 
}; 

struct LockableObject: IObject, ILockable { 
    int getAttribute() { return 0; } 
    void lock() {} 
}; 

int main() { 
    f(new MutableObject); 
    f(new LockableObject); // fails 
} 

注意check_interfaces有每個接口檢查一個指針的足跡;這是因爲它對實際參數的聲明類型執行類型擦除。

+0

+1適用於可變參數模板。 – 2013-02-21 17:42:59

1

只給你的C++ 11小情趣:

單一類型的無遞歸:

template <typename... Ts> 
class magic_interface_combiner { 
    typedef std::tuple<Ts*...> Tpl; 
    Tpl tpl; 

    template <typename T, int I> 
    T *as_(std::false_type) 
    { 
    static_assert(I < std::tuple_size<Tpl>::value, "T not found"); 
    return as_<T, I+1>(std::is_same<T, typename std::tuple_element<I+1, Tpl>::type>{}); 
    } 
    template <typename T, int I> 
    T *as_(std::true_type) { return std::get<I>(tpl); } 

public: 
    template <typename T> 
    magic_interface_combiner(T * t) : tpl(static_cast<Ts*>(t)...) {} 

    template <typename T> T * as() { return as_<T, 0>(std::false_type{}); } 
}; 

// no template  
void doSomething(magic_interface_combiner<IObject, IMutable, ILockable> object) 
{ 
} 

兩種類型,但沒有遞歸:

template <typename T> 
class single_interface_combiner { 
    T *p; 
public: 
    single_interface_combiner(T *t) : p(t) {} 
    operator T*() { return p; } 
}; 

template <typename... Ts> 
struct magic_interface_combiner : single_interface_combiner<Ts>... { 
    template <typename T> 
    magic_interface_combiner(T* t) : single_interface_combiner<Ts>(t)... {} 

    template <typename T> 
    T * as() { return *this; } 
}; 
2

一個解決方案使用std::enable_ifstd::is_base_of

#include <type_traits> 

// An object 
struct IObject 
{ 
    virtual void getAttribute() = 0; 
}; 

// A mutable object 
struct IMutable 
{ 
    virtual void setAttribute() = 0; 
}; 

// A lockable object 
struct ILockable 
{ 
    virtual void lock() = 0; 
}; 

// A certifiable object 
struct ICertifiable 
{ 
    virtual void setCertification() = 0; 
    virtual void getCertification() = 0; 
}; 

struct Object1 : public IObject, public IMutable, public ILockable 
{ 
    void getAttribute() {} 
    void setAttribute() {} 
    void lock() {} 
}; 

struct Object2 : public IObject, public ILockable, public ICertifiable 
{ 
    void getAttribute() {} 
    void lock() {} 
    void setCertification() {} 
    void getCertification() {} 
}; 

struct Object3 : public IObject 
{ 
    void getAttribute() {} 
}; 

template<typename T> 
void doSomething(
    typename std::enable_if< 
     std::is_base_of<IObject, T>::value && 
     std::is_base_of<IMutable, T>::value && 
     std::is_base_of<ILockable, T>::value, 
     T>::type& obj) 
{ 
} 

int main() 
{ 
    Object1 object1; 
    Object2 object2; 
    Object3 object3; 

    doSomething<Object1>(object1); // Works 
    doSomething<Object2>(object2); // Compilation error 
    doSomething<Object3>(object3); // Compilation error 
}