Line data Source code
1 : #ifndef NEWSLIST_H
2 : #define NEWSLIST_H
3 :
4 : #include <QObject>
5 : #include <QList>
6 : #include <QSet>
7 :
8 : #include "../FangObject.h"
9 : class NewsItem;
10 :
11 : /**
12 : * @brief The NewsList class contains a custom list type that internally is also stored as a set.
13 : *
14 : * Could be a generic list class.
15 : */
16 : class NewsList : public FangObject
17 : {
18 : public:
19 : explicit NewsList(QObject *parent = nullptr);
20 : virtual ~NewsList();
21 :
22 : /**
23 : * @brief Delete everything in the list and clear it.
24 : */
25 : void clear();
26 :
27 : qsizetype size() const;
28 : bool isEmpty() const;
29 : NewsItem* first() const;
30 : NewsItem* last() const;
31 : NewsItem* at(qsizetype i) const;
32 : bool contains(NewsItem* value) const;
33 : qsizetype indexOf(const NewsItem* value, qsizetype from = 0) const;
34 :
35 : void append(NewsItem* value);
36 : void prepend(NewsItem* value);
37 :
38 : /**
39 : * @brief Removes a certain number of items from the start or end of the list and calls deleteLater() on them.
40 : * @param fromStart True if from start of list, false if from end.
41 : * @param numberToRemove
42 : */
43 : void removeAndDelete(bool fromStart, qsizetype numberToRemove);
44 :
45 : /**
46 : * @brief If loaded, returns the news item for a given ID.
47 : * Linear time complexity.
48 : */
49 : NewsItem* newsItemForID(const qint64 id) const;
50 :
51 : /**
52 : * @brief Returns the index of the position in the list if a news item with ID is in it, otherwise -1.
53 : * Linear time complexity.
54 : */
55 : qsizetype indexForItemID(const qint64 id) const;
56 :
57 : // For list iteration.
58 1 : inline QList<NewsItem*>::iterator begin() { return list.begin(); }
59 1 : inline QList<NewsItem*>::iterator end() { return list.end(); }
60 :
61 : private:
62 : // Ordered list of news items.
63 : QList<NewsItem*> list;
64 :
65 : // Set of news items for fast "contains" check.
66 : QSet<NewsItem*> set;
67 : };
68 :
69 : #endif // NEWSLIST_H
|