00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __SP_DIMENSION_UNIVERSALBUFFER_H__
00009 #define __SP_DIMENSION_UNIVERSALBUFFER_H__
00010
00011
00012 #include "Base/spStandard.hpp"
00013
00014 #include <vector>
00015 #include <cstring>
00016
00017
00018 namespace sp
00019 {
00020 namespace dim
00021 {
00022
00023
00029 class UniversalBuffer
00030 {
00031
00032 public:
00033
00034 UniversalBuffer() :
00035 Stride_(1)
00036 {
00037 }
00038 UniversalBuffer(const UniversalBuffer &Other) :
00039 Stride_(Other.Stride_),
00040 Buffer_(Other.Buffer_)
00041 {
00042 }
00043 ~UniversalBuffer()
00044 {
00045 }
00046
00048 inline UniversalBuffer& operator = (const UniversalBuffer &Other)
00049 {
00050 Stride_ = Other.Stride_;
00051 Buffer_ = Other.Buffer_;
00052 return *this;
00053 }
00054
00060 inline bool setStride(u32 Stride)
00061 {
00062 if (Stride > 0)
00063 {
00064 Stride_ = Stride;
00065 return true;
00066 }
00067 return false;
00068 }
00069 inline u32 getStride() const
00070 {
00071 return Stride_;
00072 }
00073
00079 template <typename T> inline void set(u32 Offset, const T &Value)
00080 {
00081 if (Offset + sizeof(T) <= Buffer_.size())
00082 memcpy(&Buffer_[Offset], &Value, sizeof(T));
00083 }
00084 template <typename T> inline T get(u32 Offset) const
00085 {
00086 if (Offset + sizeof(T) <= Buffer_.size())
00087 return *((T*)&Buffer_[Offset]);
00088 return T(0);
00089 }
00090
00097 template <typename T> inline void set(u32 Index, u32 Offset, const T &Value)
00098 {
00099 set<T>(Index * Stride_ + Offset, Value);
00100 }
00101 template <typename T> inline T get(u32 Index, u32 Offset) const
00102 {
00103 return get<T>(Index * Stride_ + Offset);
00104 }
00105
00113 template <typename T> inline void set(u32 Index, u32 Offset, u32 MaxSize, const T &Value)
00114 {
00115 const u32 Size = (sizeof(T) > MaxSize ? MaxSize : sizeof(T));
00116 Offset += Index * Stride_;
00117 if (Offset + Size <= Buffer_.size())
00118 memcpy(&Buffer_[Offset], &Value, Size);
00119 }
00120 template <typename T> inline T get(u32 Index, u32 Offset, u32 MaxSize) const
00121 {
00122 const u32 Size = (sizeof(T) > MaxSize ? MaxSize : sizeof(T));
00123 Offset += Index * Stride_;
00124 if (Offset + Size <= Buffer_.size())
00125 return *((T*)&Buffer_[Offset]);
00126 return T(0);
00127 }
00128
00135 inline void setBuffer(u32 Offset, const void* Buffer, u32 Size)
00136 {
00137 if (Offset + Size <= Buffer_.size())
00138 memcpy(&Buffer_[Offset], Buffer, Size);
00139 }
00140 inline void getBuffer(u32 Offset, void* Buffer, u32 Size) const
00141 {
00142 if (Offset + Size <= Buffer_.size())
00143 memcpy(Buffer, &Buffer_[Offset], Size);
00144 }
00145
00153 inline void setBuffer(u32 Index, u32 Offset, const void* Buffer, u32 Size)
00154 {
00155 setBuffer(Index * Stride_ + Offset, Buffer, Size);
00156 }
00164 inline void getBuffer(u32 Index, u32 Offset, void* Buffer, u32 Size) const
00165 {
00166 getBuffer(Index * Stride_ + Offset, Buffer, Size);
00167 }
00168
00170 template <typename T> inline void add(const T &Value)
00171 {
00172 const u32 Offset = Buffer_.size();
00173 Buffer_.resize(Buffer_.size() + sizeof(T));
00174 set(Offset, Value);
00175 }
00177 template <typename T> inline void remove(u32 Index, u32 Offset)
00178 {
00179 const u32 FinalOffset = Index * Stride_ + Offset;
00180 Buffer_.erase(Buffer_.begin() + FinalOffset, Buffer_.end() + FinalOffset + sizeof(T));
00181 }
00182
00184 inline void add(const UniversalBuffer &Other)
00185 {
00186 if (Stride_ == Other.Stride_)
00187 {
00188 const u32 PrevSize = Buffer_.size();
00189 Buffer_.resize(PrevSize + Other.Buffer_.size());
00190 memcpy(&Buffer_[PrevSize], &Other.Buffer_[0], Other.Buffer_.size());
00191 }
00192 }
00193
00194 inline void removeBuffer(u32 Offset, u32 Size)
00195 {
00196 Buffer_.erase(Buffer_.begin() + Offset, Buffer_.begin() + Offset + Size);
00197 }
00198 inline void removeBuffer(u32 Index, u32 Offset, u32 Size)
00199 {
00200 const u32 FinalOffset = Index * Stride_ + Offset;
00201 Buffer_.erase(Buffer_.begin() + FinalOffset, Buffer_.begin() + FinalOffset + Size);
00202 }
00203
00205 inline s8* getArray()
00206 {
00207 return Buffer_.size() ? &Buffer_[0] : 0;
00208 }
00210 inline const s8* getArray() const
00211 {
00212 return Buffer_.size() ? &Buffer_[0] : 0;
00213 }
00214
00216 inline s8* getArray(u32 Offset)
00217 {
00218 return Buffer_.size() ? &Buffer_[Offset] : 0;
00219 }
00221 inline const s8* getArray(u32 Offset) const
00222 {
00223 return Buffer_.size() ? &Buffer_[Offset] : 0;
00224 }
00225
00227 inline s8* getArray(u32 Index, u32 Offset)
00228 {
00229 return Buffer_.size() ? &Buffer_[Index * Stride_ + Offset] : 0;
00230 }
00232 inline const s8* getArray(u32 Index, u32 Offset) const
00233 {
00234 return Buffer_.size() ? &Buffer_[Index * Stride_ + Offset] : 0;
00235 }
00236
00238 inline void setSize(u32 Size)
00239 {
00240 Buffer_.resize(Size);
00241 }
00242
00244 inline u32 getSize() const
00245 {
00246 return Buffer_.size();
00247 }
00248
00250 inline void setCount(u32 Count)
00251 {
00252 Buffer_.resize(Count * Stride_);
00253 }
00254
00256 inline u32 getCount() const
00257 {
00258 return Buffer_.size() / Stride_;
00259 }
00260
00266 inline void fill(u32 Offset, u32 Size)
00267 {
00268 if (Buffer_.size() >= Offset + Size)
00269 memset(&Buffer_[Offset], 0, Size);
00270 }
00271
00273 inline void clear()
00274 {
00275 Buffer_.clear();
00276 }
00277
00279 inline bool empty() const
00280 {
00281 return Buffer_.empty();
00282 }
00283
00285 inline std::vector<s8>& getContainer()
00286 {
00287 return Buffer_;
00288 }
00290 inline const std::vector<s8>& getContainer() const
00291 {
00292 return Buffer_;
00293 }
00294
00295 private:
00296
00297
00298
00299 u32 Stride_;
00300 std::vector<s8> Buffer_;
00301
00302 };
00303
00304
00305 }
00306
00307 }
00308
00309
00310 #endif
00311
00312
00313
00314