1
#include "kdlockedsharedmemorypointer.h"
3
#if QT_VERSION >= 0x040400 || defined( DOXYGEN_RUN )
4
#ifndef QT_NO_SHAREDMEMORY
9
using namespace kdtools;
11
KDLockedSharedMemoryPointerBase::KDLockedSharedMemoryPointerBase( QSharedMemory * m )
18
KDLockedSharedMemoryPointerBase::KDLockedSharedMemoryPointerBase( QSharedMemory & m )
25
KDLockedSharedMemoryPointerBase::~KDLockedSharedMemoryPointerBase() {}
27
void * KDLockedSharedMemoryPointerBase::get() {
28
return mem ? mem->data() : 0 ;
31
const void * KDLockedSharedMemoryPointerBase::get() const {
32
return mem ? mem->data() : 0 ;
35
size_t KDLockedSharedMemoryPointerBase::byteSize() const {
36
return mem ? mem->size() : 0;
40
\class KDLockedSharedMemoryPointer
41
\ingroup core raii smartptr
42
\brief Locking pointer for Qt shared memory segments
45
(The exception safety of this class has not been evaluated yet.)
47
KDLockedSharedMemoryPointer is a smart immutable pointer, which gives convenient and safe access to a QSharedMemory data segment.
48
The content of a KDLockedSharedMemoryPointer cannot be changed during it's lifetime.
50
You can use this class like a normal pointer to the shared memory segment and be sure it's locked while accessing it.
51
\note You can only put simple types/structs/classes into it. structs and classes shall not contain any other pointers. See the
52
documentation of QSharedMemory for details.
56
\fn KDLockedSharedMemoryPointer::KDLockedSharedMemoryPointer( QSharedMemory * mem )
58
Constructor. Constructs a KDLockedSharedMemory pointer which points to the data segment of \a mem.
59
The constructor locks \a mem. If the memory segment is already locked by another process, this constructor
60
blocks until the lock is released.
62
\post data() == mem->data() and the memory segment has been locked
66
\fn KDLockedSharedMemoryPointer::KDLockedSharedMemoryPointer( QSharedMemory & mem )
70
\post data() == mem.data() and the memory segment has been locked
74
\fn KDLockedSharedMemoryPointer::~KDLockedSharedMemoryPointer()
76
Destructor. Unlocks the shared memory segment.
78
\post The shared memory segment has been unlocked
82
\fn T * KDLockedSharedMemoryPointer::get()
84
\returns a pointer to the contained object.
88
\fn const T * KDLockedSharedMemoryPointer::get() const
90
\returns a const pointer to the contained object
95
\fn T * KDLockedSharedMemoryPointer::data()
97
Equivalent to get(), provided for consistency with Qt naming conventions.
101
\fn const T * KDLockedSharedMemoryPointer::data() const
107
\fn T & KDLockedSharedMemoryPointer::operator*()
109
Dereference operator. Returns \link get() *get()\endlink.
113
\fn const T & KDLockedSharedMemoryPointer::operator*() const
115
Dereference operator. Returns \link get() *get()\endlink.
120
\fn T * KDLockedSharedMemoryPointer::operator->()
122
Member-by-pointer operator. Returns get().
126
\fn const T * KDLockedSharedMemoryPointer::operator->() const
128
Member-by-pointer operator. Returns get().
133
\class KDLockedSharedMemoryArray
134
\ingroup core raii smartptr
135
\brief Locking array pointer to Qt shared memory segments
138
(The exception safety of this class has not been evaluated yet.)
140
KDLockedSharedMemoryArray is a smart immutable pointer, which gives convenient and safe access to array data stored in a QSharedMemory
142
The content of a KDLockedSharedMemoryArray cannot be changed during it's lifetime.
144
You can use this class like a normal pointer to the shared memory segment and be sure it's locked while accessing it.
145
\note You can only put arrays of simple types/structs/classes into it. structs and classes shall not contain any other pointers. See the
146
documentation of QSharedMemory for details.
148
\sa KDLockedSharedMemoryPointer
152
\fn KDLockedSharedMemoryArray::KDLockedSharedMemoryArray( QSharedMemory* mem )
153
Constructor. Constructs a KDLockedSharedMemoryArray which points to the data segment of \a mem. The constructor locks \a mem. If the memory
154
segment is already locked by another process, this constructor blocks until the lock is release.
156
\post get() == mem->data() and the memory segment has been locked
160
\fn KDLockedSharedMemoryArray::KDLockedSharedMemoryArray( QSharedMemory& mem )
163
\post get() == mem->data() and the memory segment has been locked
168
\typedef KDLockedSharedMemoryArray::size_type
169
Typedef for std::size_t. Provided for STL compatibility.
173
\typedef KDLockedSharedMemoryArray::difference_type
174
Typedef for std::ptrdiff_t. Provided for STL compatibility.
178
\typedef KDLockedSharedMemoryArray::iterator
179
Typedef for T*. Provided for STL compatibility.
184
\typedef KDLockedSharedMemoryArray::const_iterator
185
Typedef for const T*. Provided for STL compatibility.
190
\typedef KDLockedSharedMemoryArray::reverse_iterator
191
Typedef for std::reverse_iterator< \link KDLockedSharedMemoryArray::iterator iterator\endlink >. Provided for STL compatibility.
196
\typedef KDLockedSharedMemoryArray::const_reverse_iterator
197
Typedef for std::reverse_iterator< \link KDLockedSharedMemoryArray::const_iterator const_iterator\endlink >. Provided for STL compatibility.
202
\fn KDLockedSharedMemoryArray::iterator KDLockedSharedMemoryArray::begin()
203
Returns an \link KDLockedSharedMemoryArray::iterator iterator\endlink pointing to the first item of the array.
208
\fn KDLockedSharedMemoryArray::const_iterator KDLockedSharedMemoryArray::begin() const
214
\fn KDLockedSharedMemoryArray::iterator KDLockedSharedMemoryArray::end()
215
Returns an \link KDLockedSharedMemoryArray::iterator iterator\endlink pointing to the item after the last item of the array.
220
\fn KDLockedSharedMemoryArray::const_iterator KDLockedSharedMemoryArray::end() const
226
\fn KDLockedSharedMemoryArray::reverse_iterator KDLockedSharedMemoryArray::rbegin()
227
Returns an \link KDLockedSharedMemoryArray::reverse_iterator reverse_iterator\endlink pointing to the item after the last item of the array.
232
\fn KDLockedSharedMemoryArray::const_reverse_iterator KDLockedSharedMemoryArray::rbegin() const
238
\fn KDLockedSharedMemoryArray::reverse_iterator KDLockedSharedMemoryArray::rend()
239
Returns an \link KDLockedSharedMemoryArray::reverse_iterator reverse_iterator\endlink pointing to the first item of the array.
244
\fn KDLockedSharedMemoryArray::const_reverse_iterator KDLockedSharedMemoryArray::rend() const
250
\fn KDLockedSharedMemoryArray::size_type KDLockedSharedMemoryArray::size() const
251
Returns the size of this array. The size is calculated from the storage size of T and
252
the size of the shared memory segment.
257
\fn T& KDLockedSharedMemoryArray::operator[]( difference_type n )
258
Array access operator. Returns a reference to the item at index position \a n.
262
\fn const T& KDLockedSharedMemoryArray::operator[]( difference_type n ) const
267
\fn T& KDLockedSharedMemoryArray::front()
268
Returns a reference to the first item in the array. This is the same as operator[](0).
272
\fn const T& KDLockedSharedMemoryArray::front() const
277
\fn T& KDLockedSharedMemoryArray::back()
278
Returns a reference to the last item in the array. This is the same as operator[](size()-1).
283
\fn const T& KDLockedSharedMemoryArray::back() const
289
#ifdef eKDTOOLSCORE_UNITTESTS
291
#include <KDUnitTest/Test>
300
TestStruct( uint nn = 0 )
313
bool operator==( const TestStruct& lhs, const TestStruct& rhs )
315
return lhs.n == rhs.n && lhs.f == rhs.f && lhs.c == rhs.c && lhs.b == rhs.b;
318
class TestThread : public QThread
321
TestThread( const QString& key )
332
kdtools::KDLockedSharedMemoryPointer< TestStruct > p( &mem );
347
bool isConst( TestStruct* )
352
bool isConst( const TestStruct* )
359
KDAB_UNITTEST_SIMPLE( KDLockedSharedMemoryPointer, "kdcoretools" ) {
361
const QString key = QUuid::createUuid();
362
QSharedMemory mem( key );
363
const bool created = mem.create( sizeof( TestStruct ) );
364
assertTrue( created );
366
return; // don't execute tests if shm coulnd't be created
368
// On Windows, shared mem is only available in increments of page
369
// size (4k), so don't fail if the segment is larger:
370
const unsigned long mem_size = mem.size();
371
assertGreaterOrEqual( mem_size, sizeof( TestStruct ) );
374
kdtools::KDLockedSharedMemoryPointer< TestStruct > p( &mem );
377
assertEqual( p->n, 0u );
378
assertEqual( p->f, 0.0 );
379
assertEqual( p->c, '\0' );
384
TestThread thread( key );
385
assertEqual( thread.mem.key().toStdString(), key.toStdString() );
386
assertEqual( static_cast< unsigned long >( thread.mem.size() ), mem_size );
389
assertTrue( thread.isRunning() );
391
assertTrue( thread.isRunning() );
394
kdtools::KDLockedSharedMemoryPointer< TestStruct > p( &mem );
399
assertFalse( thread.isRunning() );
403
kdtools::KDLockedSharedMemoryPointer< TestStruct > p( &mem );
404
assertEqual( p->n, 5u );
405
assertEqual( p->f, 3.14 );
406
assertEqual( p->c, 'A' );
411
kdtools::KDLockedSharedMemoryPointer< TestStruct > p( mem );
412
assertEqual( mem.data(), p.get() );
413
assertEqual( p.get(), p.operator->() );
414
assertEqual( p.get(), &(*p) );
415
assertEqual( p.get(), p.data() );
416
assertFalse( isConst( p.get() ) );
420
const kdtools::KDLockedSharedMemoryPointer< TestStruct > p( &mem );
421
assertEqual( mem.data(), p.get() );
422
assertEqual( p.get(), p.operator->() );
423
assertEqual( p.get(), &(*p) );
424
assertEqual( p.get(), p.data() );
425
assertTrue( isConst( p.get() ) );
429
QSharedMemory mem2( key + key );
430
const bool created2 = mem2.create( 16 * sizeof( TestStruct ) );
431
assertTrue( created2 );
433
return; // don't execute tests if shm coulnd't be created
435
kdtools::KDLockedSharedMemoryArray<TestStruct> a( mem2 );
437
assertEqual( a.get(), mem2.data() );
438
assertEqual( &a[0], a.get() );
441
assertTrue( a[0] == a[1] );
447
assertFalse( a[0] == a[1] );
448
assertEqual( a.front().n, ts.n );
449
assertEqual( a[0].f, ts.f );
451
assertEqual( a.front().n, 10u );
453
assertEqual( ts.n, 10u );
455
std::vector< TestStruct > v;
456
for( uint i = 0; i < a.size(); ++i )
457
v.push_back( TestStruct( i ) );
459
std::copy( v.begin(), v.end(), a.begin() );
460
for( uint i = 0; i < a.size(); ++i )
461
assertEqual( a[ i ].n, i );
462
assertEqual( a.front().n, 0u );
463
assertEqual( a.back().n, a.size() - 1 );
465
std::copy( v.begin(), v.end(), a.rbegin() );
466
for( uint i = 0; i < a.size(); ++i )
467
assertEqual( a[ i ].n, a.size() - 1 - i );
468
assertEqual( a.front().n, a.size() - 1 );
469
assertEqual( a.back().n, 0u );
473
#endif // KDTOOLSCORE_UNITTESTS
474
#endif // QT_NO_SHAREDMEMORY
475
#endif // QT_VERSION >= 0x040400 || defined( DOXYGEN_RUN )