File indexing completed on 2024-05-12 04:35:06
0001 /* This file is part of the TikZKit project. 0002 * 0003 * Copyright (C) 2015 Dominik Haumann <dhaumann@kde.org> 0004 * 0005 * This library is free software; you can redistribute it and/or modify 0006 * it under the terms of the GNU Library General Public License as published 0007 * by the Free Software Foundation, either version 2 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This library is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU Library General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU Library General Public License 0016 * along with this library; see the file COPYING.LIB. If not, see 0017 * <http://www.gnu.org/licenses/>. 0018 */ 0019 #ifndef TIKZ_CORE_UID_H 0020 #define TIKZ_CORE_UID_H 0021 0022 #include "tikz.h" 0023 #include "tikz_export.h" 0024 0025 #include <QHash> 0026 #include <QVariant> 0027 #include <QDebug> 0028 0029 namespace tikz { 0030 namespace core { 0031 0032 class Entity; 0033 class Document; 0034 0035 /** 0036 * The Uid class provides document-wide unique identifiers. 0037 * 0038 * The Uid is defined by an integer number that refers to an Entity of 0039 * the associated Document. 0040 * 0041 * @see Entity, Document 0042 */ 0043 class TIKZKITCORE_EXPORT Uid 0044 { 0045 public: 0046 /** 0047 * Default constructor. Creates an invalid Uid. 0048 */ 0049 explicit constexpr Uid() noexcept = default; 0050 0051 /** 0052 * Constructor with value and type. 0053 */ 0054 explicit constexpr Uid(qint64 id, Document * doc) noexcept 0055 : m_document(doc) 0056 , m_id(id) 0057 { 0058 } 0059 0060 /** 0061 * Constructor with value and type. 0062 */ 0063 explicit Uid(const QString & idStr, tikz::core::Document * doc) noexcept 0064 : m_document(doc) 0065 { 0066 bool ok = false; 0067 m_id = idStr.toLongLong(&ok); 0068 Q_ASSERT(ok); 0069 0070 if (! ok) { 0071 m_id = -1; 0072 } 0073 } 0074 0075 /** 0076 * Returns @e true, if id() >= 0 and document() is other than a null pointer, 0077 * otherwise returns @e false. 0078 */ 0079 inline constexpr bool isValid() const noexcept 0080 { 0081 return m_id >= 0 && m_document != nullptr; 0082 } 0083 0084 /** 0085 * Returns the Document this Uid belongs to. 0086 */ 0087 inline constexpr Document * document() const noexcept 0088 { 0089 return m_document; 0090 } 0091 0092 /** 0093 * Get the internal id as qint64. 0094 */ 0095 inline constexpr qint64 id() const noexcept 0096 { 0097 return m_id; 0098 } 0099 0100 /** 0101 * Returns the Entity type Uid refers to. 0102 */ 0103 EntityType entityType() const; 0104 0105 /** 0106 * Returns the Entity this Uid refers to. 0107 */ 0108 Entity * entity() const; 0109 0110 /** 0111 * Templated getter for accessing the entity this Uid refers to as a 0112 * specific type. 0113 */ 0114 template <typename T> 0115 T * entity() const 0116 { 0117 return qobject_cast<T*>(entity()); 0118 } 0119 0120 /** 0121 * Convert this Uid to a string of the form "id (type)" 0122 */ 0123 inline QString toString() const noexcept 0124 { 0125 return QString::number(m_id); 0126 } 0127 0128 // 0129 // operators 0130 // 0131 public: 0132 /** 0133 * Implicit conversion to qint64. 0134 */ 0135 constexpr inline operator qint64() const noexcept 0136 { 0137 return m_id; 0138 } 0139 0140 /** 0141 * QDebug support. 0142 */ 0143 inline friend QDebug operator<<(QDebug s, const tikz::core::Uid& uid) 0144 { 0145 s.nospace() << uid.toString(); 0146 return s.space(); 0147 } 0148 0149 operator QVariant() const 0150 { 0151 return QVariant::fromValue(*this); 0152 } 0153 0154 private: 0155 /** 0156 * The associated Document of this Uid. 0157 */ 0158 Document * m_document = nullptr; 0159 0160 /** 0161 * The value. 0162 */ 0163 qint64 m_id = -1; 0164 }; 0165 0166 /** 0167 * Equality operator. 0168 * Return @e true, if the Uid @p lhs refers to the same Entity as Uid @p rhs. 0169 */ 0170 inline constexpr bool operator==(const Uid & lhs, const Uid & rhs) noexcept 0171 { 0172 return lhs.isValid() 0173 && rhs.isValid() 0174 && (lhs.id() == rhs.id()) 0175 && (lhs.document() == rhs.document()); 0176 } 0177 0178 /** 0179 * Inequality operator. 0180 * Return @e true, if the Uid @p lhs refers to a different Entity than Uid @p rhs. 0181 */ 0182 inline constexpr bool operator!=(const Uid & lhs, const Uid & rhs) noexcept 0183 { 0184 return ! (lhs == rhs); 0185 } 0186 0187 } //namespace core 0188 } //namespace tikz 0189 0190 namespace QTest 0191 { 0192 // forward declaration of template in qtestcase.h 0193 template<typename T> char* toString(const T&); 0194 0195 template<> 0196 TIKZKITCORE_EXPORT char *toString(const tikz::core::Uid & uid); 0197 } 0198 0199 /** 0200 * Declare as movable, since the members of Uid are primitive types. 0201 */ 0202 Q_DECLARE_TYPEINFO(tikz::core::Uid, Q_MOVABLE_TYPE); 0203 Q_DECLARE_METATYPE(tikz::core::Uid) 0204 0205 /** 0206 * QHash function. 0207 */ 0208 inline uint qHash(const tikz::core::Uid & uid, uint seed = 0) noexcept 0209 { 0210 return qHash(uid.id(), seed); 0211 } 0212 0213 #endif // TIKZ_CORE_UID_H 0214 0215 // kate: indent-width 4; replace-tabs on;