Path: blob/devel/ElmerGUI/PythonQt/src/PythonQtMisc.h
3206 views
#ifndef _PYTHONQTMISC_H1#define _PYTHONQTMISC_H23/*4*5* Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.6*7* This library is free software; you can redistribute it and/or8* modify it under the terms of the GNU Lesser General Public9* License as published by the Free Software Foundation; either10* version 2.1 of the License, or (at your option) any later version.11*12* This library is distributed in the hope that it will be useful,13* but WITHOUT ANY WARRANTY; without even the implied warranty of14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU15* Lesser General Public License for more details.16*17* Further, this software is distributed without any warranty that it is18* free of the rightful claim of any third person regarding infringement19* or the like. Any license provided herein, whether implied or20* otherwise, applies only to this software file. Patent licenses, if21* any, provided herein do not apply to combinations of this program with22* other software, or any other product whatsoever.23*24* You should have received a copy of the GNU Lesser General Public25* License along with this library; if not, write to the Free Software26* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA27*28* Contact information: MeVis Research GmbH, Universitaetsallee 29,29* 28359 Bremen, Germany or:30*31* http://www.mevis.de32*33*/3435//----------------------------------------------------------------------------------36/*!37// \file PythonQtMisc.h38// \author Florian Link39// \author Last changed by $Author: florian $40// \date 2006-0541*/42//----------------------------------------------------------------------------------434445#include <QList>4647#define PythonQtValueStorage_ADD_VALUE(store, type, value, ptr) \48{ type* item = (type*)store.nextValuePtr(); \49*item = value; \50ptr = (void*)item; \51}5253//! stores a position in the PythonQtValueStorage54class PythonQtValueStoragePosition {5556public:57PythonQtValueStoragePosition() { chunkIdx = 0; chunkOffset = 0; }5859int chunkIdx;60int chunkOffset;6162};6364//! a helper class that stores basic C++ value types in chunks65template <typename T, int chunkEntries> class PythonQtValueStorage66{67public:68PythonQtValueStorage() {69_chunkIdx = 0;70_chunkOffset = 0;71_currentChunk = new T[chunkEntries];72_chunks.append(_currentChunk);73};7475//! clear all memory76void clear() {77T* chunk;78foreach(chunk, _chunks) {79delete[]chunk;80}81}8283//! reset the storage to 0 (without freeing memory, thus caching old entries for reuse)84void reset() {85_chunkIdx = 0;86_chunkOffset = 0;87_currentChunk = _chunks.at(0);88}8990//! get the current position to be restored with setPos91void getPos(PythonQtValueStoragePosition & pos) {92pos.chunkIdx = _chunkIdx;93pos.chunkOffset = _chunkOffset;94}9596//! set the current position (without freeing memory, thus caching old entries for reuse)97void setPos(const PythonQtValueStoragePosition& pos) {98_chunkOffset = pos.chunkOffset;99if (_chunkIdx != pos.chunkIdx) {100_chunkIdx = pos.chunkIdx;101_currentChunk = _chunks.at(_chunkIdx);102}103}104105//! add one default constructed value and return the pointer to it106T* nextValuePtr() {107if (_chunkOffset>=chunkEntries) {108_chunkIdx++;109if (_chunkIdx >= _chunks.size()) {110T* newChunk = new T[chunkEntries];111_chunks.append(newChunk);112_currentChunk = newChunk;113} else {114_currentChunk = _chunks.at(_chunkIdx);115}116_chunkOffset = 0;117}118T* newEntry = _currentChunk + _chunkOffset;119_chunkOffset++;120return newEntry;121};122123private:124QList<T*> _chunks;125126int _chunkIdx;127int _chunkOffset;128T* _currentChunk;129130};131132133#endif134135136