PageRenderTime 31ms CodeModel.GetById 14ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/libtomahawk/thirdparty/kdsingleapplicationguard/kdlockedsharedmemorypointer.h

http://github.com/tomahawk-player/tomahawk
C++ Header | 115 lines | 81 code | 32 blank | 2 comment | 1 complexity | 6260c9be14a5cd101e1b9f98cac51fa9 MD5 | raw file
  1#ifndef __KDTOOLS__CORE__KDLOCKEDSHAREDMEMORYPOINTER_H__
  2#define __KDTOOLS__CORE__KDLOCKEDSHAREDMEMORYPOINTER_H__
  3
  4#include <QtCore/QtGlobal>
  5
  6#if QT_VERSION >= 0x040400 || defined( DOXYGEN_RUN )
  7#ifndef QT_NO_SHAREDMEMORY
  8
  9#include "kdsharedmemorylocker.h"
 10#include <QtCore/QSharedMemory>
 11
 12#include <cassert>
 13
 14#ifndef DOXYGEN_RUN
 15namespace kdtools {
 16#endif
 17
 18class KDLockedSharedMemoryPointerBase {
 19protected:
 20    explicit KDLockedSharedMemoryPointerBase( QSharedMemory * mem );
 21    explicit KDLockedSharedMemoryPointerBase( QSharedMemory & mem );
 22    ~KDLockedSharedMemoryPointerBase();
 23
 24    // PENDING(marc) do we really want const propagation here? I
 25    // usually declare all my RAII objects const...
 26    void * get();
 27    const void * get() const;
 28
 29    KDAB_IMPLEMENT_SAFE_BOOL_OPERATOR( get() )
 30
 31    size_t byteSize() const;
 32
 33private:
 34    KDSharedMemoryLocker locker;
 35    QSharedMemory * const mem;
 36};
 37
 38template< typename T>
 39class MAKEINCLUDES_EXPORT KDLockedSharedMemoryPointer : KDLockedSharedMemoryPointerBase {
 40    KDAB_DISABLE_COPY( KDLockedSharedMemoryPointer );
 41public:
 42    explicit KDLockedSharedMemoryPointer( QSharedMemory * m )
 43        : KDLockedSharedMemoryPointerBase( m ) {}
 44    explicit KDLockedSharedMemoryPointer( QSharedMemory & m )
 45        : KDLockedSharedMemoryPointerBase( m ) {}
 46
 47    T * get() { return static_cast<T*>( KDLockedSharedMemoryPointerBase::get() ); }
 48    const T * get() const { return static_cast<const T*>( KDLockedSharedMemoryPointerBase::get() ); }
 49
 50    T * data() { return static_cast<T*>( get() ); }
 51    const T * data() const { return static_cast<const T*>( get() ); }
 52
 53    T & operator*() { assert( get() ); return *get(); }
 54    const T & operator*() const { assert( get() ); return *get(); }
 55
 56    T * operator->() { return get(); }
 57    const T * operator->() const { return get(); }
 58
 59    KDAB_USING_SAFE_BOOL_OPERATOR( KDLockedSharedMemoryPointerBase )
 60};
 61
 62template <typename T>
 63class MAKEINCLUDES_EXPORT KDLockedSharedMemoryArray : KDLockedSharedMemoryPointerBase {
 64    KDAB_DISABLE_COPY( KDLockedSharedMemoryArray );
 65public:
 66    explicit KDLockedSharedMemoryArray( QSharedMemory * m )
 67        : KDLockedSharedMemoryPointerBase( m ) {}
 68    explicit KDLockedSharedMemoryArray( QSharedMemory & m )
 69        : KDLockedSharedMemoryPointerBase( m ) {}
 70
 71    typedef std::size_t size_type;
 72    typedef std::ptrdiff_t difference_type;
 73    typedef T* iterator;
 74    typedef const T* const_iterator;
 75    typedef std::reverse_iterator< const_iterator > const_reverse_iterator;
 76    typedef std::reverse_iterator< iterator > reverse_iterator;
 77    
 78    iterator begin() { return get(); }
 79    const_iterator begin() const { return get(); }
 80
 81    iterator end() { return begin() + size(); }
 82    const_iterator end() const { return begin() + size(); }
 83
 84    reverse_iterator rbegin() { return reverse_iterator( end() ); }
 85    const_reverse_iterator rbegin() const { return reverse_iterator( end() ); }
 86
 87    reverse_iterator rend() { return reverse_iterator( begin() ); }
 88    const_reverse_iterator rend() const { return const_reverse_iterator( begin() ); }
 89
 90    size_type size() const { return byteSize() / sizeof( T ); }
 91
 92    T * get() { return static_cast<T*>( KDLockedSharedMemoryPointerBase::get() ); }
 93    const T * get() const { return static_cast<const T*>( KDLockedSharedMemoryPointerBase::get() ); }
 94
 95    T & operator[]( difference_type n ) { assert( get() ); return *(get()+n); }
 96    const T & operator[]( difference_type n ) const { assert( get() ); return *(get()+n); }
 97
 98    T & front() { assert( get() ); return *get(); }
 99    const T & front() const { assert( get() ); return *get(); }
100
101    T & back() { assert( get() ); return *( get() + size() - 1 ); }
102    const T & back() const { assert( get() ); return *( get() + size() - 1 ); }
103
104    KDAB_USING_SAFE_BOOL_OPERATOR( KDLockedSharedMemoryPointerBase )
105};
106
107#ifndef DOXYGEN_RUN
108}
109#endif
110
111#endif /* QT_NO_SHAREDMEMORY */
112
113#endif /* QT_VERSION >= 0x040400 || defined( DOXYGEN_RUN ) */
114
115#endif /* __KDTOOLS__CORE__KDLOCKEDSHAREDMEMORYPOINTER_H__ */