Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

program.h

Go to the documentation of this file.
00001 00002 00003 #ifndef __program_h__ 00004 #define __program_h__ 00005 00006 00007 00008 #include "common.h" 00009 00010 00011 00013 class program 00014 : public std::vector<char> 00015 { 00016 public: 00017 program() {} 00018 ~program() {} 00019 00021 00025 void generate(uInt seed); 00026 00028 00034 template <typename T> 00035 T read(address addr) 00036 { 00037 ASSERT(size()>0,"cannot read from 0 length program"); 00038 00039 // get the size 00040 address size_ = static_cast<address>(size()); 00041 00042 // make sure the address is in range 00043 addr%=size_; 00044 00045 // check if the data overruns the end of the program 00046 if(addr+sizeof(T)<size_) 00047 { 00048 // if not then its simple 00049 // return the data at the address 00050 return *reinterpret_cast<T*>( &( (*this)[addr] ) ); 00051 } 00052 00053 // otherwise we need to read part of the data 00054 // from the end of the program and part of it 00055 // from the start 00056 00057 // temporaray variable to hold the return value 00058 T data_; 00059 00060 // calculate the number of bytes of data before the split 00061 uInt split = size_-addr; 00062 00063 // copy the first part of the data into the temporary 00064 // variable 00065 memcpy(&data_, &((*this)[addr]), split); 00066 00067 // copy the second part of the data into the temporary 00068 // variable 00069 memcpy(reinterpret_cast<char*>((&data_))+split, &((*this)[0]), sizeof(T)-split); 00070 00071 // return the temporary variable 00072 return data_; 00073 } 00074 00076 00082 template <typename T> 00083 void write(address addr, T val) 00084 { 00085 ASSERT(size()>0,"cannot write to 0 length program"); 00086 // get the size 00087 address size_ = static_cast<address>(size()); 00088 00089 // make sure the address is in range 00090 addr%=size_; 00091 00092 // check if the data overruns the end of the program 00093 if(addr+sizeof(T)<size_) 00094 { 00095 // if not then its simple 00096 // copy the data to the address 00097 *reinterpret_cast<T*>( &( (*this)[addr] ) ) = val; 00098 } else 00099 { 00100 00101 // otherwise we need to write part of the data 00102 // to the end of the program and part of it 00103 // to the beginning 00104 00105 // calculate the number of bytes of data before the split 00106 uInt split = size_-addr; 00107 00108 // copy the first part of the data into the temporary 00109 // variable 00110 memcpy(&((*this)[addr]), &val, split); 00111 00112 // copy the second part of the data into the temporary 00113 // variable 00114 memcpy(&((*this)[0]), reinterpret_cast<char*>((&val))+split, sizeof(T)-split); 00115 } 00116 } 00117 00118 protected: 00119 private: 00120 }; 00121 00122 00123 00124 #endif 00125

Generated on Sun Mar 6 22:12:28 2005 for virtualMachine03 by doxygen 1.3.7