StaticBuffer.cpp
Go to the documentation of this file.00001 //*** StaticBuffer.cpp *** 00002 00003 #include "StaticBuffer.h" 00004 #include "Debug.h" 00005 #include "StandardLibrary.h" 00006 00007 00008 //*** Constructor *** 00009 00010 StaticBuffer::StaticBuffer(const DynamicBuffer& buffer): 00011 buffer_(static_cast<unsigned char*>(buffer.GetPointer())), 00012 size_(buffer.GetSize()), 00013 position_(0) 00014 { 00015 } 00016 00017 00018 //*** Constructor *** 00019 00020 StaticBuffer::StaticBuffer(const void* buffer, int size): 00021 buffer_(static_cast<const unsigned char*>(buffer)), 00022 size_(size), 00023 position_(0) 00024 { 00025 } 00026 00027 00028 //*** Destructor *** 00029 00030 StaticBuffer::~StaticBuffer() 00031 { 00032 } 00033 00034 00035 //*** GetSize *** 00036 00037 int StaticBuffer::GetSize() const 00038 { 00039 return size_; 00040 } 00041 00042 00043 //*** GetPosition *** 00044 00045 int StaticBuffer::GetPosition() const 00046 { 00047 return position_; 00048 } 00049 00050 00051 //*** SetPosition *** 00052 00053 void StaticBuffer::SetPosition(int position) const 00054 { 00055 position_=position; 00056 } 00057 00058 00059 //*** GetPointer *** 00060 00061 const void* StaticBuffer::GetPointer() const 00062 { 00063 return buffer_; 00064 } 00065 00066 00067 //*** READMACRO *** 00068 00069 // This macro defines the code that is to be executed for all of the overloaded Read methods 00070 // The code is identical for all the different Read methods, only the input parameters change, 00071 // so it makes more sense to use a macro rather than copy-paste. This macro is undefined as 00072 // soon as it is not needed anymore 00073 00074 #define READMACRO() \ 00075 /* Calculate the total number of bytes to be read */ \ 00076 int totalSize=sizeof(*value)*count; \ 00077 /* Make sure we're not trying to read more bytes than there is */ \ 00078 if (position_+totalSize>size_) \ 00079 { \ 00080 count=(size_-position_)/sizeof(*value); \ 00081 totalSize=sizeof(*value)*count; \ 00082 } \ 00083 /* Copy the actual bytes from the buffer */ \ 00084 MemCpy(value,buffer_+position_,totalSize); \ 00085 /* Update the current position */ \ 00086 position_+=totalSize; \ 00087 /* Return the number of elements written */ \ 00088 return count; \ 00089 00090 00091 //** Read methods */ 00092 00093 int StaticBuffer::Read(char* value, int count) const 00094 { 00095 READMACRO(); 00096 } 00097 00098 int StaticBuffer::Read(short* value, int count) const 00099 { 00100 READMACRO(); 00101 } 00102 00103 int StaticBuffer::Read(int* value, int count) const 00104 { 00105 READMACRO(); 00106 } 00107 00108 int StaticBuffer::Read(long* value, int count) const 00109 { 00110 READMACRO(); 00111 } 00112 00113 int StaticBuffer::Read(unsigned char* value, int count) const 00114 { 00115 READMACRO(); 00116 } 00117 00118 int StaticBuffer::Read(unsigned short* value, int count) const 00119 { 00120 READMACRO(); 00121 } 00122 00123 int StaticBuffer::Read(unsigned int* value, int count) const 00124 { 00125 READMACRO(); 00126 } 00127 00128 int StaticBuffer::Read(unsigned long* value, int count) const 00129 { 00130 READMACRO(); 00131 } 00132 00133 int StaticBuffer::Read(float* value, int count) const 00134 { 00135 READMACRO(); 00136 } 00137 00138 int StaticBuffer::Read(double* value, int count) const 00139 { 00140 READMACRO(); 00141 } 00142 00143 int StaticBuffer::Read(bool* value, int count) const 00144 { 00145 READMACRO(); 00146 } 00147 00148 // No need for the read macro anymore, so undefine it 00149 #undef READMACRO
Reproduction/republishing of any material on this site without permission is strictly prohibited.
