Home

qtsharedmemory.h

This is the verbatim text of the qtsharedmemory.h include file. It is provided only for illustration; the copyright remains with Trolltech.


#ifndef QTSHAREDMEMORY_H
#define QTSHAREDMEMORY_H
#include <qstring.h>
#include <qmutex.h>
#if defined Q_OS_WIN
#include <qt_windows.h>
#endif

#if defined(Q_WS_WIN)
#  if !defined(QT_QTSHAREDMEMORY_EXPORT) && !defined(QT_QTSHAREDMEMORY_IMPORT)
#    define QT_QTSHAREDMEMORY_EXPORT
#  elif defined(QT_QTSHAREDMEMORY_IMPORT)
#    if defined(QT_QTSHAREDMEMORY_EXPORT)
#      undef QT_QTSHAREDMEMORY_EXPORT
#    endif
#    define QT_QTSHAREDMEMORY_EXPORT __declspec(dllimport)
#  elif defined(QT_QTSHAREDMEMORY_EXPORT)
#    undef QT_QTSHAREDMEMORY_EXPORT
#    define QT_QTSHAREDMEMORY_EXPORT __declspec(dllexport)
#  endif
#else
#  define QT_QTSHAREDMEMORY_EXPORT
#endif

class QT_QTSHAREDMEMORY_EXPORT QtSharedMemory
{
public:
    QtSharedMemory();
    QtSharedMemory(const QString &key);
    ~QtSharedMemory();

    enum Mode {
        ReadOnly,
        ReadWrite
    };

    void setKey(const QString &key);
    QString key() const;

    int handle() const;

    bool create(int size = 4096);
    bool exists() const;
    bool attach(Mode mode = ReadWrite);
    bool detach();

    enum DestroyMode {
        NoWait,
        ForceNoWait,
        Wait
    };

    bool destroy(DestroyMode = NoWait);

    int numAttachments() const;

    bool lock();
    void unlock();

    void *data() const;

    int size() const;
    bool isValid() const;

    enum Error {
        InvalidSize,
        KeyError,
        AlreadyExists,
        OutOfMemory,
        OutOfResources,
        NotFound,
        PermissionDenied,
        LockError,
        UnknownError
    };

    Error error() const;
    QString errorString() const;

private:
	void platformInit();
    bool initHandle(bool create = false) const;
    void closeHandle();

    mutable Error err;
    mutable QString errStr;

    void *sharedMemoryPtr;
    int siz;
    QString k;
    int shmId;
    bool needReallocate;
#if defined Q_OS_WIN
    mutable HANDLE sema;
#else
    mutable int sema;
#endif

    int lockRecursions;
#if defined Q_OS_WIN
    mutable HANDLE hand;
    mutable HANDLE handRefCnt;
    int *refCountPtr;
	QString intKey;
	QString intKeyRefCnt;
#else
    mutable int hand;
#endif

    QMutex *mutex;

    // disable copying
    QtSharedMemory(const QtSharedMemory &copy);
    QtSharedMemory &operator =(const QtSharedMemory &copy);
};

#endif

Copyright © 2003-2006 TrolltechTrademarks
Qt Solutions