So, one pattern of the problem that I constantly encounter and do not have a good solution is how to provide specialized patterns based on what type the pattern parameter is determined from. For example, suppose I have:
template<typename T>
struct implementPersist;
template<typename T>
void persist( T& object )
{
implementPersist::doPersist( object );
}
I would like for persist users to be able to provide implementation implementations of Persist :: persist for types declared after the above. This is straightforward in principle, but cumbersome in practice, but the user needs to provide a Persist implementation for each type.
To be more clear, suppose I have:
struct Persistent { virtual void myPersist() = 0; };
struct MyClass : public persistent { virtual void MyPersist() { ...implementation...} };
template<>
struct implementPersist<Persistent>{ void doPersist(Persistent& p) { p->myPersist(); } };
struct X{};
template<>
struct implementPersist<X>{ void doPersist(X& p) { ...implementation...} };
struct MyBoostPersistedObject { virtual void boostPersist() = 0 };
struct Z : public MyBoostPersistedObject { virtual void boostPersist() = 0 };
template<>
struct implementPersist<myBoostPersistedObject>{ void boostPersist() { ...implementation... } };
, Persist, myBoostPersistedObject , (, POD).
, ,
implementPersist<Persistent>::doPersist
if: persist (T &) , T . () , T = myClass. , . , , , , , .
void persist (Persistent &); void persist (X &); void persist (myBoostPersistedObject &);
, , .
- - :
class persist;
template<typename T, bool hasMyPersistMethod=isDerivedFrom(T,persist)::value >
struct implementPersist;
template<typename T, bool true >
struct implementPersist<T,true>
{
template<> struct implementPersist<X>{ void doPersist(T& p) { p->myPersist(); } }
};
(. isDerivedFrom).
, implementPersist , . - .
, .
?