Polaris: BinPtr.h Source File

BinPtr.h

Go to the documentation of this file.
00001 #ifndef _BIN_PTR_H
00002 #define _BIN_PTR_H
00003 ///
00004 ///
00005 /// file BinPtr.h
00006 ///
00007 ///
00008 /// \class BinPtr 
00009 /// \brief a class for doing item I/O on a memory buffer.
00010 /// \defgroup Polaris
00011 /// \ingroup Polaris
00012 ///  Base
00013 /// \see BinPtr.h
00014 /// \see BinPtr.h
00015 ///
00016 /// \endcode
00017 /// \section Overview Overview
00018 /// This classes is intended for reading/writing binary objects to
00019 /// a section of memory.  It is essentially a frontend to the <memory.h>
00020 /// commands.  In addition, we can also read from a file with the same
00021 /// interface.
00022 ///
00023 /// \endcode
00024 /// \section Description Description
00025 /// The BinRep class needs to have easy access to the binary representation
00026 /// of items stored in a stream of memory.  Also, we need to be able to
00027 /// do the same operations is the memory buffer is mapped to a iostream.
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     ///< The file being used.  Will be NULL is not used.
00043     char           *_base;
00044     ///< The beginning of the memory buffer.
00045     char           *_cur;
00046     ///< The current position in the memory buffer.
00047 
00048  public:
00049     BinPtr(const void *base); 
00050     ///< Initialize the BinPtr the the memory buffer at base.
00051 
00052     BinPtr(istream & fs); 
00053     ///< Initialize the BinPtr to the input stream fs.
00054 
00055     BinPtr(ostream & fs); 
00056     ///< Initialize the BinPtr to the output stream fs.
00057 
00058     ~BinPtr();
00059     ///< Destroy the BinPtr.
00060 
00061     
00062 
00063     void fetch(void *buf, int length);
00064     ///< Read in length bytes from the input.
00065     void fetch(binptr_4 & x);
00066     ///< Read a 4 byte integer.
00067     void fetch(binptr_2 & x);
00068     ///< Read a 2 byte integer.
00069     void fetch(binptr_1 & x);
00070     ///< Read a 1 byte integer.
00071     void fetch(double &x);
00072     ///< Read an 8 byte floating point number.
00073 
00074     
00075 
00076     void store(const void *buf, int length); 
00077     ///< Write out length bytes to the output.
00078     void store(long value, int length); 
00079     ///< Write value as a length byte integer.
00080     void store(binptr_4 & x); 
00081     ///< Write a 4 byte integer.
00082     void store(binptr_2 & x); 
00083     ///< Write a 2 byte integer.
00084     void store(binptr_1 & x); 
00085     ///< Write a 1 byte integer.
00086     void store(double &x); 
00087     ///< Write an 8 byte floating point number.
00088 
00089     
00090 
00091     inline void reset(const void *base);
00092     ///< Initialize the BinPtr to base.
00093     inline void reset(istream & fs);
00094     ///< Initialize the BinPtr to input from fs.
00095     inline void reset(ostream & fs);
00096     ///< Initialize the BinPtr to write to fs.
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
 © 1995-2005 University of Illinois, Urbana-Champaign. All rights reserved.  Fri Mar 25 23:05:41 2005