1 | /* |
---|
2 | * ====================================================================== |
---|
3 | * Rappture::Serializable |
---|
4 | * |
---|
5 | * AUTHOR: Michael McLennan, Purdue University |
---|
6 | * Carol X Song, Purdue University |
---|
7 | * |
---|
8 | * Copyright (c) 2004-2006 Purdue Research Foundation |
---|
9 | * ---------------------------------------------------------------------- |
---|
10 | * See the file "license.terms" for information on usage and |
---|
11 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
12 | * ====================================================================== |
---|
13 | */ |
---|
14 | #ifndef RAPPTURE_SERIALIZABLE_H |
---|
15 | #define RAPPTURE_SERIALIZABLE_H |
---|
16 | |
---|
17 | #include <string> |
---|
18 | #include <map> |
---|
19 | #include "rappture2.h" |
---|
20 | #include "RpSerialBuffer.h" |
---|
21 | |
---|
22 | namespace Rappture { |
---|
23 | |
---|
24 | class SerialConversion; // see below |
---|
25 | |
---|
26 | /** |
---|
27 | * Base class for any object that can be serialized into a stream, |
---|
28 | * then saved to a file or written to a socket, and reconstituted |
---|
29 | * into its original form. Serializable objects can be added to |
---|
30 | * a serializer, which handles the overall conversion. |
---|
31 | */ |
---|
32 | class Serializable { |
---|
33 | public: |
---|
34 | Serializable(); |
---|
35 | virtual ~Serializable(); |
---|
36 | |
---|
37 | virtual const char* serializerType() const = 0; |
---|
38 | virtual char serializerVersion() const = 0; |
---|
39 | |
---|
40 | virtual void serialize(SerialBuffer& bufferPtr) const; |
---|
41 | static Outcome deserialize(SerialBuffer& buffer, Ptr<Serializable>* objPtrPtr); |
---|
42 | |
---|
43 | typedef void (Serializable::*serializeObjectMethod)(SerialBuffer& buffer) const; |
---|
44 | typedef Ptr<Serializable> (*createObjectFunc)(); |
---|
45 | typedef Outcome (Serializable::*deserializeObjectMethod)(SerialBuffer& buffer); |
---|
46 | |
---|
47 | private: |
---|
48 | friend class SerialConversion; |
---|
49 | |
---|
50 | class ConversionFuncs { |
---|
51 | public: |
---|
52 | char version; |
---|
53 | serializeObjectMethod serialMethod; |
---|
54 | createObjectFunc createFunc; |
---|
55 | deserializeObjectMethod deserialMethod; |
---|
56 | }; |
---|
57 | |
---|
58 | typedef std::map<std::string, ConversionFuncs> Name2ConvFuncsMap; |
---|
59 | static Name2ConvFuncsMap *_name2convFuncs; |
---|
60 | }; |
---|
61 | |
---|
62 | /** |
---|
63 | * Each class derived from Serializable should have a SerialConversion |
---|
64 | * stored as a static data member. This declares information needed |
---|
65 | * to serialize/deserialize the class, making objects in that class |
---|
66 | * serializable. |
---|
67 | */ |
---|
68 | class SerialConversion { |
---|
69 | public: |
---|
70 | SerialConversion(const char *className, char version, |
---|
71 | Serializable::serializeObjectMethod, |
---|
72 | Serializable::createObjectFunc, |
---|
73 | Serializable::deserializeObjectMethod); |
---|
74 | }; |
---|
75 | |
---|
76 | } // namespace Rappture |
---|
77 | |
---|
78 | #endif |
---|