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