![]() |
CopperSpice API
2.0.0
|
The QSharedMemory class provides access to a shared memory segment. More...
Public Types | |
enum | AccessMode |
enum | SharedMemoryError |
Public Methods | |
QSharedMemory (const QString &key, QObject *parent=nullptr) | |
QSharedMemory (QObject *parent=nullptr) | |
~QSharedMemory () | |
bool | attach (AccessMode mode=ReadWrite) |
const void * | constData () const |
bool | create (int size, AccessMode mode=ReadWrite) |
void * | data () |
const void * | data () const |
bool | detach () |
SharedMemoryError | error () const |
QString | errorString () const |
bool | isAttached () const |
QString | key () const |
bool | lock () |
QString | nativeKey () const |
void | setKey (const QString &key) |
void | setNativeKey (const QString &key) |
int | size () const |
bool | unlock () |
![]() | |
QObject (QObject *parent=nullptr) | |
~QObject () | |
bool | blockSignals (bool block) |
const QList< QObject * > & | children () const |
bool | connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
bool | connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
bool | disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const |
bool | disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const |
bool | disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const |
void | dumpObjectInfo () |
void | dumpObjectTree () |
QList< QString > | dynamicPropertyNames () const |
virtual bool | event (QEvent *event) |
virtual bool | eventFilter (QObject *watched, QEvent *event) |
template<typename T > | |
T | findChild (const QString &childName=QString ()) const |
template<class T > | |
QList< T > | findChildren (const QRegularExpression ®Exp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
template<class T > | |
QList< T > | findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
bool | inherits (const QString &className) const |
void | installEventFilter (QObject *filterObj) |
bool | isWidgetType () const |
bool | isWindowType () const |
void | killTimer (int id) |
const QMetaObject * | metaObject () const |
void | moveToThread (QThread *targetThread) |
QString | objectName () const |
QObject * | parent () const |
template<class T = QVariant> | |
T | property (const QString &name) const |
void | removeEventFilter (QObject *obj) |
void | setObjectName (const QString &name) |
void | setParent (QObject *parent) |
bool | setProperty (const QString &name, const QVariant &value) |
bool | signalsBlocked () const |
int | startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer) |
QThread * | thread () const |
Additional Inherited Members | |
![]() | |
void | destroyed (QObject *obj=nullptr) |
void | objectNameChanged (const QString &objectName) |
![]() | |
void | deleteLater () |
![]() | |
static bool | connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
static bool | connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ()) |
static bool | connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn > | |
static bool | connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection) |
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T > | |
static bool | connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection) |
static bool | disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod) |
static bool | disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod) |
static bool | disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod) |
static bool | disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t) |
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn > | |
static bool | disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...)) |
template<class Sender , class SignalClass , class... SignalArgs, class Receiver > | |
static bool | disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr) |
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T > | |
static bool | disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod) |
static QMetaObject & | staticMetaObject () |
static QString | tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >()) |
![]() | |
virtual void | childEvent (QChildEvent *event) |
virtual void | connectNotify (const QMetaMethod &signalMethod) const |
virtual void | customEvent (QEvent *event) |
virtual void | disconnectNotify (const QMetaMethod &signalMethod) const |
bool | isSignalConnected (const QMetaMethod &signalMethod) const |
int | receivers (const QString &signal) const |
QObject * | sender () const |
int | senderSignalIndex () const |
virtual void | timerEvent (QTimerEvent *event) |
![]() | |
objectName | |
![]() | |
T | qobject_cast (QObject *object) |
QObjectList | |
The QSharedMemory class provides access to a shared memory segment.
QSharedMemory provides access to a shared memory segment by multiple threads and processes. It also provides a way for a single thread or process to lock the memory for exclusive access.
When using this class be aware of the following platform differences:
Remember to lock the shared memory with lock() before reading from or writing to the shared memory, and remember to release the lock with unlock() after you are done.
Unlike QtSharedMemory, QSharedMemory automatically destroys the shared memory segment when the last instance of QSharedMemory is detached from the segment, and no references to the segment remain. Do not mix using QtSharedMemory and QSharedMemory. Port everything to QSharedMemory.
Constant | Value | Description |
---|---|---|
QSharedMemory::ReadOnly | 0 | The shared memory segment is read only. Writing to the shared memory segment is not allowed. An attempt to write to a shared memory segment created with ReadOnly causes the program to abort. |
QSharedMemory::ReadWrite | 1 | Reading and writing the shared memory segment are both allowed. |
Constant | Value | Description |
---|---|---|
QSharedMemory::NoError | 0 | No error occurred. |
QSharedMemory::PermissionDenied | 1 | The operation failed because the caller did not have the required permissions. |
QSharedMemory::InvalidSize | 2 | A create operation failed because the requested size was invalid. |
QSharedMemory::KeyError | 3 | The operation failed because of an invalid key. |
QSharedMemory::AlreadyExists | 4 | A create() operation failed because a shared memory segment with the specified key already existed. |
QSharedMemory::NotFound | 5 | An attach() failed because a shared memory segment with the specified key could not be found. |
QSharedMemory::LockError | 6 | The attempt to lock() the shared memory segment failed because create() or attach() failed and returned false, or because a system error occurred in QSystemSemaphore::acquire(). |
QSharedMemory::OutOfResources | 7 | A create() operation failed because there was not enough memory available to fill the request. |
QSharedMemory::UnknownError | 8 | Something else happened and it was bad. |
QSharedMemory::QSharedMemory | ( | QObject * | parent = nullptr | ) |
Constructs a shared memory object with the given parent. The shared memory object's key is not set by the constructor, so the shared memory object does not have an underlying shared memory segment attached. The key must be set with setKey() or setNativeKey() before create() or attach() can be used.
QSharedMemory::~QSharedMemory | ( | ) |
The destructor clears the key, which forces the shared memory object to detach from its underlying shared memory segment. If this shared memory object is the last one connected to the shared memory segment, the detach() operation destroys the shared memory segment.
bool QSharedMemory::attach | ( | AccessMode | mode = ReadWrite | ) |
Attempts to attach the process to the shared memory segment identified by the key that was passed to the constructor or to a call to setKey() or setNativeKey(). The access mode is ReadWrite by default. It can also be ReadOnly. Returns true if the attach operation is successful. If false is returned, call error() to determine which error occurred. After attaching the shared memory segment, a pointer to the shared memory can be obtained by calling data().
const void * QSharedMemory::constData | ( | ) | const |
bool QSharedMemory::create | ( | int | size, |
AccessMode | mode = ReadWrite |
||
) |
Creates a shared memory segment of size bytes with the key passed to the constructor, set with setKey() or set with setNativeKey(), then attaches to the new shared memory segment with the given access mode and returns true
. If a shared memory segment identified by the key already exists, the attach operation is not performed and false
is returned. When the return value is false
, call error() to determine which error occurred.
void * QSharedMemory::data | ( | ) |
const void * QSharedMemory::data | ( | ) | const |
Returns a const pointer to the contents of the shared memory segment, if one is attached.
bool QSharedMemory::detach | ( | ) |
Detaches the process from the shared memory segment. If this was the last process attached to the shared memory segment, then the shared memory segment is released by the system, i.e., the contents are destroyed. The function returns true if it detaches the shared memory segment. If it returns false, it usually means the segment either is not attached, or it is locked by another process.
SharedMemoryError QSharedMemory::error | ( | ) | const |
Returns a value indicating whether an error occurred, and, if so, which error it was.
QString QSharedMemory::errorString | ( | ) | const |
bool QSharedMemory::isAttached | ( | ) | const |
QString QSharedMemory::key | ( | ) | const |
Returns the key assigned with setKey() to this shared memory, or a null key if no key has been assigned, or if the segment is using a nativeKey(). The key is the identifier used by CopperSpice applications to identify the shared memory segment.
You can find the native, platform specific, key used by the operating system by calling nativeKey().
bool QSharedMemory::lock | ( | ) |
This is a semaphore that locks the shared memory segment for access by this process and returns true. If another process has locked the segment, this function blocks until the lock is released. Then it acquires the lock and returns true. If this function returns false, it means that you have ignored a false return from create() or attach(), that you have set the key with setNativeKey() or that QSystemSemaphore::acquire() failed due to an unknown system error.
QString QSharedMemory::nativeKey | ( | ) | const |
Returns the native, platform specific, key for this shared memory object. The native key is the identifier used by the operating system to identify the shared memory segment.
You can use the native key to access shared memory segments that have not been created by CopperSpice, or to grant shared memory access to non-CopperSpice applications.
void QSharedMemory::setKey | ( | const QString & | key | ) |
Sets the platform independent key for this shared memory object. If key is the same as the current key, the function returns without doing anything.
You can call key() to retrieve the platform independent key. Internally, QSharedMemory converts this key into a platform specific key. If you instead call nativeKey(), you will get the platform specific, converted key.
If the shared memory object is attached to an underlying shared memory segment, it will detach from it before setting the new key. This function does not do an attach().
void QSharedMemory::setNativeKey | ( | const QString & | key | ) |
Sets the native, platform specific, key for this shared memory object. If key is the same as the current native key, the function returns without doing anything. If all you want is to assign a key to a segment, you should call setKey() instead.
You can call nativeKey() to retrieve the native key. If a native key has been assigned, calling key() will return a null string.
If the shared memory object is attached to an underlying shared memory segment, it will detach from it before setting the new key. This function does not do an attach().
The application will not be portable if you set a native key.
int QSharedMemory::size | ( | ) | const |
bool QSharedMemory::unlock | ( | ) |
Releases the lock on the shared memory segment and returns true, if the lock is currently held by this process. If the segment is not locked, or if the lock is held by another process, nothing happens and false is returned.