source: dev/trunk/jenga/include/common/Binaly.h@ 337

Last change on this file since 337 was 189, checked in by dai_9181, 17 years ago
File size: 3.1 KB
RevLine 
[189]1#pragma once
2
3#include <string>
4#include <vector>
5
6#include <memory.h>
7#include <windows.h>
8
9#define JENGA_SERIALIZER(param) ( isRead?(binaly.Read(param)):(binaly << param) );
10
11class MemoryBlock
12{
13 int base;
14 int size;
15public:
16 MemoryBlock( int base, int size )
17 : base( base )
18 , size( size )
19 {
20 }
21 int GetBase() const
22 {
23 return base;
24 }
25 int GetSize() const
26 {
27 return size;
28 }
29};
30class Binaly
31{
32 static const int alignment = 1024 * 1024;
33
34 unsigned char *pBuffer;
35 int size;
36 int maxSize;
37
38 std::vector<MemoryBlock> memoryBlocks;
39 int indexOfMemoryBlocks;
40
41 // 初期化
42 void Initialize()
43 {
44 maxSize = alignment;
45 pBuffer = (unsigned char *)calloc( maxSize, 1 );
46 size = 0;
47
48 memoryBlocks.clear();
49 indexOfMemoryBlocks = 0;
50 }
51
52 // メモリ領域の再確保
53 void Allocator( int newSize )
54 {
55 size = newSize;
56
57 if( maxSize > newSize )
58 {
59 // 確保済みメモリのサイズがあまっているとき
60 return;
61 }
62
63 while( maxSize <= newSize )
64 {
65 maxSize += alignment;
66 }
67 pBuffer = (unsigned char *)realloc( pBuffer, maxSize );
68 }
69 void AddMemorySize( int plusSize )
70 {
71 Allocator( size + plusSize );
72 }
73 void ReadMemoryCheck( int readSize )
74 {
75 if( (int)memoryBlocks.size() < indexOfMemoryBlocks )
76 {
77 throw "bad memory access";
78 }
79
80 if( memoryBlocks[indexOfMemoryBlocks].GetSize() != readSize )
81 {
82 throw "bad memory access";
83 }
84 }
85
86public:
87 Binaly( const Binaly &binaly )
88 {
89 Initialize();
90 }
91 Binaly( const unsigned char *pNewBuffer, int size )
92 {
93 Initialize();
94 }
95 Binaly()
96 {
97 Initialize();
98 }
99 ~Binaly()
100 {
101 free( pBuffer );
102 }
103
104 void Clear()
105 {
106 free( pBuffer );
107 Initialize();
108 }
109
110 void Add( const unsigned char *pBufferOfBlock, int sizeOfBlock )
111 {
112 int base = this->size;
113 AddMemorySize( sizeOfBlock );
114 memcpy( pBuffer + base, pBufferOfBlock, sizeOfBlock );
115
116 memoryBlocks.push_back( MemoryBlock( base, sizeOfBlock ) );
117 }
118 void operator<< ( int i )
119 {
120 Add( (const unsigned char *)&i, sizeof(int) );
121 }
122 void operator<< ( double dbl )
123 {
124 Add( (const unsigned char *)&dbl, sizeof(double) );
125 }
126 void operator<< ( bool b )
127 {
128 Add( (const unsigned char *)&b, sizeof(bool) );
129 }
130 void operator<< ( const std::string &str )
131 {
132 Add( (const unsigned char *)str.c_str(), (int)str.size() );
133 }
134 void operator<< ( const char *lpszStr )
135 {
136 Add( (const unsigned char *)lpszStr, lstrlen(lpszStr) );
137 }
138
139 int GetNextSizeToReading()
140 {
141 return memoryBlocks[indexOfMemoryBlocks].GetSize();
142 }
143 void Read( unsigned char *pBuffer, int readSize )
144 {
145 ReadMemoryCheck( readSize );
146
147 memcpy(
148 pBuffer,
149 this->pBuffer + memoryBlocks[indexOfMemoryBlocks].GetBase(),
150 memoryBlocks[indexOfMemoryBlocks].GetSize()
151 );
152
153 indexOfMemoryBlocks++;
154 }
155 void Read( int &i )
156 {
157 Read( (unsigned char *)&i, sizeof(int) );
158 }
159 void Read( bool &b )
160 {
161 Read( (unsigned char *)&b, sizeof(bool) );
162 }
163 void Read( std::string &str )
164 {
165 str = std::string(
166 (const char *)pBuffer + memoryBlocks[indexOfMemoryBlocks].GetBase(),
167 memoryBlocks[indexOfMemoryBlocks].GetSize()
168 );
169 }
170};
Note: See TracBrowser for help on using the repository browser.