I have an object interface and a open ended collection of interfaces that a derived object might want to support.
// 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
}
Some derived objects might look like this:
class Object1 : public IObject, public IMutable, public ILockable {}
class Object2 : public IObject, public ILockable, public ICertifiable {}
class Object3 : public IObject {}
Here is my question: Is there a way to write functions that will only take certain combinations of these interfaces? For example:
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.
The closest thing I've found is boost::mpl::inherit. I've had some limited success but it doesn't do exactly what I need.
For example:
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.
I think something similar to boost::mpl::inherit but that would generate an inheritance tree with all possible permutations of the supplied types might work.
I'm also curious about other approaches to solving this problem. Ideally something that does compile time checks as opposed to runtime (i.e. no dynamic_cast).
virtual
as well as being=0
? – Guyotstd::is_base_of
. Might be used in combination withstd::enable_if
. – HagerdoSomething
to get passed so many interfaces at once? Your problems might be hinting a design flaw. – Sand