diff options
author | Mike Buland <eichlan@xagasoft.com> | 2006-05-13 01:54:06 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2006-05-13 01:54:06 +0000 |
commit | eb71d88041ccbb0aec3532f9b1aa40e956e948c2 (patch) | |
tree | f2cb344f21b0cac643435701ddf4742c9a581707 | |
parent | 3df557a87b39fc4c2ff14006506e6992d1620ab0 (diff) | |
download | libbu++-eb71d88041ccbb0aec3532f9b1aa40e956e948c2.tar.gz libbu++-eb71d88041ccbb0aec3532f9b1aa40e956e948c2.tar.bz2 libbu++-eb71d88041ccbb0aec3532f9b1aa40e956e948c2.tar.xz libbu++-eb71d88041ccbb0aec3532f9b1aa40e956e948c2.zip |
Changed some things in the serializer...it's cool
-rw-r--r-- | src/serializer.cpp | 30 | ||||
-rw-r--r-- | src/serializer.h | 14 | ||||
-rw-r--r-- | src/serializerbinary.cpp | 10 | ||||
-rw-r--r-- | src/serializerbinary.h | 5 | ||||
-rw-r--r-- | src/staticstring.cpp | 16 | ||||
-rw-r--r-- | src/staticstring.h | 5 | ||||
-rw-r--r-- | src/test/serialize.cpp | 10 |
7 files changed, 68 insertions, 22 deletions
diff --git a/src/serializer.cpp b/src/serializer.cpp index 5982a26..d41cc64 100644 --- a/src/serializer.cpp +++ b/src/serializer.cpp | |||
@@ -1,5 +1,6 @@ | |||
1 | #include "serializer.h" | 1 | #include "serializer.h" |
2 | #include "serializable.h" | 2 | #include "serializable.h" |
3 | #include <list> | ||
3 | 4 | ||
4 | Serializer::Serializer(bool bLoading): | 5 | Serializer::Serializer(bool bLoading): |
5 | bLoading(bLoading) | 6 | bLoading(bLoading) |
@@ -159,16 +160,16 @@ Serializer::Serializer &operator&(long double &p) | |||
159 | }*/ | 160 | }*/ |
160 | 161 | ||
161 | 162 | ||
162 | Serializer &Serializer::operator<<(Serializable &p) | 163 | Serializer &operator<<(Serializer &s, Serializable &p) |
163 | { | 164 | { |
164 | p.serialize(*this); | 165 | p.serialize( s ); |
165 | return *this; | 166 | return s; |
166 | } | 167 | } |
167 | 168 | ||
168 | Serializer &Serializer::operator>>(Serializable &p) | 169 | Serializer &operator>>(Serializer &s, Serializable &p) |
169 | { | 170 | { |
170 | p.serialize(*this); | 171 | p.serialize( s ); |
171 | return *this; | 172 | return s; |
172 | } | 173 | } |
173 | 174 | ||
174 | /*Serializer::Serializer &operator&(Serializable &p) | 175 | /*Serializer::Serializer &operator&(Serializable &p) |
@@ -183,3 +184,20 @@ Serializer &Serializer::operator>>(Serializable &p) | |||
183 | } | 184 | } |
184 | }*/ | 185 | }*/ |
185 | 186 | ||
187 | Serializer &operator<<( Serializer &ar, std::string &s ) | ||
188 | { | ||
189 | ar << s.length(); | ||
190 | ar.write( s.c_str(), s.length() ); | ||
191 | } | ||
192 | |||
193 | Serializer &operator>>( Serializer &ar, std::string &s ) | ||
194 | { | ||
195 | int l; | ||
196 | ar >> l; | ||
197 | char *tmp = new char[l+1]; | ||
198 | tmp[l] = '\0'; | ||
199 | ar.read( tmp, l ); | ||
200 | s = tmp; | ||
201 | delete tmp; | ||
202 | } | ||
203 | |||
diff --git a/src/serializer.h b/src/serializer.h index f2648e3..274d8de 100644 --- a/src/serializer.h +++ b/src/serializer.h | |||
@@ -2,6 +2,8 @@ | |||
2 | #define SERIALIZABLE_H | 2 | #define SERIALIZABLE_H |
3 | 3 | ||
4 | #include <stdint.h> | 4 | #include <stdint.h> |
5 | #include <string> | ||
6 | #include <list> | ||
5 | //#include "serializable.h" | 7 | //#include "serializable.h" |
6 | 8 | ||
7 | class Serializer | 9 | class Serializer |
@@ -15,9 +17,9 @@ public: | |||
15 | virtual ~Serializer(); | 17 | virtual ~Serializer(); |
16 | virtual void close()=0; | 18 | virtual void close()=0; |
17 | 19 | ||
18 | virtual void write(void *, int32_t)=0; | 20 | virtual void write(const void *, int32_t)=0; |
19 | virtual void read(void *, int32_t)=0; | 21 | virtual void read(void *, int32_t)=0; |
20 | 22 | ||
21 | virtual Serializer &operator<<(bool)=0; | 23 | virtual Serializer &operator<<(bool)=0; |
22 | virtual Serializer &operator<<(int8_t)=0; | 24 | virtual Serializer &operator<<(int8_t)=0; |
23 | virtual Serializer &operator<<(int16_t)=0; | 25 | virtual Serializer &operator<<(int16_t)=0; |
@@ -59,9 +61,13 @@ public: | |||
59 | virtual Serializer &operator&(long double &); | 61 | virtual Serializer &operator&(long double &); |
60 | */ | 62 | */ |
61 | 63 | ||
62 | virtual Serializer &operator<<(class Serializable &); | ||
63 | virtual Serializer &operator>>(class Serializable &); | ||
64 | //virtual Serializer &operator&(Serializable &); | 64 | //virtual Serializer &operator&(Serializable &); |
65 | }; | 65 | }; |
66 | 66 | ||
67 | Serializer &operator<<(Serializer &, class Serializable &); | ||
68 | Serializer &operator>>(Serializer &, class Serializable &); | ||
69 | |||
70 | Serializer &operator<<(Serializer &, std::string &); | ||
71 | Serializer &operator>>(Serializer &, std::string &); | ||
72 | |||
67 | #endif | 73 | #endif |
diff --git a/src/serializerbinary.cpp b/src/serializerbinary.cpp index 53e469f..4b741ae 100644 --- a/src/serializerbinary.cpp +++ b/src/serializerbinary.cpp | |||
@@ -1,4 +1,5 @@ | |||
1 | #include "serializerbinary.h" | 1 | #include "serializerbinary.h" |
2 | #include "serializable.h" | ||
2 | 3 | ||
3 | SerializerBinary::SerializerBinary(FILE *fhFile, bool bLoading): | 4 | SerializerBinary::SerializerBinary(FILE *fhFile, bool bLoading): |
4 | Serializer(bLoading), | 5 | Serializer(bLoading), |
@@ -35,7 +36,7 @@ void SerializerBinary::close() | |||
35 | } | 36 | } |
36 | } | 37 | } |
37 | 38 | ||
38 | void SerializerBinary::write(void * pData, int32_t nSize) | 39 | void SerializerBinary::write(const void * pData, int32_t nSize) |
39 | { | 40 | { |
40 | fwrite(pData, nSize, 1, fhFile); | 41 | fwrite(pData, nSize, 1, fhFile); |
41 | } | 42 | } |
@@ -62,7 +63,6 @@ Serializer &SerializerBinary::operator<<(int16_t p) | |||
62 | } | 63 | } |
63 | Serializer &SerializerBinary::operator<<(int32_t p) | 64 | Serializer &SerializerBinary::operator<<(int32_t p) |
64 | { | 65 | { |
65 | printf("int: %d, size: %d\n", p, sizeof(p)); | ||
66 | fwrite(&p, sizeof(p), 1, fhFile); | 66 | fwrite(&p, sizeof(p), 1, fhFile); |
67 | return *this; | 67 | return *this; |
68 | } | 68 | } |
@@ -109,9 +109,7 @@ Serializer &SerializerBinary::operator<<(long double p) | |||
109 | 109 | ||
110 | Serializer &SerializerBinary::operator>>(bool &p) | 110 | Serializer &SerializerBinary::operator>>(bool &p) |
111 | { | 111 | { |
112 | bool bTmp; | 112 | fread(&p, sizeof(p), 1, fhFile); |
113 | fread(&bTmp, sizeof(p), 1, fhFile); | ||
114 | p = bTmp; | ||
115 | return *this; | 113 | return *this; |
116 | } | 114 | } |
117 | Serializer &SerializerBinary::operator>>(int8_t &p) | 115 | Serializer &SerializerBinary::operator>>(int8_t &p) |
@@ -126,9 +124,7 @@ Serializer &SerializerBinary::operator>>(int16_t &p) | |||
126 | } | 124 | } |
127 | Serializer &SerializerBinary::operator>>(int32_t &p) | 125 | Serializer &SerializerBinary::operator>>(int32_t &p) |
128 | { | 126 | { |
129 | printf("reding before: %d", p); | ||
130 | fread(&p, sizeof(p), 1, fhFile); | 127 | fread(&p, sizeof(p), 1, fhFile); |
131 | printf(" after: %d\n", p); | ||
132 | return *this; | 128 | return *this; |
133 | } | 129 | } |
134 | Serializer &SerializerBinary::operator>>(int64_t &p) | 130 | Serializer &SerializerBinary::operator>>(int64_t &p) |
diff --git a/src/serializerbinary.h b/src/serializerbinary.h index 7323d3b..9d68d46 100644 --- a/src/serializerbinary.h +++ b/src/serializerbinary.h | |||
@@ -13,9 +13,9 @@ public: | |||
13 | 13 | ||
14 | virtual void close(); | 14 | virtual void close(); |
15 | 15 | ||
16 | virtual void write(void *, int32_t); | 16 | virtual void write(const void *, int32_t); |
17 | virtual void read(void *, int32_t); | 17 | virtual void read(void *, int32_t); |
18 | 18 | ||
19 | virtual Serializer &operator<<(bool); | 19 | virtual Serializer &operator<<(bool); |
20 | virtual Serializer &operator<<(int8_t); | 20 | virtual Serializer &operator<<(int8_t); |
21 | virtual Serializer &operator<<(int16_t); | 21 | virtual Serializer &operator<<(int16_t); |
@@ -41,6 +41,7 @@ public: | |||
41 | virtual Serializer &operator>>(float &); | 41 | virtual Serializer &operator>>(float &); |
42 | virtual Serializer &operator>>(double &); | 42 | virtual Serializer &operator>>(double &); |
43 | virtual Serializer &operator>>(long double &); | 43 | virtual Serializer &operator>>(long double &); |
44 | |||
44 | private: | 45 | private: |
45 | FILE *fhFile; | 46 | FILE *fhFile; |
46 | bool bCloseFile; | 47 | bool bCloseFile; |
diff --git a/src/staticstring.cpp b/src/staticstring.cpp index 7543178..24b9ecb 100644 --- a/src/staticstring.cpp +++ b/src/staticstring.cpp | |||
@@ -1,4 +1,5 @@ | |||
1 | #include "staticstring.h" | 1 | #include "staticstring.h" |
2 | #include "serializer.h" | ||
2 | 3 | ||
3 | #include <stdlib.h> | 4 | #include <stdlib.h> |
4 | #include <stdio.h> | 5 | #include <stdio.h> |
@@ -182,3 +183,18 @@ void StaticString::clear() | |||
182 | memset( lpStr, 0, nLen+1 ); | 183 | memset( lpStr, 0, nLen+1 ); |
183 | } | 184 | } |
184 | 185 | ||
186 | void StaticString::serialize( Serializer &ar ) | ||
187 | { | ||
188 | if( ar.isLoading() ) | ||
189 | { | ||
190 | ar >> nLen; | ||
191 | setLength( nLen ); | ||
192 | ar.read( lpStr, nLen ); | ||
193 | } | ||
194 | else | ||
195 | { | ||
196 | ar << nLen; | ||
197 | ar.write( lpStr, nLen ); | ||
198 | } | ||
199 | } | ||
200 | |||
diff --git a/src/staticstring.h b/src/staticstring.h index 88cae70..1c98b3c 100644 --- a/src/staticstring.h +++ b/src/staticstring.h | |||
@@ -2,6 +2,7 @@ | |||
2 | #define STATIC_STRING_H | 2 | #define STATIC_STRING_H |
3 | 3 | ||
4 | #include <string> | 4 | #include <string> |
5 | #include "serializable.h" | ||
5 | 6 | ||
6 | /** | 7 | /** |
7 | * Simple string managing class. Allows for dynamically allocated string data | 8 | * Simple string managing class. Allows for dynamically allocated string data |
@@ -10,7 +11,7 @@ | |||
10 | * and making accessing meta-info like length fast and reliable as well. | 11 | * and making accessing meta-info like length fast and reliable as well. |
11 | *@author Mike Buland | 12 | *@author Mike Buland |
12 | */ | 13 | */ |
13 | class StaticString | 14 | class StaticString : public Serializable |
14 | { | 15 | { |
15 | public: | 16 | public: |
16 | StaticString(); | 17 | StaticString(); |
@@ -40,6 +41,8 @@ public: | |||
40 | 41 | ||
41 | void clear(); | 42 | void clear(); |
42 | 43 | ||
44 | virtual void serialize( class Serializer &ar ); | ||
45 | |||
43 | private: | 46 | private: |
44 | char *lpStr; | 47 | char *lpStr; |
45 | int nLen; | 48 | int nLen; |
diff --git a/src/test/serialize.cpp b/src/test/serialize.cpp index 883be5e..e233704 100644 --- a/src/test/serialize.cpp +++ b/src/test/serialize.cpp | |||
@@ -1,24 +1,30 @@ | |||
1 | #include "serializerbinary.h" | 1 | #include "serializerbinary.h" |
2 | #include "staticstring.h" | ||
2 | #include <stdio.h> | 3 | #include <stdio.h> |
4 | #include <string> | ||
3 | 5 | ||
4 | int main() | 6 | int main() |
5 | { | 7 | { |
6 | int32_t one; | 8 | int32_t one; |
7 | double two; | 9 | double two; |
8 | bool three; | 10 | bool three; |
11 | StaticString s("Test string!"); | ||
12 | std::string ss("Another test string"); | ||
9 | SerializerBinary ar("hello.dat", false); | 13 | SerializerBinary ar("hello.dat", false); |
10 | ar << (int)85; | 14 | ar << (int)85; |
11 | ar << (double)2.63434; | 15 | ar << (double)2.63434; |
12 | ar << false; | 16 | ar << false; |
17 | ar << ss; | ||
13 | ar.close(); | 18 | ar.close(); |
14 | 19 | ||
15 | one = 0; two = 0; three = true; | 20 | one = 0; two = 0; three = true; s = "die"; |
16 | 21 | ||
17 | SerializerBinary ar2("hello.dat", true); | 22 | SerializerBinary ar2("hello.dat", true); |
18 | ar2 >> one; | 23 | ar2 >> one; |
19 | ar2 >> two; | 24 | ar2 >> two; |
20 | ar2 >> three; | 25 | ar2 >> three; |
26 | ar2 >> s; | ||
21 | 27 | ||
22 | printf("we got %d - %f - %s\n", one, two, (three ? "true":"false")); | 28 | printf("we got %d - %f - %s - \"%s\"\n", one, two, (three ? "true":"false"), s.getString() ); |
23 | return 0; | 29 | return 0; |
24 | } | 30 | } |