1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
template <class P, class C>
struct inherit
{
typedef char one[1];
typedef char two[2];
static one test(P*);
static two test(...);
static const bool value = (sizeof(test((C*)0)) == sizeof(one));
};
class Archive
{
private:
template <class T, bool serializable>
bool ReadHelper(const std::string& name, T& value);
template <class T, bool serializable>
bool WriteHelper(const std::string& name, const T& value);
public:
template <class T>
bool Read(const std::string& name, T& value)
{
return ReadHelper<T,inherit<ISerializable, T>::value>(name, value);
}
template <class T>
bool Write(const std::string& name, const T& value)
{
return WriteHelper<T,inherit<ISerializable, T>::value>(name, value);
}
};
template <class T>
bool Archive::ReadHelper<T,true>(const std::string& name, ISerializable& value)
{
return value.DeSerialize(name);
}
template <class T>
bool Archive::WriteHelper<T,true>(const std::string& name, const ISerializable& value)
{
return value.Serialize(name);
}
template <bool serializable>
bool Archive::WriteHelper<int,serializable>(const std::string& name, const ISerializable& value)
{
...
}
template <bool serializable>
bool Archive::WriteHelper<float,serializable>(const std::string& name, const ISerializable& value)
{
...
}
...
// a partir de là, tu sais comment faire. |
Partager