SourceForge.net Logo
PythonQt
PythonQtMisc.h
Go to the documentation of this file.
1 #ifndef _PYTHONQTMISC_H
2 #define _PYTHONQTMISC_H
3 
4 /*
5  *
6  * Copyright (C) 2010 MeVis Medical Solutions AG 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 Medical Solutions AG, Universitaetsallee 29,
30  * 28359 Bremen, Germany or:
31  *
32  * http://www.mevis.de
33  *
34  */
35 
36 //----------------------------------------------------------------------------------
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 #define PythonQtValueStorage_ADD_VALUE_IF_NEEDED(alreadyAllocatedPtr,store, type, value, ptr) \
55 { \
56  type* item = (type*)(alreadyAllocatedPtr?alreadyAllocatedPtr:store.nextValuePtr()); \
57  *item = value; \
58  ptr = (void*)item; \
59 }
60 
63 
64 public:
66 
67  int chunkIdx;
69 
70 };
71 
73 template <typename T, int chunkEntries> class PythonQtValueStorage
74 {
75 public:
77  _chunkIdx = 0;
78  _chunkOffset = 0;
79  _currentChunk = new T[chunkEntries];
80  _chunks.append(_currentChunk);
81  };
82 
84  void clear() {
85  T* chunk;
86  Q_FOREACH(chunk, _chunks) {
87  delete[]chunk;
88  }
89  _chunks.clear();
90  }
91 
94  pos.chunkIdx = _chunkIdx;
96  }
97 
101  if (_chunkIdx != pos.chunkIdx) {
102  _chunkIdx = pos.chunkIdx;
104  }
105  }
106 
109  if (_chunkOffset>=chunkEntries) {
110  _chunkIdx++;
111  if (_chunkIdx >= _chunks.size()) {
112  T* newChunk = new T[chunkEntries];
113  _chunks.append(newChunk);
114  _currentChunk = newChunk;
115  } else {
117  }
118  _chunkOffset = 0;
119  }
120  T* newEntry = _currentChunk + _chunkOffset;
121  _chunkOffset++;
122  return newEntry;
123  };
124 
125 protected:
126  QList<T*> _chunks;
127 
131 
132 };
133 
135 template <typename T, int chunkEntries> class PythonQtValueStorageWithCleanup : public PythonQtValueStorage<T, chunkEntries>
136 {
137 public:
139  if (_chunkIdx > pos.chunkIdx) {
140  T* firstChunk = _chunks.at(pos.chunkIdx);
141  // clear region in first chunk
142  for (int i = pos.chunkOffset; i < chunkEntries; i++) {
143  firstChunk[i] = T();
144  }
145  for (int chunk = pos.chunkIdx + 1; chunk < _chunkIdx; chunk++) {
146  // clear the full chunks between the first and last chunk
147  T* fullChunk = _chunks.at(chunk);
148  for (int i = 0; i < chunkEntries; i++) {
149  fullChunk[i] = T();
150  }
151  }
152  // clear region in last chunk
153  T* lastChunk = _chunks.at(_chunkIdx);
154  for (int i = 0; i < _chunkOffset; i++) {
155  lastChunk[i] = T();
156  }
157  } else if (_chunkIdx == pos.chunkIdx) {
158  // clear the region in the last chunk only
159  T* lastChunk = _chunks.at(_chunkIdx);
160  for (int i = pos.chunkOffset; i<_chunkOffset; i++) {
161  lastChunk[i] = T();
162  }
163  }
164 
166  }
167 
168 private:
173 };
174 
175 #endif
void setPos(const PythonQtValueStoragePosition &pos)
Definition: PythonQtMisc.h:138
void clear()
clear all memory
Definition: PythonQtMisc.h:84
T * nextValuePtr()
add one default constructed value and return the pointer to it
Definition: PythonQtMisc.h:108
a helper class that stores basic C++ value types in chunks
Definition: PythonQtMisc.h:73
stores a position in the PythonQtValueStorage
Definition: PythonQtMisc.h:62
QList< T * > _chunks
Definition: PythonQtMisc.h:123
a helper class that stores basic C++ value types in chunks and clears the unused values on setPos() u...
Definition: PythonQtMisc.h:135
void setPos(const PythonQtValueStoragePosition &pos)
set the current position (without freeing memory, thus caching old entries for reuse) ...
Definition: PythonQtMisc.h:99
void getPos(PythonQtValueStoragePosition &pos)
get the current position to be restored with setPos
Definition: PythonQtMisc.h:93