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