diff options
author | David <david@xagasoft.com> | 2006-05-13 00:08:20 +0000 |
---|---|---|
committer | David <david@xagasoft.com> | 2006-05-13 00:08:20 +0000 |
commit | de1aa6d66e11232edf2d7bbc729425003f6c3db5 (patch) | |
tree | 443216dc0fb9ac2bc66539332286b7b3167940b2 | |
parent | 59cea3a2c49618ec7351028334d468a9204f4027 (diff) | |
download | libbu++-de1aa6d66e11232edf2d7bbc729425003f6c3db5.tar.gz libbu++-de1aa6d66e11232edf2d7bbc729425003f6c3db5.tar.bz2 libbu++-de1aa6d66e11232edf2d7bbc729425003f6c3db5.tar.xz libbu++-de1aa6d66e11232edf2d7bbc729425003f6c3db5.zip |
david - initial commit of serialize code ;p
Diffstat (limited to '')
-rw-r--r-- | src/serializable.cpp | 8 | ||||
-rw-r--r-- | src/serializable.h | 14 | ||||
-rw-r--r-- | src/serializer.cpp | 185 | ||||
-rw-r--r-- | src/serializer.h | 64 | ||||
-rw-r--r-- | src/serializerbinary.cpp | 163 | ||||
-rw-r--r-- | src/serializerbinary.h | 46 | ||||
-rw-r--r-- | src/test/serialize.cpp | 24 |
7 files changed, 504 insertions, 0 deletions
diff --git a/src/serializable.cpp b/src/serializable.cpp new file mode 100644 index 0000000..fd50943 --- /dev/null +++ b/src/serializable.cpp | |||
@@ -0,0 +1,8 @@ | |||
1 | #include "serializable.h" | ||
2 | |||
3 | Serializable::Serializable() | ||
4 | { | ||
5 | } | ||
6 | Serializable::~Serializable() | ||
7 | { | ||
8 | } | ||
diff --git a/src/serializable.h b/src/serializable.h new file mode 100644 index 0000000..36c94df --- /dev/null +++ b/src/serializable.h | |||
@@ -0,0 +1,14 @@ | |||
1 | #ifndef SERIALIZER_H | ||
2 | #define SERIALIZER_H | ||
3 | |||
4 | //#include "serializer.h" | ||
5 | |||
6 | class Serializable | ||
7 | { | ||
8 | public: | ||
9 | Serializable(); | ||
10 | virtual ~Serializable(); | ||
11 | virtual void serialize(class Serializer &)=0; | ||
12 | }; | ||
13 | |||
14 | #endif | ||
diff --git a/src/serializer.cpp b/src/serializer.cpp new file mode 100644 index 0000000..5982a26 --- /dev/null +++ b/src/serializer.cpp | |||
@@ -0,0 +1,185 @@ | |||
1 | #include "serializer.h" | ||
2 | #include "serializable.h" | ||
3 | |||
4 | Serializer::Serializer(bool bLoading): | ||
5 | bLoading(bLoading) | ||
6 | { | ||
7 | } | ||
8 | Serializer::~Serializer() | ||
9 | { | ||
10 | } | ||
11 | |||
12 | bool Serializer::isLoading() | ||
13 | { | ||
14 | return bLoading; | ||
15 | } | ||
16 | |||
17 | /*Serializer::Serializer &operator&(bool &p) | ||
18 | { | ||
19 | if (bLoading) | ||
20 | { | ||
21 | return *this << p; | ||
22 | } | ||
23 | else | ||
24 | { | ||
25 | return *this >> p; | ||
26 | } | ||
27 | } | ||
28 | |||
29 | Serializer::Serializer &operator&(int8_t &p) | ||
30 | { | ||
31 | if (bLoading) | ||
32 | { | ||
33 | return *this << p; | ||
34 | } | ||
35 | else | ||
36 | { | ||
37 | return *this >> p; | ||
38 | } | ||
39 | } | ||
40 | |||
41 | Serializer::Serializer &operator&(int16_t &p) | ||
42 | { | ||
43 | if (bLoading) | ||
44 | { | ||
45 | return *this << p; | ||
46 | } | ||
47 | else | ||
48 | { | ||
49 | return *this >> p; | ||
50 | } | ||
51 | } | ||
52 | |||
53 | Serializer::Serializer &operator&(int32_t &p) | ||
54 | { | ||
55 | if (bLoading) | ||
56 | { | ||
57 | return *this << p; | ||
58 | } | ||
59 | else | ||
60 | { | ||
61 | return *this >> p; | ||
62 | } | ||
63 | } | ||
64 | |||
65 | Serializer::Serializer &operator&(int64_t &p) | ||
66 | { | ||
67 | if (bLoading) | ||
68 | { | ||
69 | return *this << p; | ||
70 | } | ||
71 | else | ||
72 | { | ||
73 | return *this >> p; | ||
74 | } | ||
75 | } | ||
76 | |||
77 | Serializer::Serializer &operator&(uint8_t &p) | ||
78 | { | ||
79 | if (bLoading) | ||
80 | { | ||
81 | return *this << p; | ||
82 | } | ||
83 | else | ||
84 | { | ||
85 | return *this >> p; | ||
86 | } | ||
87 | } | ||
88 | |||
89 | Serializer::Serializer &operator&(uint16_t &p) | ||
90 | { | ||
91 | if (bLoading) | ||
92 | { | ||
93 | return *this << p; | ||
94 | } | ||
95 | else | ||
96 | { | ||
97 | return *this >> p; | ||
98 | } | ||
99 | } | ||
100 | |||
101 | Serializer::Serializer &operator&(uint32_t &p) | ||
102 | { | ||
103 | if (bLoading) | ||
104 | { | ||
105 | return *this << p; | ||
106 | } | ||
107 | else | ||
108 | { | ||
109 | return *this >> p; | ||
110 | } | ||
111 | } | ||
112 | |||
113 | Serializer::Serializer &operator&(uint64_t &p) | ||
114 | { | ||
115 | if (bLoading) | ||
116 | { | ||
117 | return *this << p; | ||
118 | } | ||
119 | else | ||
120 | { | ||
121 | return *this >> p; | ||
122 | } | ||
123 | } | ||
124 | |||
125 | Serializer::Serializer &operator&(float &p) | ||
126 | { | ||
127 | if (bLoading) | ||
128 | { | ||
129 | return *this << p; | ||
130 | } | ||
131 | else | ||
132 | { | ||
133 | return *this >> p; | ||
134 | } | ||
135 | } | ||
136 | |||
137 | Serializer::Serializer &operator&(double &p) | ||
138 | { | ||
139 | if (bLoading) | ||
140 | { | ||
141 | return *this << p; | ||
142 | } | ||
143 | else | ||
144 | { | ||
145 | return *this >> p; | ||
146 | } | ||
147 | } | ||
148 | |||
149 | Serializer::Serializer &operator&(long double &p) | ||
150 | { | ||
151 | if (bLoading) | ||
152 | { | ||
153 | return *this << p; | ||
154 | } | ||
155 | else | ||
156 | { | ||
157 | return *this >> p; | ||
158 | } | ||
159 | }*/ | ||
160 | |||
161 | |||
162 | Serializer &Serializer::operator<<(Serializable &p) | ||
163 | { | ||
164 | p.serialize(*this); | ||
165 | return *this; | ||
166 | } | ||
167 | |||
168 | Serializer &Serializer::operator>>(Serializable &p) | ||
169 | { | ||
170 | p.serialize(*this); | ||
171 | return *this; | ||
172 | } | ||
173 | |||
174 | /*Serializer::Serializer &operator&(Serializable &p) | ||
175 | { | ||
176 | if (bLoading) | ||
177 | { | ||
178 | return *this << p; | ||
179 | } | ||
180 | else | ||
181 | { | ||
182 | return *this >> p; | ||
183 | } | ||
184 | }*/ | ||
185 | |||
diff --git a/src/serializer.h b/src/serializer.h new file mode 100644 index 0000000..03e6820 --- /dev/null +++ b/src/serializer.h | |||
@@ -0,0 +1,64 @@ | |||
1 | #ifndef SERIALIZABLE_H | ||
2 | #define SERIALIZABLE_H | ||
3 | |||
4 | #include <stdint.h> | ||
5 | //#include "serializable.h" | ||
6 | |||
7 | class Serializer | ||
8 | { | ||
9 | private: | ||
10 | bool bLoading; | ||
11 | public: | ||
12 | bool isLoading(); | ||
13 | |||
14 | Serializer(bool bLoading); | ||
15 | virtual ~Serializer(); | ||
16 | virtual void close()=0; | ||
17 | |||
18 | virtual Serializer &operator<<(bool)=0; | ||
19 | virtual Serializer &operator<<(int8_t)=0; | ||
20 | virtual Serializer &operator<<(int16_t)=0; | ||
21 | virtual Serializer &operator<<(int32_t)=0; | ||
22 | virtual Serializer &operator<<(int64_t)=0; | ||
23 | virtual Serializer &operator<<(uint8_t)=0; | ||
24 | virtual Serializer &operator<<(uint16_t)=0; | ||
25 | virtual Serializer &operator<<(uint32_t)=0; | ||
26 | virtual Serializer &operator<<(uint64_t)=0; | ||
27 | virtual Serializer &operator<<(float)=0; | ||
28 | virtual Serializer &operator<<(double)=0; | ||
29 | virtual Serializer &operator<<(long double)=0; | ||
30 | |||
31 | virtual Serializer &operator>>(bool &)=0; | ||
32 | virtual Serializer &operator>>(int8_t &)=0; | ||
33 | virtual Serializer &operator>>(int16_t &)=0; | ||
34 | virtual Serializer &operator>>(int32_t &)=0; | ||
35 | virtual Serializer &operator>>(int64_t &)=0; | ||
36 | virtual Serializer &operator>>(uint8_t &)=0; | ||
37 | virtual Serializer &operator>>(uint16_t &)=0; | ||
38 | virtual Serializer &operator>>(uint32_t &)=0; | ||
39 | virtual Serializer &operator>>(uint64_t &)=0; | ||
40 | virtual Serializer &operator>>(float &)=0; | ||
41 | virtual Serializer &operator>>(double &)=0; | ||
42 | virtual Serializer &operator>>(long double &)=0; | ||
43 | |||
44 | /* | ||
45 | virtual Serializer &operator&(bool &); | ||
46 | virtual Serializer &operator&(int8_t &); | ||
47 | virtual Serializer &operator&(int16_t &); | ||
48 | virtual Serializer &operator&(int32_t &); | ||
49 | virtual Serializer &operator&(int64_t &); | ||
50 | virtual Serializer &operator&(uint8_t &); | ||
51 | virtual Serializer &operator&(uint16_t &); | ||
52 | virtual Serializer &operator&(uint32_t &); | ||
53 | virtual Serializer &operator&(uint64_t &); | ||
54 | virtual Serializer &operator&(float &); | ||
55 | virtual Serializer &operator&(double &); | ||
56 | virtual Serializer &operator&(long double &); | ||
57 | */ | ||
58 | |||
59 | virtual Serializer &operator<<(class Serializable &); | ||
60 | virtual Serializer &operator>>(class Serializable &); | ||
61 | //virtual Serializer &operator&(Serializable &); | ||
62 | }; | ||
63 | |||
64 | #endif | ||
diff --git a/src/serializerbinary.cpp b/src/serializerbinary.cpp new file mode 100644 index 0000000..1ea9f36 --- /dev/null +++ b/src/serializerbinary.cpp | |||
@@ -0,0 +1,163 @@ | |||
1 | #include "serializerbinary.h" | ||
2 | |||
3 | SerializerBinary::SerializerBinary(FILE *fhFile, bool bLoading): | ||
4 | Serializer(bLoading), | ||
5 | fhFile(fhFile), | ||
6 | bCloseFile(false) | ||
7 | { | ||
8 | } | ||
9 | |||
10 | SerializerBinary::SerializerBinary(char *sFileName, bool bLoading): | ||
11 | Serializer(bLoading), | ||
12 | bCloseFile(true) | ||
13 | { | ||
14 | if (bLoading) | ||
15 | { | ||
16 | fhFile = fopen(sFileName, "rb"); | ||
17 | } | ||
18 | else | ||
19 | { | ||
20 | fhFile = fopen(sFileName, "wb"); | ||
21 | } | ||
22 | } | ||
23 | |||
24 | SerializerBinary::~SerializerBinary() | ||
25 | { | ||
26 | close(); | ||
27 | } | ||
28 | |||
29 | void SerializerBinary::close() | ||
30 | { | ||
31 | if (fhFile != NULL) | ||
32 | { | ||
33 | fclose(fhFile); | ||
34 | fhFile = NULL; | ||
35 | } | ||
36 | } | ||
37 | |||
38 | Serializer &SerializerBinary::operator<<(bool p) | ||
39 | { | ||
40 | fwrite(&p, sizeof(p), 1, fhFile); | ||
41 | return *this; | ||
42 | } | ||
43 | Serializer &SerializerBinary::operator<<(int8_t p) | ||
44 | { | ||
45 | fwrite(&p, sizeof(p), 1, fhFile); | ||
46 | return *this; | ||
47 | } | ||
48 | Serializer &SerializerBinary::operator<<(int16_t p) | ||
49 | { | ||
50 | fwrite(&p, sizeof(p), 1, fhFile); | ||
51 | return *this; | ||
52 | } | ||
53 | Serializer &SerializerBinary::operator<<(int32_t p) | ||
54 | { | ||
55 | printf("int: %d, size: %d\n", p, sizeof(p)); | ||
56 | fwrite(&p, sizeof(p), 1, fhFile); | ||
57 | return *this; | ||
58 | } | ||
59 | Serializer &SerializerBinary::operator<<(int64_t p) | ||
60 | { | ||
61 | fwrite(&p, sizeof(p), 1, fhFile); | ||
62 | return *this; | ||
63 | } | ||
64 | Serializer &SerializerBinary::operator<<(uint8_t p) | ||
65 | { | ||
66 | fwrite(&p, sizeof(p), 1, fhFile); | ||
67 | return *this; | ||
68 | } | ||
69 | Serializer &SerializerBinary::operator<<(uint16_t p) | ||
70 | { | ||
71 | fwrite(&p, sizeof(p), 1, fhFile); | ||
72 | return *this; | ||
73 | } | ||
74 | Serializer &SerializerBinary::operator<<(uint32_t p) | ||
75 | { | ||
76 | fwrite(&p, sizeof(p), 1, fhFile); | ||
77 | return *this; | ||
78 | } | ||
79 | Serializer &SerializerBinary::operator<<(uint64_t p) | ||
80 | { | ||
81 | fwrite(&p, sizeof(p), 1, fhFile); | ||
82 | return *this; | ||
83 | } | ||
84 | Serializer &SerializerBinary::operator<<(float p) | ||
85 | { | ||
86 | fwrite(&p, sizeof(p), 1, fhFile); | ||
87 | return *this; | ||
88 | } | ||
89 | Serializer &SerializerBinary::operator<<(double p) | ||
90 | { | ||
91 | fwrite(&p, sizeof(p), 1, fhFile); | ||
92 | return *this; | ||
93 | } | ||
94 | Serializer &SerializerBinary::operator<<(long double p) | ||
95 | { | ||
96 | fwrite(&p, sizeof(p), 1, fhFile); | ||
97 | return *this; | ||
98 | } | ||
99 | |||
100 | Serializer &SerializerBinary::operator>>(bool &p) | ||
101 | { | ||
102 | bool bTmp; | ||
103 | fread(&bTmp, sizeof(p), 1, fhFile); | ||
104 | p = bTmp; | ||
105 | return *this; | ||
106 | } | ||
107 | Serializer &SerializerBinary::operator>>(int8_t &p) | ||
108 | { | ||
109 | fread(&p, sizeof(p), 1, fhFile); | ||
110 | return *this; | ||
111 | } | ||
112 | Serializer &SerializerBinary::operator>>(int16_t &p) | ||
113 | { | ||
114 | fread(&p, sizeof(p), 1, fhFile); | ||
115 | return *this; | ||
116 | } | ||
117 | Serializer &SerializerBinary::operator>>(int32_t &p) | ||
118 | { | ||
119 | printf("reding before: %d", p); | ||
120 | fread(&p, sizeof(p), 1, fhFile); | ||
121 | printf(" after: %d\n", p); | ||
122 | return *this; | ||
123 | } | ||
124 | Serializer &SerializerBinary::operator>>(int64_t &p) | ||
125 | { | ||
126 | fread(&p, sizeof(p), 1, fhFile); | ||
127 | return *this; | ||
128 | } | ||
129 | Serializer &SerializerBinary::operator>>(uint8_t &p) | ||
130 | { | ||
131 | fread(&p, sizeof(p), 1, fhFile); | ||
132 | return *this; | ||
133 | } | ||
134 | Serializer &SerializerBinary::operator>>(uint16_t &p) | ||
135 | { | ||
136 | fread(&p, sizeof(p), 1, fhFile); | ||
137 | return *this; | ||
138 | } | ||
139 | Serializer &SerializerBinary::operator>>(uint32_t &p) | ||
140 | { | ||
141 | fread(&p, sizeof(p), 1, fhFile); | ||
142 | return *this; | ||
143 | } | ||
144 | Serializer &SerializerBinary::operator>>(uint64_t &p) | ||
145 | { | ||
146 | fread(&p, sizeof(p), 1, fhFile); | ||
147 | return *this; | ||
148 | } | ||
149 | Serializer &SerializerBinary::operator>>(float &p) | ||
150 | { | ||
151 | fread(&p, sizeof(p), 1, fhFile); | ||
152 | return *this; | ||
153 | } | ||
154 | Serializer &SerializerBinary::operator>>(double &p) | ||
155 | { | ||
156 | fread(&p, sizeof(p), 1, fhFile); | ||
157 | return *this; | ||
158 | } | ||
159 | Serializer &SerializerBinary::operator>>(long double &p) | ||
160 | { | ||
161 | fread(&p, sizeof(p), 1, fhFile); | ||
162 | return *this; | ||
163 | } | ||
diff --git a/src/serializerbinary.h b/src/serializerbinary.h new file mode 100644 index 0000000..4a2f301 --- /dev/null +++ b/src/serializerbinary.h | |||
@@ -0,0 +1,46 @@ | |||
1 | #ifndef SERIALIZER_BINARY_H | ||
2 | #define SERIALIZER_BINARY_H | ||
3 | |||
4 | #include "serializer.h" | ||
5 | #include <stdio.h> | ||
6 | |||
7 | class SerializerBinary : public Serializer | ||
8 | { | ||
9 | public: | ||
10 | SerializerBinary(FILE *fhFile, bool bLoading); | ||
11 | SerializerBinary(char *sFileName, bool bLoading); | ||
12 | ~SerializerBinary(); | ||
13 | |||
14 | virtual void close(); | ||
15 | |||
16 | virtual Serializer &operator<<(bool); | ||
17 | virtual Serializer &operator<<(int8_t); | ||
18 | virtual Serializer &operator<<(int16_t); | ||
19 | virtual Serializer &operator<<(int32_t); | ||
20 | virtual Serializer &operator<<(int64_t); | ||
21 | virtual Serializer &operator<<(uint8_t); | ||
22 | virtual Serializer &operator<<(uint16_t); | ||
23 | virtual Serializer &operator<<(uint32_t); | ||
24 | virtual Serializer &operator<<(uint64_t); | ||
25 | virtual Serializer &operator<<(float); | ||
26 | virtual Serializer &operator<<(double); | ||
27 | virtual Serializer &operator<<(long double); | ||
28 | |||
29 | virtual Serializer &operator>>(bool &); | ||
30 | virtual Serializer &operator>>(int8_t &); | ||
31 | virtual Serializer &operator>>(int16_t &); | ||
32 | virtual Serializer &operator>>(int32_t &); | ||
33 | virtual Serializer &operator>>(int64_t &); | ||
34 | virtual Serializer &operator>>(uint8_t &); | ||
35 | virtual Serializer &operator>>(uint16_t &); | ||
36 | virtual Serializer &operator>>(uint32_t &); | ||
37 | virtual Serializer &operator>>(uint64_t &); | ||
38 | virtual Serializer &operator>>(float &); | ||
39 | virtual Serializer &operator>>(double &); | ||
40 | virtual Serializer &operator>>(long double &); | ||
41 | private: | ||
42 | FILE *fhFile; | ||
43 | bool bCloseFile; | ||
44 | }; | ||
45 | |||
46 | #endif | ||
diff --git a/src/test/serialize.cpp b/src/test/serialize.cpp new file mode 100644 index 0000000..883be5e --- /dev/null +++ b/src/test/serialize.cpp | |||
@@ -0,0 +1,24 @@ | |||
1 | #include "serializerbinary.h" | ||
2 | #include <stdio.h> | ||
3 | |||
4 | int main() | ||
5 | { | ||
6 | int32_t one; | ||
7 | double two; | ||
8 | bool three; | ||
9 | SerializerBinary ar("hello.dat", false); | ||
10 | ar << (int)85; | ||
11 | ar << (double)2.63434; | ||
12 | ar << false; | ||
13 | ar.close(); | ||
14 | |||
15 | one = 0; two = 0; three = true; | ||
16 | |||
17 | SerializerBinary ar2("hello.dat", true); | ||
18 | ar2 >> one; | ||
19 | ar2 >> two; | ||
20 | ar2 >> three; | ||
21 | |||
22 | printf("we got %d - %f - %s\n", one, two, (three ? "true":"false")); | ||
23 | return 0; | ||
24 | } | ||