Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/QtCore/qtipccommon.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Copyright (C) 2022 Intel Corporation.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 
0004 #ifndef QNATIVEIPCKEY_H
0005 #define QNATIVEIPCKEY_H
0006 
0007 #include <QtCore/qglobal.h>
0008 #include <QtCore/qtcore-config.h>
0009 
0010 #if QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore)
0011 #  include <QtCore/qstring.h>
0012 #  include <QtCore/qobjectdefs.h>
0013 
0014 QT_BEGIN_NAMESPACE
0015 
0016 class QNativeIpcKeyPrivate;
0017 class QNativeIpcKey
0018 {
0019     Q_GADGET_EXPORT(Q_CORE_EXPORT)
0020 public:
0021     enum class Type : quint16 {
0022         // 0 is reserved for the invalid type
0023         // keep 1 through 0xff free, except for SystemV
0024         SystemV = 0x51,         // 'Q'
0025 
0026         PosixRealtime = 0x100,
0027         Windows,
0028     };
0029     Q_ENUM(Type)
0030 
0031     static constexpr Type DefaultTypeForOs =
0032 #ifdef Q_OS_WIN
0033             Type::Windows
0034 #else
0035             Type::PosixRealtime
0036 #endif
0037             ;
0038     static Type legacyDefaultTypeForOs() noexcept;
0039 
0040     constexpr QNativeIpcKey() noexcept = default;
0041 
0042     explicit constexpr QNativeIpcKey(Type type) noexcept
0043         : typeAndFlags{type}
0044     {
0045     }
0046 
0047     Q_IMPLICIT QNativeIpcKey(const QString &k, Type type = DefaultTypeForOs)
0048         : key(k), typeAndFlags{type}
0049     {
0050     }
0051 
0052     QNativeIpcKey(const QNativeIpcKey &other)
0053         : d(other.d), key(other.key), typeAndFlags(other.typeAndFlags)
0054     {
0055         if (isSlowPath())
0056             copy_internal(other);
0057     }
0058 
0059     QNativeIpcKey(QNativeIpcKey &&other) noexcept
0060         : d(std::exchange(other.d, nullptr)), key(std::move(other.key)),
0061           typeAndFlags(std::move(other.typeAndFlags))
0062     {
0063         if (isSlowPath())
0064             move_internal(std::move(other));
0065     }
0066 
0067     ~QNativeIpcKey()
0068     {
0069         if (isSlowPath())
0070             destroy_internal();
0071     }
0072 
0073     QNativeIpcKey &operator=(const QNativeIpcKey &other)
0074     {
0075         typeAndFlags = other.typeAndFlags;
0076         key = other.key;
0077         if (isSlowPath() || other.isSlowPath())
0078             return assign_internal(other);
0079         Q_ASSERT(!d);
0080         return *this;
0081     }
0082 
0083     QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QNativeIpcKey)
0084     void swap(QNativeIpcKey &other) noexcept
0085     {
0086         std::swap(d, other.d);
0087         key.swap(other.key);
0088         typeAndFlags.swap(other.typeAndFlags);
0089     }
0090 
0091     bool isEmpty() const noexcept
0092     {
0093         return key.isEmpty();
0094     }
0095 
0096     bool isValid() const noexcept
0097     {
0098         return type() != Type{};
0099     }
0100 
0101     constexpr Type type() const noexcept
0102     {
0103         return typeAndFlags.type;
0104     }
0105 
0106     constexpr void setType(Type type)
0107     {
0108         if (isSlowPath())
0109             return setType_internal(type);
0110         typeAndFlags.type = type;
0111     }
0112 
0113     QString nativeKey() const noexcept
0114     {
0115         return key;
0116     }
0117     void setNativeKey(const QString &newKey)
0118     {
0119         key = newKey;
0120         if (isSlowPath())
0121             setNativeKey_internal(newKey);
0122     }
0123 
0124     Q_CORE_EXPORT QString toString() const;
0125     Q_CORE_EXPORT static QNativeIpcKey fromString(const QString &string);
0126 
0127 private:
0128     struct TypeAndFlags {
0129         Type type = DefaultTypeForOs;
0130         quint16 reserved1 = {};
0131         quint32 reserved2 = {};
0132 
0133         void swap(TypeAndFlags &other) noexcept
0134         {
0135             std::swap(type, other.type);
0136             std::swap(reserved1, other.reserved1);
0137             std::swap(reserved2, other.reserved2);
0138         }
0139 
0140         friend constexpr bool operator==(const TypeAndFlags &lhs, const TypeAndFlags &rhs) noexcept
0141         {
0142             return lhs.type == rhs.type &&
0143                     lhs.reserved1 == rhs.reserved1 &&
0144                     lhs.reserved2 == rhs.reserved2;
0145         }
0146     };
0147 
0148     QNativeIpcKeyPrivate *d = nullptr;
0149     QString key;
0150     TypeAndFlags typeAndFlags;
0151 
0152     friend class QNativeIpcKeyPrivate;
0153     constexpr bool isSlowPath() const noexcept
0154     { return Q_UNLIKELY(d); }
0155 
0156     friend Q_CORE_EXPORT size_t qHash(const QNativeIpcKey &ipcKey, size_t seed) noexcept;
0157     friend size_t qHash(const QNativeIpcKey &ipcKey) noexcept
0158     { return qHash(ipcKey, 0); }
0159 
0160     friend bool operator==(const QNativeIpcKey &lhs, const QNativeIpcKey &rhs) noexcept
0161     {
0162         if (!(lhs.typeAndFlags == rhs.typeAndFlags))
0163             return false;
0164         if (lhs.key != rhs.key)
0165             return false;
0166         if (lhs.d == rhs.d)
0167             return true;
0168         return compare_internal(lhs, rhs) == 0;
0169     }
0170     friend bool operator!=(const QNativeIpcKey &lhs, const QNativeIpcKey &rhs) noexcept
0171     {
0172         return !(lhs == rhs);
0173     }
0174 
0175     Q_CORE_EXPORT void copy_internal(const QNativeIpcKey &other);
0176     Q_CORE_EXPORT void move_internal(QNativeIpcKey &&other) noexcept;
0177     Q_CORE_EXPORT QNativeIpcKey &assign_internal(const QNativeIpcKey &other);
0178     Q_CORE_EXPORT void destroy_internal() noexcept;
0179     Q_CORE_EXPORT void setType_internal(Type);
0180     Q_CORE_EXPORT void setNativeKey_internal(const QString &);
0181     Q_DECL_PURE_FUNCTION Q_CORE_EXPORT static int
0182     compare_internal(const QNativeIpcKey &lhs, const QNativeIpcKey &rhs) noexcept;
0183 
0184 #ifdef Q_OS_DARWIN
0185     Q_DECL_CONST_FUNCTION Q_CORE_EXPORT static Type defaultTypeForOs_internal() noexcept;
0186 #endif
0187 };
0188 
0189 // not a shared type, exactly, but this works too
0190 Q_DECLARE_SHARED(QNativeIpcKey)
0191 
0192 inline auto QNativeIpcKey::legacyDefaultTypeForOs() noexcept -> Type
0193 {
0194 #if defined(Q_OS_WIN)
0195     return Type::Windows;
0196 #elif defined(QT_POSIX_IPC)
0197     return Type::PosixRealtime;
0198 #elif defined(Q_OS_DARWIN)
0199     return defaultTypeForOs_internal();
0200 #else
0201     return Type::SystemV;
0202 #endif
0203 }
0204 
0205 QT_END_NAMESPACE
0206 
0207 #endif // QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore)
0208 
0209 
0210 #endif // QNATIVEIPCKEY_H