1 | #include "serializer.h" |
---|
2 | |
---|
3 | // |
---|
4 | // add an serializable object to the Serializer |
---|
5 | // |
---|
6 | void |
---|
7 | RpSerializer::addObject(RpSerializable* obj) |
---|
8 | { |
---|
9 | // add object pointer to object map |
---|
10 | m_objMap[obj->objectName()] = obj; |
---|
11 | |
---|
12 | // add object entry to reference count table |
---|
13 | m_refCount[obj->objectName()] = 0; |
---|
14 | } |
---|
15 | |
---|
16 | // |
---|
17 | // Input: |
---|
18 | // a byte stream of a serialized object |
---|
19 | // RV-A-<object-type><numBytes>..... |
---|
20 | // |
---|
21 | void |
---|
22 | RpSerializer::addObject(const char* buf) |
---|
23 | { |
---|
24 | // todo |
---|
25 | |
---|
26 | } |
---|
27 | |
---|
28 | // |
---|
29 | // Remove an object from Serializer |
---|
30 | // Input: |
---|
31 | // name of rp object (e.g., "output.mesh(m3d)") |
---|
32 | // |
---|
33 | void |
---|
34 | RpSerializer::deleteObject(const char* name) |
---|
35 | { |
---|
36 | typeObjMap::iterator iter = m_objMap.find(name); |
---|
37 | if (iter == m_objMap.end()) |
---|
38 | // does not exist |
---|
39 | return; |
---|
40 | |
---|
41 | RpSerializable* obj_ptr = (*iter).second; |
---|
42 | |
---|
43 | // decrement object's reference count |
---|
44 | // if reference count is zero, free memory and remove entry in obj map |
---|
45 | if ( (--(m_refCount[name])) == 0) { |
---|
46 | delete obj_ptr; // free obj memory |
---|
47 | m_objMap.erase(name); // erase entry in object map |
---|
48 | m_refCount.erase(name); // erase entry in object map |
---|
49 | } |
---|
50 | |
---|
51 | return; |
---|
52 | } |
---|
53 | |
---|
54 | void |
---|
55 | RpSerializer::deleteObject(RpSerializable* obj) |
---|
56 | { |
---|
57 | const char* name = obj->objectName(); |
---|
58 | typeObjMap::iterator iter = m_objMap.find(name); |
---|
59 | |
---|
60 | if (iter == m_objMap.end()) |
---|
61 | // does not exist |
---|
62 | return; |
---|
63 | |
---|
64 | RpSerializable* obj_ptr = (*iter).second; |
---|
65 | |
---|
66 | // decrement object's reference count |
---|
67 | // if reference count is zero, free memory and remove entry in obj map |
---|
68 | if ( (--(m_refCount[name])) == 0) { |
---|
69 | delete obj_ptr; // free obj memory |
---|
70 | m_objMap.erase(name); // erase entry in object map |
---|
71 | m_refCount.erase(name); // erase entry in object map |
---|
72 | } |
---|
73 | |
---|
74 | return; |
---|
75 | } |
---|
76 | |
---|
77 | void |
---|
78 | RpSerializer::deleteAllObjects() |
---|
79 | { |
---|
80 | typeObjMap::iterator iter; |
---|
81 | |
---|
82 | // free memory for all objects |
---|
83 | for (iter = m_objMap.begin(); iter != m_objMap.end(); iter++) |
---|
84 | delete (*iter).second; |
---|
85 | |
---|
86 | m_objMap.clear(); |
---|
87 | m_refCount.clear(); |
---|
88 | m_numObjects = 0; |
---|
89 | } |
---|
90 | |
---|
91 | void |
---|
92 | RpSerializer::clear() |
---|
93 | { |
---|
94 | deleteAllObjects(); |
---|
95 | } |
---|
96 | |
---|
97 | // retrieve object |
---|
98 | RpSerializable* |
---|
99 | RpSerializer::getObject(const char* objectName) |
---|
100 | { |
---|
101 | typeObjMap::iterator iter = m_objMap.find(objectName); |
---|
102 | |
---|
103 | if (iter != m_objMap.end()) { |
---|
104 | // object found, increment ref count |
---|
105 | ++m_refCount[objectName]; |
---|
106 | |
---|
107 | // return pointer to object |
---|
108 | return (*iter).second; |
---|
109 | } |
---|
110 | else // not found |
---|
111 | return NULL; |
---|
112 | } |
---|
113 | |
---|
114 | // marshalling |
---|
115 | char* |
---|
116 | RpSerializer::serialize() |
---|
117 | { |
---|
118 | // calculate total number of bytes by adding up all the objects |
---|
119 | int nbytes = 0; |
---|
120 | |
---|
121 | typeObjMap::iterator iter; |
---|
122 | for (iter = m_objMap.begin(); iter != m_objMap.end(); iter++) |
---|
123 | nbytes += ( (*iter).second)->numBytes(); |
---|
124 | |
---|
125 | // add bytes necessary for encoding and compression and |
---|
126 | // total number of bytes |
---|
127 | int headerSize = sizeof(int) + 8; |
---|
128 | |
---|
129 | nbytes += headerSize; |
---|
130 | |
---|
131 | // allocate memory |
---|
132 | char* buf = new char(nbytes); |
---|
133 | if (buf == NULL) { |
---|
134 | printf("RpSerializer::serialize(): failed to allocate mem\n"); |
---|
135 | return buf; |
---|
136 | } |
---|
137 | |
---|
138 | // call each object to serialize itself |
---|
139 | char* ptr = buf + headerSize; |
---|
140 | int nb; |
---|
141 | for (iter = m_objMap.begin(); iter != m_objMap.end(); iter++) { |
---|
142 | RpSerializable* obj = (*iter).second; |
---|
143 | nb = obj->numBytes(); |
---|
144 | obj->doSerialize(ptr, nb); // object serialization |
---|
145 | ptr += nb; // advance buffer pointer |
---|
146 | } |
---|
147 | |
---|
148 | // return pointer to buffer |
---|
149 | return buf; |
---|
150 | } |
---|
151 | |
---|
152 | // |
---|
153 | // unmarshalling |
---|
154 | // |
---|
155 | // Input: |
---|
156 | // buf - pointer to byte stream buffer, including the beginning bytes |
---|
157 | // for total number of bytes, compression and encoding indicators. |
---|
158 | // Side effects: |
---|
159 | // objects are created in m_objMap |
---|
160 | // reference count for each object is set to 0 |
---|
161 | void |
---|
162 | RpSerializer::deserialize(const char* buf) |
---|
163 | { |
---|
164 | |
---|
165 | } |
---|
166 | |
---|
167 | |
---|