diff options
author | Alexey Rusakov <Kitsune-Ral@users.sf.net> | 2022-01-14 22:37:28 +0100 |
---|---|---|
committer | Alexey Rusakov <Kitsune-Ral@users.sf.net> | 2022-01-18 08:55:23 +0100 |
commit | aaa57e0e458435e23047fcb325872f0350171bad (patch) | |
tree | 017b5f0d923ec344fe3136e5eea34986d369e6be /lib/accountregistry.h | |
parent | aa2465bb7991876c3b5afb5afda54f43fefd9e0a (diff) | |
download | libquotient-aaa57e0e458435e23047fcb325872f0350171bad.tar.gz libquotient-aaa57e0e458435e23047fcb325872f0350171bad.zip |
AccountRegistry: derive from QVector and clean up
Notably, Quotient::AccountRegistry::instance() is now deprecated
in favour of Quotient::Accounts inline variable.
Diffstat (limited to 'lib/accountregistry.h')
-rw-r--r-- | lib/accountregistry.h | 49 |
1 files changed, 31 insertions, 18 deletions
diff --git a/lib/accountregistry.h b/lib/accountregistry.h index f7a864df..2f6dffdf 100644 --- a/lib/accountregistry.h +++ b/lib/accountregistry.h @@ -6,42 +6,55 @@ #include "quotient_export.h" -#include <QtCore/QObject> -#include <QtCore/QList> #include <QtCore/QAbstractListModel> namespace Quotient { class Connection; -class QUOTIENT_API AccountRegistry : public QAbstractListModel { +class QUOTIENT_API AccountRegistry : public QAbstractListModel, + private QVector<Connection*> { Q_OBJECT public: + using const_iterator = QVector::const_iterator; + using const_reference = QVector::const_reference; + enum EventRoles { - ConnectionRole = Qt::UserRole + 1, + AccountRole = Qt::UserRole + 1, + ConnectionRole = AccountRole }; - static AccountRegistry &instance() { - static AccountRegistry _instance; - return _instance; - } + [[deprecated("Use Accounts variable instead")]] // + static AccountRegistry& instance(); + + // Expose most of QVector's const-API but only provide add() and drop() + // for changing it. In theory other changing operations could be supported + // too; but then boilerplate begin/end*() calls has to be tucked into each + // and this class gives no guarantees on the order of entries, so why care. - const QVector<Connection*> accounts() const; + const QVector<Connection*>& accounts() const { return *this; } void add(Connection* a); void drop(Connection* a); + const_iterator begin() const { return QVector::begin(); } + const_iterator end() const { return QVector::end(); } + const_reference front() const { return QVector::front(); } + const_reference back() const { return QVector::back(); } bool isLoggedIn(const QString& userId) const; - bool isEmpty() const; - int count() const; - bool contains(Connection*) const; Connection* get(const QString& userId); - [[nodiscard]] QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - [[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override; + using QVector::isEmpty, QVector::empty; + using QVector::size, QVector::count, QVector::capacity; + using QVector::cbegin, QVector::cend, QVector::contains; + + // QAbstractItemModel interface implementation + [[nodiscard]] QVariant data(const QModelIndex& index, + int role) const override; + [[nodiscard]] int rowCount( + const QModelIndex& parent = QModelIndex()) const override; [[nodiscard]] QHash<int, QByteArray> roleNames() const override; +}; -private: - AccountRegistry(); +inline QUOTIENT_API AccountRegistry Accounts {}; - QVector<Connection *> m_accounts; -}; +inline AccountRegistry& AccountRegistry::instance() { return Accounts; } } |