|
|
1.1 ! root 1: /* ! 2: Copyright (c) 2008 TrueCrypt Foundation. All rights reserved. ! 3: ! 4: Governed by the TrueCrypt License 2.4 the full text of which is contained ! 5: in the file License.txt included in TrueCrypt binary and source code ! 6: distribution packages. ! 7: */ ! 8: ! 9: #ifndef TC_HEADER_Platform_Serializable ! 10: #define TC_HEADER_Platform_Serializable ! 11: ! 12: #include <stdexcept> ! 13: #include "PlatformBase.h" ! 14: #include "ForEach.h" ! 15: #include "Serializer.h" ! 16: #include "SerializerFactory.h" ! 17: ! 18: namespace TrueCrypt ! 19: { ! 20: class Serializable ! 21: { ! 22: public: ! 23: virtual ~Serializable () { } ! 24: ! 25: virtual void Deserialize (shared_ptr <Stream> stream) = 0; ! 26: static string DeserializeHeader (shared_ptr <Stream> stream); ! 27: static Serializable *DeserializeNew (shared_ptr <Stream> stream); ! 28: ! 29: template <class T> ! 30: static shared_ptr <T> DeserializeNew (shared_ptr <Stream> stream) ! 31: { ! 32: shared_ptr <T> p (dynamic_cast <T *> (DeserializeNew (stream))); ! 33: if (!p) ! 34: throw std::runtime_error (SRC_POS); ! 35: return p; ! 36: } ! 37: ! 38: template <class T> ! 39: static void DeserializeList (shared_ptr <Stream> stream, list < shared_ptr <T> > &dataList) ! 40: { ! 41: if (DeserializeHeader (stream) != string ("list<") + SerializerFactory::GetName (typeid (T)) + ">") ! 42: throw std::runtime_error (SRC_POS); ! 43: ! 44: Serializer sr (stream); ! 45: uint64 listSize; ! 46: sr.Deserialize ("ListSize", listSize); ! 47: ! 48: for (int i = 0; i < listSize; i++) ! 49: { ! 50: shared_ptr <T> p (dynamic_cast <T *> (DeserializeNew (stream))); ! 51: if (!p) ! 52: throw std::runtime_error (SRC_POS); ! 53: dataList.push_back (p); ! 54: } ! 55: } ! 56: ! 57: virtual void Serialize (shared_ptr <Stream> stream) const; ! 58: ! 59: template <class T> ! 60: static void SerializeList (shared_ptr <Stream> stream, const list < shared_ptr <T> > &dataList) ! 61: { ! 62: Serializer sr (stream); ! 63: SerializeHeader (sr, string ("list<") + SerializerFactory::GetName (typeid (T)) + ">"); ! 64: ! 65: sr.Serialize ("ListSize", (uint64) dataList.size()); ! 66: foreach_ref (const T &item, dataList) ! 67: item.Serialize (stream); ! 68: } ! 69: ! 70: static void SerializeHeader (Serializer &serializer, const string &name); ! 71: ! 72: protected: ! 73: Serializable () { } ! 74: }; ! 75: } ! 76: ! 77: #define TC_SERIALIZABLE(TYPE) \ ! 78: static Serializable *GetNewSerializable () { return new TYPE(); } \ ! 79: virtual void Deserialize (shared_ptr <Stream> stream); \ ! 80: virtual void Serialize (shared_ptr <Stream> stream) const ! 81: ! 82: #endif // TC_HEADER_Platform_Serializable
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.