1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QSHAREDMEMORY_H
5#define QSHAREDMEMORY_H
6
7#include <QtCore/qtipccommon.h>
8#ifndef QT_NO_QOBJECT
9# include <QtCore/qobject.h>
10#else
11# include <QtCore/qobjectdefs.h>
12# include <QtCore/qscopedpointer.h>
13# include <QtCore/qstring.h>
14#endif
15
16QT_BEGIN_NAMESPACE
17
18#if QT_CONFIG(sharedmemory)
19
20class QSharedMemoryPrivate;
21
22class Q_CORE_EXPORT QSharedMemory : public QObject
23{
24 Q_OBJECT
25 Q_DECLARE_PRIVATE(QSharedMemory)
26
27public:
28 enum AccessMode
29 {
30 ReadOnly,
31 ReadWrite
32 };
33 Q_ENUM(AccessMode)
34
35 enum SharedMemoryError
36 {
37 NoError,
38 PermissionDenied,
39 InvalidSize,
40 KeyError,
41 AlreadyExists,
42 NotFound,
43 LockError,
44 OutOfResources,
45 UnknownError
46 };
47 Q_ENUM(SharedMemoryError)
48
49 QSharedMemory(QObject *parent = nullptr);
50 QSharedMemory(const QNativeIpcKey &key, QObject *parent = nullptr);
51 ~QSharedMemory();
52
53#if QT_DEPRECATED_SINCE(6, 10)
54 QT_DEPRECATED_VERSION_X_6_10("Please refer to 'Native IPC Key' documentation")
55 QSharedMemory(const QString &key, QObject *parent = nullptr);
56 QT_DEPRECATED_VERSION_X_6_10("Please refer to 'Native IPC Key' documentation")
57 void setKey(const QString &key);
58 QT_DEPRECATED_VERSION_X_6_10("Please refer to 'Native IPC Key' documentation")
59 QString key() const;
60#endif
61
62 void setNativeKey(const QNativeIpcKey &key);
63 void setNativeKey(const QString &key, QNativeIpcKey::Type type = QNativeIpcKey::legacyDefaultTypeForOs())
64 { setNativeKey({ key, type }); }
65 QString nativeKey() const;
66 QNativeIpcKey nativeIpcKey() const;
67#if QT_CORE_REMOVED_SINCE(6, 5)
68 void setNativeKey(const QString &key);
69#endif
70
71 bool create(qsizetype size, AccessMode mode = ReadWrite);
72 qsizetype size() const;
73
74 bool attach(AccessMode mode = ReadWrite);
75 bool isAttached() const;
76 bool detach();
77
78 void *data();
79 const void* constData() const;
80 const void *data() const;
81
82#if QT_CONFIG(systemsemaphore)
83 bool lock();
84 bool unlock();
85#endif
86
87 SharedMemoryError error() const;
88 QString errorString() const;
89
90 static bool isKeyTypeSupported(QNativeIpcKey::Type type) Q_DECL_CONST_FUNCTION;
91 static QNativeIpcKey platformSafeKey(const QString &key,
92 QNativeIpcKey::Type type = QNativeIpcKey::DefaultTypeForOs);
93 static QNativeIpcKey legacyNativeKey(const QString &key,
94 QNativeIpcKey::Type type = QNativeIpcKey::legacyDefaultTypeForOs());
95
96private:
97 Q_DISABLE_COPY(QSharedMemory)
98};
99
100#endif // QT_CONFIG(sharedmemory)
101
102QT_END_NAMESPACE
103
104#endif // QSHAREDMEMORY_H
105

source code of qtbase/src/corelib/ipc/qsharedmemory.h