00001 #ifndef _BIN_PTR_H
00002 #define _BIN_PTR_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "ClassNames.h"
00030 #include <stream.h>
00031 #include <memory.h>
00032 #include "macros.h"
00033
00034 typedef char binptr_1;
00035 typedef short binptr_2;
00036 typedef int binptr_4;
00037 typedef double binptr_8;
00038
00039 class BinPtr {
00040 private:
00041 iostream * _fs;
00042
00043 char *_base;
00044
00045 char *_cur;
00046
00047
00048 public:
00049 BinPtr(const void *base);
00050
00051
00052 BinPtr(istream & fs);
00053
00054
00055 BinPtr(ostream & fs);
00056
00057
00058 ~BinPtr();
00059
00060
00061
00062
00063 void fetch(void *buf, int length);
00064
00065 void fetch(binptr_4 & x);
00066
00067 void fetch(binptr_2 & x);
00068
00069 void fetch(binptr_1 & x);
00070
00071 void fetch(double &x);
00072
00073
00074
00075
00076 void store(const void *buf, int length);
00077
00078 void store(long value, int length);
00079
00080 void store(binptr_4 & x);
00081
00082 void store(binptr_2 & x);
00083
00084 void store(binptr_1 & x);
00085
00086 void store(double &x);
00087
00088
00089
00090
00091 inline void reset(const void *base);
00092
00093 inline void reset(istream & fs);
00094
00095 inline void reset(ostream & fs);
00096
00097 };
00098
00099
00100
00101 inline
00102 BinPtr::BinPtr(const void *base)
00103 {
00104 #ifdef CLASS_INSTANCE_REGISTRY
00105 register_instance(BIN_PTR, sizeof(BinPtr), this);
00106 #endif
00107
00108 reset(base);
00109 }
00110
00111 inline
00112 BinPtr::BinPtr(istream & fs)
00113 {
00114 #ifdef CLASS_INSTANCE_REGISTRY
00115 register_instance(BIN_PTR, sizeof(BinPtr), this);
00116 #endif
00117
00118 reset(fs);
00119 }
00120
00121 inline
00122 BinPtr::BinPtr(ostream & fs)
00123 {
00124 #ifdef CLASS_INSTANCE_REGISTRY
00125 register_instance(BIN_PTR, sizeof(BinPtr), this);
00126 #endif
00127
00128 reset(fs);
00129 }
00130
00131 inline
00132 BinPtr::~BinPtr()
00133 {
00134 #ifdef CLASS_INSTANCE_REGISTRY
00135 unregister_instance(BIN_PTR, this);
00136 #endif
00137 }
00138
00139
00140
00141 inline void
00142 BinPtr::fetch(void *buf, int length)
00143 {
00144 if (_fs)
00145 _fs->read((char *) buf, length);
00146 else {
00147 memcpy(buf, _cur, length);
00148 _cur += length;
00149 }
00150 }
00151
00152 inline void
00153 BinPtr::fetch(binptr_4 & x)
00154 {
00155 if (_fs)
00156 _fs->read((char *) &x, sizeof(x));
00157 else {
00158 memcpy(&x, _cur, sizeof(x));
00159 _cur += sizeof(x);
00160 }
00161 }
00162
00163 inline void
00164 BinPtr::fetch(binptr_2 & x)
00165 {
00166 if (_fs)
00167 _fs->read((char *) &x, sizeof(x));
00168 else {
00169 memcpy(&x, _cur, sizeof(x));
00170 _cur += sizeof(x);
00171 }
00172 }
00173
00174 inline void
00175 BinPtr::fetch(binptr_1 & x)
00176 {
00177 if (_fs)
00178 _fs->read((char *) &x, sizeof(x));
00179 else {
00180 memcpy(&x, _cur, sizeof(x));
00181 _cur += sizeof(x);
00182 }
00183 }
00184
00185 inline void
00186 BinPtr::fetch(double &x)
00187 {
00188 if (_fs)
00189 _fs->read((char *) &x, sizeof(x));
00190 else {
00191 memcpy(&x, _cur, sizeof(x));
00192 _cur += sizeof(x);
00193 }
00194 }
00195
00196
00197
00198 inline void
00199 BinPtr::store(const void *buf, int length)
00200 {
00201 if (_fs)
00202 _fs->write((char *) buf, length);
00203 else {
00204 memcpy(_cur, buf, length);
00205 _cur += length;
00206 }
00207 }
00208
00209 inline void
00210 BinPtr::store(long value, int length)
00211 {
00212 switch (length) {
00213 case 1:
00214 {
00215 binptr_1 x = (binptr_1) value;
00216
00217 if (_fs)
00218 _fs->write((char *) &x, sizeof(x));
00219 else {
00220 memcpy(_cur, &x, length);
00221 _cur += length;
00222 }
00223 }
00224 break;
00225
00226 case 2:
00227 {
00228 binptr_2 x = (binptr_2) value;
00229
00230 if (_fs)
00231 _fs->write((char *) &x, sizeof(x));
00232 else {
00233 memcpy(_cur, &x, length);
00234 _cur += length;
00235 }
00236 }
00237 break;
00238
00239 case 4:
00240 {
00241 binptr_4 x = (binptr_4) value;
00242
00243 if (_fs)
00244 _fs->write((char *) &x, sizeof(x));
00245 else {
00246 memcpy(_cur, &x, length);
00247 _cur += length;
00248 }
00249 }
00250 break;
00251 }
00252 }
00253
00254 inline void
00255 BinPtr::store(binptr_4 & x)
00256 {
00257 if (_fs)
00258 _fs->write((char *) &x, sizeof(x));
00259 else {
00260 memcpy(_cur, &x, sizeof(x));
00261 _cur += sizeof(x);
00262 }
00263 }
00264
00265 inline void
00266 BinPtr::store(binptr_2 & x)
00267 {
00268 if (_fs)
00269 _fs->write((char *) &x, sizeof(x));
00270 else {
00271 memcpy(_cur, &x, sizeof(x));
00272 _cur += sizeof(x);
00273 }
00274 }
00275
00276 inline void
00277 BinPtr::store(binptr_1 & x)
00278 {
00279 if (_fs)
00280 _fs->write((char *) &x, sizeof(x));
00281 else {
00282 memcpy(_cur, &x, sizeof(x));
00283 _cur += sizeof(x);
00284 }
00285 }
00286
00287 inline void
00288 BinPtr::store(double &x)
00289 {
00290 if (_fs)
00291 _fs->write((char *) &x, sizeof(x));
00292 else {
00293 memcpy(_cur, &x, sizeof(x));
00294 _cur += sizeof(x);
00295 }
00296 }
00297
00298
00299
00300 inline void
00301 BinPtr::reset(const void *base)
00302 {
00303 _cur = _base = (char *) base;
00304 _fs = 0;
00305 }
00306
00307 inline void
00308 BinPtr::reset(istream & fs)
00309 {
00310 _cur = _base = 0;
00311 _fs = (iostream *) & fs;
00312 }
00313
00314 inline void
00315 BinPtr::reset(ostream & fs)
00316 {
00317 _cur = _base = 0;
00318 _fs = (iostream *) & fs;
00319 }
00320
00321 #endif