summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/fstring.h61
-rw-r--r--src/hash.h47
-rw-r--r--src/serializer.cpp94
-rw-r--r--src/serializer.h29
-rw-r--r--src/tests/fstring.cpp1
5 files changed, 162 insertions, 70 deletions
diff --git a/src/fstring.h b/src/fstring.h
index 585073f..2f508b7 100644
--- a/src/fstring.h
+++ b/src/fstring.h
@@ -3,8 +3,10 @@
3 3
4#include <stdint.h> 4#include <stdint.h>
5#include <memory> 5#include <memory>
6#include "serializable.h"
7#include "serializer.h"
6 8
7template< typename chr=char > 9template< typename chr >
8struct FStringChunk 10struct FStringChunk
9{ 11{
10 long nLength; 12 long nLength;
@@ -23,9 +25,12 @@ struct FStringChunk
23 * data is actually copied. This also means that you never need to put any 25 * data is actually copied. This also means that you never need to put any
24 * FBasicString into a ref-counting container class. 26 * FBasicString into a ref-counting container class.
25 */ 27 */
26template< typename chr=char, typename chralloc=std::allocator<chr>, typename chunkalloc=std::allocator<struct FStringChunk<chr> > > 28template< typename chr, typename chralloc=std::allocator<chr>, typename chunkalloc=std::allocator<struct FStringChunk<chr> > >
27class FBasicString 29class FBasicString : public Serializable
28{ 30{
31#ifndef VALTEST
32#define cpy( dest, src, size ) memcpy( dest, src, size*sizeof(chr) )
33#endif
29private: 34private:
30 typedef struct FStringChunk<chr> Chunk; 35 typedef struct FStringChunk<chr> Chunk;
31 typedef struct FBasicString<chr, chralloc, chunkalloc> MyType; 36 typedef struct FBasicString<chr, chralloc, chunkalloc> MyType;
@@ -57,16 +62,6 @@ public:
57 append( pData, nLength ); 62 append( pData, nLength );
58 } 63 }
59 64
60 /*
61 FBasicString( MyType &rSrc ) :
62 nLength( 0 ),
63 pnRefs( NULL ),
64 pFirst( NULL ),
65 pLast( NULL )
66 {
67 joinShare( rSrc );
68 }*/
69
70 FBasicString( const MyType &rSrc ) : 65 FBasicString( const MyType &rSrc ) :
71 nLength( 0 ), 66 nLength( 0 ),
72 pnRefs( NULL ), 67 pnRefs( NULL ),
@@ -131,6 +126,23 @@ public:
131 realClear(); 126 realClear();
132 } 127 }
133 128
129 void resize( long nNewSize )
130 {
131 if( nLength == nNewSize )
132 return;
133
134 flatten();
135
136 Chunk *pNew = newChunk( nNewSize );
137 long nNewLen = (nNewSize<nLength)?(nNewSize):(nLength);
138 cpy( pNew->pData, pFirst->pData, nNewLen );
139 pNew->pData[nNewLen] = (chr)0;
140 aChr.deallocate( pFirst->pData, pFirst->nLength+1 );
141 aChunk.deallocate( pFirst, 1 );
142 pFirst = pLast = pNew;
143 nLength = nNewSize;
144 }
145
134 chr *c_str() 146 chr *c_str()
135 { 147 {
136 if( pFirst == NULL ) 148 if( pFirst == NULL )
@@ -243,6 +255,27 @@ public:
243 return pFirst->pData[nIndex]; 255 return pFirst->pData[nIndex];
244 } 256 }
245 257
258 void serialize( class Serializer &ar )
259 {
260 if( ar.isLoading() )
261 {
262 clear();
263 long nLen;
264 ar >> nLen;
265
266 Chunk *pNew = newChunk( nLen );
267 ar.read( pNew->pData, nLen*sizeof(chr) );
268 appendChunk( pNew );
269 }
270 else
271 {
272 flatten();
273
274 ar << nLength;
275 ar.write( pFirst->pData, nLength*sizeof(chr) );
276 }
277 }
278
246private: 279private:
247 void flatten() const 280 void flatten() const
248 { 281 {
@@ -471,6 +504,7 @@ private:
471 realClear(); 504 realClear();
472 } 505 }
473 506
507#ifdef VALTEST
474 void cpy( chr *dest, const chr *src, long count ) const 508 void cpy( chr *dest, const chr *src, long count ) const
475 { 509 {
476 for( int j = 0; j < count; j++ ) 510 for( int j = 0; j < count; j++ )
@@ -480,6 +514,7 @@ private:
480 src++; 514 src++;
481 } 515 }
482 } 516 }
517#endif
483 518
484 void initCount() const 519 void initCount() const
485 { 520 {
diff --git a/src/hash.h b/src/hash.h
index 6671ae6..c37cfba 100644
--- a/src/hash.h
+++ b/src/hash.h
@@ -7,6 +7,8 @@
7#include <iostream> 7#include <iostream>
8#include "exceptionbase.h" 8#include "exceptionbase.h"
9#include "hashable.h" 9#include "hashable.h"
10#include "serializable.h"
11#include "serializer.h"
10 12
11#define bitsToBytes( n ) (n/32+(n%32>0 ? 1 : 0)) 13#define bitsToBytes( n ) (n/32+(n%32>0 ? 1 : 0))
12 14
@@ -241,7 +243,7 @@ public:
241 243
242 uint32_t size() 244 uint32_t size()
243 { 245 {
244 return nFilled; 246 return nFilled-nDeleted;
245 } 247 }
246 248
247 uint32_t getDeleted() 249 uint32_t getDeleted()
@@ -667,4 +669,47 @@ template<> bool __cmpHashKeys<std::string>( const std::string &a, const std::str
667template<> uint32_t __calcHashCode<Hashable>( const Hashable &k ); 669template<> uint32_t __calcHashCode<Hashable>( const Hashable &k );
668template<> bool __cmpHashKeys<Hashable>( const Hashable &a, const Hashable &b ); 670template<> bool __cmpHashKeys<Hashable>( const Hashable &a, const Hashable &b );
669 671
672template<typename key, typename value>
673Serializer &operator<<( Serializer &ar, Hash<key,value> &h )
674{
675 ar << h.size();
676 for( typename Hash<key,value>::iterator i = h.begin(); i != h.end(); i++ )
677 {
678 std::pair<key,value> p = *i;
679 ar << p.first << p.second;
680 }
681
682 return ar;
683}
684
685template<typename key, typename value>
686Serializer &operator>>( Serializer &ar, Hash<key,value> &h )
687{
688 h.clear();
689 uint32_t nSize;
690 ar >> nSize;
691
692 for( uint32_t j = 0; j < nSize; j++ )
693 {
694 key k; value v;
695 ar >> k >> v;
696 h.insert( k, v );
697 }
698
699 return ar;
700}
701
702template<typename key, typename value>
703Serializer &operator&&( Serializer &ar, Hash<key,value> &h )
704{
705 if( ar.isLoading() )
706 {
707 return ar >> h;
708 }
709 else
710 {
711 return ar << h;
712 }
713}
714
670#endif 715#endif
diff --git a/src/serializer.cpp b/src/serializer.cpp
index 407aab2..636224e 100644
--- a/src/serializer.cpp
+++ b/src/serializer.cpp
@@ -59,6 +59,11 @@ Serializer &Serializer::operator<<(uint64_t p)
59 write( &p, sizeof(p) ); 59 write( &p, sizeof(p) );
60 return *this; 60 return *this;
61} 61}
62Serializer &Serializer::operator<<(long p)
63{
64 write( &p, sizeof(p) );
65 return *this;
66}
62Serializer &Serializer::operator<<(float p) 67Serializer &Serializer::operator<<(float p)
63{ 68{
64 write( &p, sizeof(p) ); 69 write( &p, sizeof(p) );
@@ -120,6 +125,11 @@ Serializer &Serializer::operator>>(uint64_t &p)
120 read( &p, sizeof(p) ); 125 read( &p, sizeof(p) );
121 return *this; 126 return *this;
122} 127}
128Serializer &Serializer::operator>>(long &p)
129{
130 read( &p, sizeof(p) );
131 return *this;
132}
123Serializer &Serializer::operator>>(float &p) 133Serializer &Serializer::operator>>(float &p)
124{ 134{
125 read( &p, sizeof(p) ); 135 read( &p, sizeof(p) );
@@ -136,149 +146,149 @@ Serializer &Serializer::operator>>(long double &p)
136 return *this; 146 return *this;
137} 147}
138 148
139/*Serializer::Serializer &operator&(bool &p) 149Serializer &Serializer::operator&&(bool &p)
140{ 150{
141 if (bLoading) 151 if (bLoading)
142 { 152 {
143 return *this << p; 153 return *this >> p;
144 } 154 }
145 else 155 else
146 { 156 {
147 return *this >> p; 157 return *this << p;
148 } 158 }
149} 159}
150 160
151Serializer::Serializer &operator&(int8_t &p) 161Serializer &Serializer::operator&&(int8_t &p)
152{ 162{
153 if (bLoading) 163 if (bLoading)
154 { 164 {
155 return *this << p; 165 return *this >> p;
156 } 166 }
157 else 167 else
158 { 168 {
159 return *this >> p; 169 return *this << p;
160 } 170 }
161} 171}
162 172
163Serializer::Serializer &operator&(int16_t &p) 173Serializer &Serializer::operator&&(int16_t &p)
164{ 174{
165 if (bLoading) 175 if (bLoading)
166 { 176 {
167 return *this << p; 177 return *this >> p;
168 } 178 }
169 else 179 else
170 { 180 {
171 return *this >> p; 181 return *this << p;
172 } 182 }
173} 183}
174 184
175Serializer::Serializer &operator&(int32_t &p) 185Serializer &Serializer::operator&&(int32_t &p)
176{ 186{
177 if (bLoading) 187 if (bLoading)
178 { 188 {
179 return *this << p; 189 return *this >> p;
180 } 190 }
181 else 191 else
182 { 192 {
183 return *this >> p; 193 return *this << p;
184 } 194 }
185} 195}
186 196
187Serializer::Serializer &operator&(int64_t &p) 197Serializer &Serializer::operator&&(int64_t &p)
188{ 198{
189 if (bLoading) 199 if (bLoading)
190 { 200 {
191 return *this << p; 201 return *this >> p;
192 } 202 }
193 else 203 else
194 { 204 {
195 return *this >> p; 205 return *this << p;
196 } 206 }
197} 207}
198 208
199Serializer::Serializer &operator&(uint8_t &p) 209Serializer &Serializer::operator&&(uint8_t &p)
200{ 210{
201 if (bLoading) 211 if (bLoading)
202 { 212 {
203 return *this << p; 213 return *this >> p;
204 } 214 }
205 else 215 else
206 { 216 {
207 return *this >> p; 217 return *this << p;
208 } 218 }
209} 219}
210 220
211Serializer::Serializer &operator&(uint16_t &p) 221Serializer &Serializer::operator&&(uint16_t &p)
212{ 222{
213 if (bLoading) 223 if (bLoading)
214 { 224 {
215 return *this << p; 225 return *this >> p;
216 } 226 }
217 else 227 else
218 { 228 {
219 return *this >> p; 229 return *this << p;
220 } 230 }
221} 231}
222 232
223Serializer::Serializer &operator&(uint32_t &p) 233Serializer &Serializer::operator&&(uint32_t &p)
224{ 234{
225 if (bLoading) 235 if (bLoading)
226 { 236 {
227 return *this << p; 237 return *this >> p;
228 } 238 }
229 else 239 else
230 { 240 {
231 return *this >> p; 241 return *this << p;
232 } 242 }
233} 243}
234 244
235Serializer::Serializer &operator&(uint64_t &p) 245Serializer &Serializer::operator&&(uint64_t &p)
236{ 246{
237 if (bLoading) 247 if (bLoading)
238 { 248 {
239 return *this << p; 249 return *this >> p;
240 } 250 }
241 else 251 else
242 { 252 {
243 return *this >> p; 253 return *this << p;
244 } 254 }
245} 255}
246 256
247Serializer::Serializer &operator&(float &p) 257Serializer &Serializer::operator&&(float &p)
248{ 258{
249 if (bLoading) 259 if (bLoading)
250 { 260 {
251 return *this << p; 261 return *this >> p;
252 } 262 }
253 else 263 else
254 { 264 {
255 return *this >> p; 265 return *this << p;
256 } 266 }
257} 267}
258 268
259Serializer::Serializer &operator&(double &p) 269Serializer &Serializer::operator&&(double &p)
260{ 270{
261 if (bLoading) 271 if (bLoading)
262 { 272 {
263 return *this << p; 273 return *this >> p;
264 } 274 }
265 else 275 else
266 { 276 {
267 return *this >> p; 277 return *this << p;
268 } 278 }
269} 279}
270 280
271Serializer::Serializer &operator&(long double &p) 281Serializer &Serializer::operator&&(long double &p)
272{ 282{
273 if (bLoading) 283 if (bLoading)
274 { 284 {
275 return *this << p; 285 return *this >> p;
276 } 286 }
277 else 287 else
278 { 288 {
279 return *this >> p; 289 return *this << p;
280 } 290 }
281}*/ 291}
282 292
283 293
284Serializer &operator<<(Serializer &s, Serializable &p) 294Serializer &operator<<(Serializer &s, Serializable &p)
@@ -293,17 +303,17 @@ Serializer &operator>>(Serializer &s, Serializable &p)
293 return s; 303 return s;
294} 304}
295 305
296/*Serializer::Serializer &operator&(Serializable &p) 306Serializer &operator&&(Serializer &s, Serializable &p)
297{ 307{
298 if (bLoading) 308 if (s.isLoading())
299 { 309 {
300 return *this << p; 310 return s >> p;
301 } 311 }
302 else 312 else
303 { 313 {
304 return *this >> p; 314 return s << p;
305 } 315 }
306}*/ 316}
307 317
308Serializer &operator<<( Serializer &ar, std::string &s ) 318Serializer &operator<<( Serializer &ar, std::string &s )
309{ 319{
diff --git a/src/serializer.h b/src/serializer.h
index e79c810..3af489c 100644
--- a/src/serializer.h
+++ b/src/serializer.h
@@ -35,6 +35,7 @@ public:
35 virtual Serializer &operator<<(uint16_t); 35 virtual Serializer &operator<<(uint16_t);
36 virtual Serializer &operator<<(uint32_t); 36 virtual Serializer &operator<<(uint32_t);
37 virtual Serializer &operator<<(uint64_t); 37 virtual Serializer &operator<<(uint64_t);
38 virtual Serializer &operator<<(long);
38 virtual Serializer &operator<<(float); 39 virtual Serializer &operator<<(float);
39 virtual Serializer &operator<<(double); 40 virtual Serializer &operator<<(double);
40 virtual Serializer &operator<<(long double); 41 virtual Serializer &operator<<(long double);
@@ -48,30 +49,30 @@ public:
48 virtual Serializer &operator>>(uint16_t &); 49 virtual Serializer &operator>>(uint16_t &);
49 virtual Serializer &operator>>(uint32_t &); 50 virtual Serializer &operator>>(uint32_t &);
50 virtual Serializer &operator>>(uint64_t &); 51 virtual Serializer &operator>>(uint64_t &);
52 virtual Serializer &operator>>(long &);
51 virtual Serializer &operator>>(float &); 53 virtual Serializer &operator>>(float &);
52 virtual Serializer &operator>>(double &); 54 virtual Serializer &operator>>(double &);
53 virtual Serializer &operator>>(long double &); 55 virtual Serializer &operator>>(long double &);
54 56
55 /* 57 virtual Serializer &operator&&(bool &);
56 virtual Serializer &operator&(bool &); 58 virtual Serializer &operator&&(int8_t &);
57 virtual Serializer &operator&(int8_t &); 59 virtual Serializer &operator&&(int16_t &);
58 virtual Serializer &operator&(int16_t &); 60 virtual Serializer &operator&&(int32_t &);
59 virtual Serializer &operator&(int32_t &); 61 virtual Serializer &operator&&(int64_t &);
60 virtual Serializer &operator&(int64_t &); 62 virtual Serializer &operator&&(uint8_t &);
61 virtual Serializer &operator&(uint8_t &); 63 virtual Serializer &operator&&(uint16_t &);
62 virtual Serializer &operator&(uint16_t &); 64 virtual Serializer &operator&&(uint32_t &);
63 virtual Serializer &operator&(uint32_t &); 65 virtual Serializer &operator&&(uint64_t &);
64 virtual Serializer &operator&(uint64_t &); 66 virtual Serializer &operator&&(float &);
65 virtual Serializer &operator&(float &); 67 virtual Serializer &operator&&(double &);
66 virtual Serializer &operator&(double &); 68 virtual Serializer &operator&&(long double &);
67 virtual Serializer &operator&(long double &);
68 */
69 69
70 //virtual Serializer &operator&(Serializable &); 70 //virtual Serializer &operator&(Serializable &);
71}; 71};
72 72
73Serializer &operator<<(Serializer &, class Serializable &); 73Serializer &operator<<(Serializer &, class Serializable &);
74Serializer &operator>>(Serializer &, class Serializable &); 74Serializer &operator>>(Serializer &, class Serializable &);
75Serializer &operator&&(Serializer &s, class Serializable &p);
75 76
76Serializer &operator<<(Serializer &, std::string &); 77Serializer &operator<<(Serializer &, std::string &);
77Serializer &operator>>(Serializer &, std::string &); 78Serializer &operator>>(Serializer &, std::string &);
diff --git a/src/tests/fstring.cpp b/src/tests/fstring.cpp
index 33e24b4..271738c 100644
--- a/src/tests/fstring.cpp
+++ b/src/tests/fstring.cpp
@@ -41,6 +41,7 @@ int main( int argc, char *argv )
41 pem; 41 pem;
42 42
43 thing( str2 ); 43 thing( str2 );
44 thing("test.");
44 45
45 printf("%d == %d\n", __calcHashCode( str ), __calcHashCode( str.c_str() ) ); 46 printf("%d == %d\n", __calcHashCode( str ), __calcHashCode( str.c_str() ) );
46} 47}