1 | #ifndef __RPSERIALIZER_H__ |
---|
2 | #define __RPSERIALIZER_H__ |
---|
3 | |
---|
4 | #include <vector> |
---|
5 | #include <map> |
---|
6 | #include <string> |
---|
7 | #include "serializable.h" |
---|
8 | #include "field.h" |
---|
9 | #include "grid2d.h" |
---|
10 | #include "util.h" |
---|
11 | |
---|
12 | // |
---|
13 | // class managing serializable objects |
---|
14 | // |
---|
15 | class RpSerializer { |
---|
16 | public: |
---|
17 | |
---|
18 | RpSerializer() { m_buf = 0; }; |
---|
19 | |
---|
20 | // remove all objects, reset ref counts to zero. |
---|
21 | // delete buffer memory so the caller does not need to |
---|
22 | void clear(); |
---|
23 | |
---|
24 | void addObject(RpSerializable* obj); |
---|
25 | void addObject(const char* key, RpSerializable* obj); |
---|
26 | |
---|
27 | void deleteObject(RpSerializable* obj); |
---|
28 | void deleteObject(const char* objectName); |
---|
29 | void deleteAllObjects(); |
---|
30 | |
---|
31 | // marshalling |
---|
32 | char* serialize(); |
---|
33 | |
---|
34 | // unmarshalling - instantiate all objects |
---|
35 | void deserialize(const char* buf); |
---|
36 | |
---|
37 | // retrieve object |
---|
38 | RpSerializable* getObject(const char* objectName); |
---|
39 | |
---|
40 | // returns total number of bytes of all objects in serializer |
---|
41 | int numBytesObjects(); |
---|
42 | |
---|
43 | // returns total number of bytes including objects and header |
---|
44 | int numBytes() { return (numBytesObjects() + headerSize()); }; |
---|
45 | |
---|
46 | // returns total number of bytes in header |
---|
47 | // header contains the following: |
---|
48 | // blob id (in the form of: RV-A-TYPENAME) |
---|
49 | // where A is a version number represented by a char in A-Z |
---|
50 | // where TYPENAME is a char string, e.g., FIELD, GRID1D. |
---|
51 | // 2 bytes: encoding indicator (id encoding algorithm) |
---|
52 | // 2 bytes: compression indicator (id compression algorithm) |
---|
53 | // |
---|
54 | int headerSize() { return 8; }; |
---|
55 | |
---|
56 | void print(); |
---|
57 | |
---|
58 | protected: |
---|
59 | // read/write file header |
---|
60 | void readHeader(const char* buf, int& nbytes, |
---|
61 | std::string& encodeFlag, std::string& compressFlag); |
---|
62 | void writeHeader(char* buf, int nbytes, |
---|
63 | const char* eflag, const char* cflag); |
---|
64 | |
---|
65 | RpSerializable* createObject(std::string header, const char* buf); |
---|
66 | |
---|
67 | struct rpStrCmp { |
---|
68 | bool operator()( const char* s1, const char* s2 ) const { |
---|
69 | return strcmp( s1, s2 ) < 0; |
---|
70 | } |
---|
71 | }; |
---|
72 | |
---|
73 | private: |
---|
74 | typedef map<const char*, RpSerializable*, rpStrCmp> typeObjMap; |
---|
75 | typedef map<const char*, int, rpStrCmp> typeRefCount; |
---|
76 | |
---|
77 | // archive of objects referenced by their names (e.g., output.field) |
---|
78 | typeObjMap m_objMap; |
---|
79 | |
---|
80 | // reference count for each object |
---|
81 | typeRefCount m_refCount; |
---|
82 | |
---|
83 | char* m_buf; // tmp buffer for serialization |
---|
84 | }; |
---|
85 | |
---|
86 | #endif |
---|