Simplify InfoHash, TrackerEntry class internals

This commit is contained in:
Chocobo1 2018-12-24 00:39:14 +08:00
parent 2a84345835
commit a5c53ff756
No known key found for this signature in database
GPG key ID: 210D9C873253A68C
4 changed files with 36 additions and 53 deletions

View file

@ -28,6 +28,7 @@
#include "infohash.h" #include "infohash.h"
#include <QByteArray>
#include <QHash> #include <QHash>
using namespace BitTorrent; using namespace BitTorrent;
@ -41,29 +42,25 @@ InfoHash::InfoHash(const libtorrent::sha1_hash &nativeHash)
: m_valid(true) : m_valid(true)
, m_nativeHash(nativeHash) , m_nativeHash(nativeHash)
{ {
char out[(libtorrent::sha1_hash::size * 2) + 1]; const QByteArray raw = QByteArray::fromRawData(nativeHash.data(), libtorrent::sha1_hash::size);
libtorrent::to_hex(reinterpret_cast<const char*>(&m_nativeHash[0]), libtorrent::sha1_hash::size, out); m_hashString = QString::fromLatin1(raw.toHex());
m_hashString = QString(out);
} }
InfoHash::InfoHash(const QString &hashString) InfoHash::InfoHash(const QString &hashString)
: m_valid(false) : m_valid(false)
, m_hashString(hashString)
{ {
QByteArray raw = m_hashString.toLatin1(); if (hashString.size() != (libtorrent::sha1_hash::size * 2))
if (raw.size() == 40) return;
m_valid = libtorrent::from_hex(raw.constData(), 40, reinterpret_cast<char*>(&m_nativeHash[0]));
const QByteArray raw = QByteArray::fromHex(hashString.toLatin1());
if (raw.size() != libtorrent::sha1_hash::size) // QByteArray::fromHex() will skip over invalid characters
return;
m_valid = true;
m_hashString = hashString;
m_nativeHash.assign(raw.constData());
} }
InfoHash::InfoHash(const InfoHash &other)
: m_valid(other.m_valid)
, m_nativeHash(other.m_nativeHash)
, m_hashString(other.m_hashString)
{
}
bool InfoHash::isValid() const bool InfoHash::isValid() const
{ {
return m_valid; return m_valid;
@ -74,25 +71,23 @@ InfoHash::operator libtorrent::sha1_hash() const
return m_nativeHash; return m_nativeHash;
} }
InfoHash::operator QString() const InfoHash::operator QString() const
{ {
return m_hashString; return m_hashString;
} }
bool BitTorrent::operator==(const InfoHash &left, const InfoHash &right)
bool InfoHash::operator==(const InfoHash &other) const
{ {
return (m_nativeHash == other.m_nativeHash); return (static_cast<libtorrent::sha1_hash>(left)
== static_cast<libtorrent::sha1_hash>(right));
} }
bool BitTorrent::operator!=(const InfoHash &left, const InfoHash &right)
bool InfoHash::operator!=(const InfoHash &other) const
{ {
return (m_nativeHash != other.m_nativeHash); return !(left == right);
} }
uint BitTorrent::qHash(const InfoHash &key, uint seed) uint BitTorrent::qHash(const InfoHash &key, const uint seed)
{ {
return qHash(static_cast<QString>(key), seed); return ::qHash(static_cast<QString>(key), seed);
} }

View file

@ -40,14 +40,12 @@ namespace BitTorrent
InfoHash(); InfoHash();
InfoHash(const libtorrent::sha1_hash &nativeHash); InfoHash(const libtorrent::sha1_hash &nativeHash);
InfoHash(const QString &hashString); InfoHash(const QString &hashString);
InfoHash(const InfoHash &other); InfoHash(const InfoHash &other) = default;
bool isValid() const; bool isValid() const;
operator libtorrent::sha1_hash() const; operator libtorrent::sha1_hash() const;
operator QString() const; operator QString() const;
bool operator==(const InfoHash &other) const;
bool operator!=(const InfoHash &other) const;
private: private:
bool m_valid; bool m_valid;
@ -55,6 +53,8 @@ namespace BitTorrent
QString m_hashString; QString m_hashString;
}; };
bool operator==(const InfoHash &left, const InfoHash &right);
bool operator!=(const InfoHash &left, const InfoHash &right);
uint qHash(const InfoHash &key, uint seed); uint qHash(const InfoHash &key, uint seed);
} }

View file

@ -29,14 +29,12 @@
#include "trackerentry.h" #include "trackerentry.h"
#include <QString> #include <QString>
#include <QUrl>
#include "base/utils/misc.h"
#include "base/utils/string.h"
using namespace BitTorrent; using namespace BitTorrent;
TrackerEntry::TrackerEntry(const QString &url) TrackerEntry::TrackerEntry(const QString &url)
: m_nativeEntry(libtorrent::announce_entry(url.toStdString())) : m_nativeEntry(url.toStdString())
{ {
} }
@ -45,11 +43,6 @@ TrackerEntry::TrackerEntry(const libtorrent::announce_entry &nativeEntry)
{ {
} }
TrackerEntry::TrackerEntry(const TrackerEntry &other)
: m_nativeEntry(other.m_nativeEntry)
{
}
QString TrackerEntry::url() const QString TrackerEntry::url() const
{ {
return QString::fromStdString(m_nativeEntry.url); return QString::fromStdString(m_nativeEntry.url);
@ -74,23 +67,17 @@ TrackerEntry::Status TrackerEntry::status() const
return NotWorking; return NotWorking;
} }
void TrackerEntry::setTier(int value) void TrackerEntry::setTier(const int value)
{ {
m_nativeEntry.tier = value; m_nativeEntry.tier = value;
} }
TrackerEntry &TrackerEntry::operator=(const TrackerEntry &other)
{
this->m_nativeEntry = other.m_nativeEntry;
return *this;
}
bool TrackerEntry::operator==(const TrackerEntry &other) const
{
return (QUrl(url()) == QUrl(other.url()));
}
libtorrent::announce_entry TrackerEntry::nativeEntry() const libtorrent::announce_entry TrackerEntry::nativeEntry() const
{ {
return m_nativeEntry; return m_nativeEntry;
} }
bool BitTorrent::operator==(const TrackerEntry &left, const TrackerEntry &right)
{
return (QUrl(left.url()) == QUrl(right.url()));
}

View file

@ -52,21 +52,22 @@ namespace BitTorrent
TrackerEntry(const QString &url); TrackerEntry(const QString &url);
TrackerEntry(const libtorrent::announce_entry &nativeEntry); TrackerEntry(const libtorrent::announce_entry &nativeEntry);
TrackerEntry(const TrackerEntry &other); TrackerEntry(const TrackerEntry &other) = default;
TrackerEntry &operator=(const TrackerEntry &other) = default;
QString url() const; QString url() const;
int tier() const;
Status status() const; Status status() const;
int tier() const;
void setTier(int value); void setTier(int value);
TrackerEntry &operator=(const TrackerEntry &other);
bool operator==(const TrackerEntry &other) const;
libtorrent::announce_entry nativeEntry() const; libtorrent::announce_entry nativeEntry() const;
private: private:
libtorrent::announce_entry m_nativeEntry; libtorrent::announce_entry m_nativeEntry;
}; };
bool operator==(const TrackerEntry &left, const TrackerEntry &right);
} }
#endif // BITTORRENT_TRACKERENTRY_H #endif // BITTORRENT_TRACKERENTRY_H