1 #ifndef _PYTHONQTMISC_H 2 #define _PYTHONQTMISC_H 3 4 /* 5 * 6 * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved. 7 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public 10 * License as published by the Free Software Foundation; either 11 * version 2.1 of the License, or (at your option) any later version. 12 * 13 * This library is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * Further, this software is distributed without any warranty that it is 19 * free of the rightful claim of any third person regarding infringement 20 * or the like. Any license provided herein, whether implied or 21 * otherwise, applies only to this software file. Patent licenses, if 22 * any, provided herein do not apply to combinations of this program with 23 * other software, or any other product whatsoever. 24 * 25 * You should have received a copy of the GNU Lesser General Public 26 * License along with this library; if not, write to the Free Software 27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 28 * 29 * Contact information: MeVis Research GmbH, Universitaetsallee 29, 30 * 28359 Bremen, Germany or: 31 * 32 * http://www.mevis.de 33 * 34 */ 35 36 //---------------------------------------------------------------------------------- 37 /*! 38 // \file PythonQtMisc.h 39 // \author Florian Link 40 // \author Last changed by $Author: florian $ 41 // \date 2006-05 42 */ 43 //---------------------------------------------------------------------------------- 44 45 46 #include <QList> 47 48 #define PythonQtValueStorage_ADD_VALUE(store, type, value, ptr) \ 49 { type* item = (type*)store.nextValuePtr(); \ 50 *item = value; \ 51 ptr = (void*)item; \ 52 } 53 54 //! stores a position in the PythonQtValueStorage 55 class PythonQtValueStoragePosition { 56 57 public: PythonQtValueStoragePosition()58 PythonQtValueStoragePosition() { chunkIdx = 0; chunkOffset = 0; } 59 60 int chunkIdx; 61 int chunkOffset; 62 63 }; 64 65 //! a helper class that stores basic C++ value types in chunks 66 template <typename T, int chunkEntries> class PythonQtValueStorage 67 { 68 public: PythonQtValueStorage()69 PythonQtValueStorage() { 70 _chunkIdx = 0; 71 _chunkOffset = 0; 72 _currentChunk = new T[chunkEntries]; 73 _chunks.append(_currentChunk); 74 }; 75 76 //! clear all memory clear()77 void clear() { 78 T* chunk; 79 foreach(chunk, _chunks) { 80 delete[]chunk; 81 } 82 } 83 84 //! reset the storage to 0 (without freeing memory, thus caching old entries for reuse) reset()85 void reset() { 86 _chunkIdx = 0; 87 _chunkOffset = 0; 88 _currentChunk = _chunks.at(0); 89 } 90 91 //! get the current position to be restored with setPos getPos(PythonQtValueStoragePosition & pos)92 void getPos(PythonQtValueStoragePosition & pos) { 93 pos.chunkIdx = _chunkIdx; 94 pos.chunkOffset = _chunkOffset; 95 } 96 97 //! set the current position (without freeing memory, thus caching old entries for reuse) setPos(const PythonQtValueStoragePosition & pos)98 void setPos(const PythonQtValueStoragePosition& pos) { 99 _chunkOffset = pos.chunkOffset; 100 if (_chunkIdx != pos.chunkIdx) { 101 _chunkIdx = pos.chunkIdx; 102 _currentChunk = _chunks.at(_chunkIdx); 103 } 104 } 105 106 //! add one default constructed value and return the pointer to it nextValuePtr()107 T* nextValuePtr() { 108 if (_chunkOffset>=chunkEntries) { 109 _chunkIdx++; 110 if (_chunkIdx >= _chunks.size()) { 111 T* newChunk = new T[chunkEntries]; 112 _chunks.append(newChunk); 113 _currentChunk = newChunk; 114 } else { 115 _currentChunk = _chunks.at(_chunkIdx); 116 } 117 _chunkOffset = 0; 118 } 119 T* newEntry = _currentChunk + _chunkOffset; 120 _chunkOffset++; 121 return newEntry; 122 }; 123 124 private: 125 QList<T*> _chunks; 126 127 int _chunkIdx; 128 int _chunkOffset; 129 T* _currentChunk; 130 131 }; 132 133 134 #endif 135