Line | |
---|
1 | #include "node2d.h" |
---|
2 | |
---|
3 | static const int NodeDim = 2; |
---|
4 | // |
---|
5 | // - serialize node (x y) |
---|
6 | // - store byte stream in buf |
---|
7 | // |
---|
8 | // Return: |
---|
9 | // NULL: fail to allocate memory |
---|
10 | // valid pointer: point to buffer holding the byte stream |
---|
11 | // |
---|
12 | char* |
---|
13 | RpNode2d::serialize() |
---|
14 | { |
---|
15 | int len = NodeDim * sizeof(int); |
---|
16 | |
---|
17 | char * buf = new char[len]; |
---|
18 | serialize(buf); |
---|
19 | |
---|
20 | return buf; |
---|
21 | } |
---|
22 | |
---|
23 | // |
---|
24 | // actual function that does the serialization |
---|
25 | // |
---|
26 | // Input: |
---|
27 | // buf: pointer to valid memory |
---|
28 | // len: number of bytes in buf |
---|
29 | // Return: |
---|
30 | // error code for null pointer (defined in "util.h") |
---|
31 | // success |
---|
32 | // |
---|
33 | RP_ERROR |
---|
34 | RpNode2d::serialize(char* buf) |
---|
35 | { |
---|
36 | if (buf == NULL) { |
---|
37 | RpAppendErr("RpElement::deserialize: null buffer"); |
---|
38 | RpPrintErr(); |
---|
39 | return RP_ERR_NULL_PTR; |
---|
40 | } |
---|
41 | |
---|
42 | char * ptr = buf; |
---|
43 | memcpy((void*)ptr, (void *)&m_x, sizeof(int)); |
---|
44 | ptr += sizeof(int); |
---|
45 | memcpy((void*)ptr, (void *)&m_y, sizeof(int)); |
---|
46 | |
---|
47 | return RP_SUCCESS; |
---|
48 | } |
---|
49 | |
---|
50 | RP_ERROR |
---|
51 | RpNode2d::deserialize(const char* buf) |
---|
52 | { |
---|
53 | if (buf == NULL) { |
---|
54 | RpAppendErr("RpElement::deserialize: null buffer"); |
---|
55 | RpPrintErr(); |
---|
56 | return RP_ERR_NULL_PTR; |
---|
57 | } |
---|
58 | |
---|
59 | char * ptr = (char*)buf; |
---|
60 | memcpy((void*)&m_x, (void *)ptr, sizeof(int)); |
---|
61 | ptr += sizeof(int); |
---|
62 | memcpy((void*)&m_y, (void *)ptr, sizeof(int)); |
---|
63 | |
---|
64 | return RP_SUCCESS; |
---|
65 | } |
---|
66 | |
---|
67 | void |
---|
68 | RpNode2d::print() |
---|
69 | { |
---|
70 | printf("<node id=\"%d\"> %d %d </node>\n",m_id,m_x,m_y); |
---|
71 | } |
---|
72 | |
---|
Note: See
TracBrowser
for help on using the repository browser.