BWAPI
|
00001 #pragma once 00002 00003 // Boundaries of a non typed memory block // 00004 // // 00006 00007 namespace Util 00008 { 00009 class MemoryFrame; 00010 } 00011 00012 #include <Util/Exceptions.h> 00013 00014 #ifndef __FUNCTION__ 00015 #define __FUNCTION__ "UNKNOWN FUNCTION" 00016 #endif 00017 00018 namespace Util 00019 { 00020 class MemoryFrame 00021 { 00022 public: 00023 MemoryFrame(void *base, unsigned int size); 00024 MemoryFrame(); 00025 ~MemoryFrame(); 00026 00027 void fill(unsigned char value); 00028 00029 MemoryFrame getSubFrame(int from, int size); 00030 MemoryFrame getSubFrameByLimits(int from, int to); 00031 MemoryFrame getFrameUpto(const MemoryFrame &upto); 00032 void *begin() const; 00033 void *end() const; 00034 unsigned int size() const; 00035 bool isInside(void *ptr) const; 00036 bool isEmpty() const; 00037 bool isMultipleOf(unsigned int bytes) const; 00038 template<typename T> 00039 bool isFitFor() const 00040 { 00041 return this->frameSize >= sizeof(T); 00042 } 00043 template<typename T> 00044 bool isMultipleOf() const 00045 { 00046 return this->isMultipleOf(sizeof(T)); 00047 } 00048 bool operator == (const MemoryFrame &); // compares the frame addresses 00049 bool compareBytes(const MemoryFrame &); 00050 00051 void skip(unsigned int bytes); 00052 template<typename T> 00053 void skip() 00054 { 00055 this->skip(sizeof(T)); 00056 } 00057 MemoryFrame read(int bytes); 00058 void write(const MemoryFrame &source); 00059 template<typename T> 00060 T& readAs() 00061 { 00062 if(!this->isFitFor<T>()) 00063 throw GeneralException(__FUNCTION__ ": memory too small"); 00064 T& retval = getAs<T>(); 00065 skip<T>(); 00066 return retval; 00067 } 00068 template<typename T> 00069 void readTo(T &destination) 00070 { 00071 if(!this->tryReadTo(destination)) 00072 { 00073 throw GeneralException(__FUNCTION__ ": memory too small"); 00074 } 00075 } 00076 template<typename T> 00077 bool tryReadTo(T &destination) throw() 00078 { 00079 if(!this->isFitFor<T>()) 00080 return false; 00081 destination = getAs<T>(); 00082 skip<T>(); 00083 return true; 00084 } 00085 template<typename T> 00086 bool writeAs(const T& storee) 00087 { 00088 if(!this->isFitFor<T>()) 00089 return false; 00090 this->getAs<T>() = storee; 00091 skip<T>(); 00092 return true; 00093 } 00094 00095 // interpreter functions 00096 template<typename T> 00097 T &getAs() 00098 { 00099 return *((T*)frameBase); 00100 } 00101 template<typename T> 00102 T &offsetAs(int offset) 00103 { 00104 return *((T*)((int)frameBase+offset)); 00105 } 00106 template<typename T> 00107 T *offset(int offset = 0) 00108 { 00109 return ((T*)((int)frameBase+offset)); 00110 } 00111 template<typename T> 00112 T *beginAs() 00113 { 00114 return (T*)frameBase; 00115 } 00116 template<typename T> 00117 T *endAs() // the array filling as much as possible 00118 { 00119 return (T*)((int)frameBase+(frameSize/sizeof(T))*sizeof(T)); 00120 } 00121 template<typename T> 00122 unsigned int sizeAs() 00123 { 00124 return frameSize/sizeof(T); 00125 } 00126 template<typename T> 00127 static MemoryFrame from(T &structure) 00128 { 00129 return MemoryFrame((void*)&structure, sizeof(T)); 00130 } 00131 00132 private: 00133 void *frameBase; 00134 unsigned int frameSize; 00135 00136 int _limit(int a, int low, int hi); 00137 }; 00138 }