Go to the documentation of this file.00001 #ifndef _PYTHONQTMISC_H
00002 #define _PYTHONQTMISC_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00043
00044
00045
00046 #include <QList>
00047
00048 #define PythonQtValueStorage_ADD_VALUE(store, type, value, ptr) \
00049 { type* item = (type*)store.nextValuePtr(); \
00050 *item = value; \
00051 ptr = (void*)item; \
00052 }
00053
00054 #define PythonQtValueStorage_ADD_VALUE_IF_NEEDED(alreadyAllocatedPtr,store, type, value, ptr) \
00055 { \
00056 type* item = (type*)(alreadyAllocatedPtr?alreadyAllocatedPtr:store.nextValuePtr()); \
00057 *item = value; \
00058 ptr = (void*)item; \
00059 }
00060
00062 class PythonQtValueStoragePosition {
00063
00064 public:
00065 PythonQtValueStoragePosition() { chunkIdx = 0; chunkOffset = 0; }
00066
00067 int chunkIdx;
00068 int chunkOffset;
00069
00070 };
00071
00073 template <typename T, int chunkEntries> class PythonQtValueStorage
00074 {
00075 public:
00076 PythonQtValueStorage() {
00077 _chunkIdx = 0;
00078 _chunkOffset = 0;
00079 _currentChunk = new T[chunkEntries];
00080 _chunks.append(_currentChunk);
00081 };
00082
00084 void clear() {
00085 T* chunk;
00086 foreach(chunk, _chunks) {
00087 delete[]chunk;
00088 }
00089 _chunks.clear();
00090 }
00091
00093 void reset() {
00094 _chunkIdx = 0;
00095 _chunkOffset = 0;
00096 _currentChunk = _chunks.at(0);
00097 }
00098
00100 void getPos(PythonQtValueStoragePosition & pos) {
00101 pos.chunkIdx = _chunkIdx;
00102 pos.chunkOffset = _chunkOffset;
00103 }
00104
00106 void setPos(const PythonQtValueStoragePosition& pos) {
00107 _chunkOffset = pos.chunkOffset;
00108 if (_chunkIdx != pos.chunkIdx) {
00109 _chunkIdx = pos.chunkIdx;
00110 _currentChunk = _chunks.at(_chunkIdx);
00111 }
00112 }
00113
00115 T* nextValuePtr() {
00116 if (_chunkOffset>=chunkEntries) {
00117 _chunkIdx++;
00118 if (_chunkIdx >= _chunks.size()) {
00119 T* newChunk = new T[chunkEntries];
00120 _chunks.append(newChunk);
00121 _currentChunk = newChunk;
00122 } else {
00123 _currentChunk = _chunks.at(_chunkIdx);
00124 }
00125 _chunkOffset = 0;
00126 }
00127 T* newEntry = _currentChunk + _chunkOffset;
00128 _chunkOffset++;
00129 return newEntry;
00130 };
00131
00132 private:
00133 QList<T*> _chunks;
00134
00135 int _chunkIdx;
00136 int _chunkOffset;
00137 T* _currentChunk;
00138
00139 };
00140
00141
00142 #endif