Update README.md
[nemesis.git] / Platform / Serializable.h
1 /*
2  Copyright (c) 2008 TrueCrypt Developers Association. All rights reserved.
3
4  Governed by the TrueCrypt License 3.0 the full text of which is contained in
5  the file License.txt included in TrueCrypt binary and source code distribution
6  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 (size_t 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 page took 0.031383 seconds and 3 git commands to generate. Download a nemesis OSX (sierra+high sierra, tested/working) binary, with fuse-ext3 via e2fsprogs, at this link. application and installer are signed by screwjack, llc. must install fuse with macFUSE layer first.