Mercurial > hg > svcore
comparison base/ById.h @ 1731:601851995f4b by-id
Introduce Model to ById
author | Chris Cannam |
---|---|
date | Fri, 21 Jun 2019 13:37:00 +0100 |
parents | abd8b9673028 |
children | bffccc8de3c1 d91ff235e69d |
comparison
equal
deleted
inserted
replaced
1729:abd8b9673028 | 1731:601851995f4b |
---|---|
15 #ifndef SV_BY_ID_H | 15 #ifndef SV_BY_ID_H |
16 #define SV_BY_ID_H | 16 #define SV_BY_ID_H |
17 | 17 |
18 #include <memory> | 18 #include <memory> |
19 #include <map> | 19 #include <map> |
20 #include <typeinfo> | |
21 #include <iostream> | |
22 #include <climits> | |
20 | 23 |
21 #include <QMutex> | 24 #include <QMutex> |
25 #include <QString> | |
22 | 26 |
23 typedef int Id; | 27 template <typename T> |
28 struct SvId { | |
29 int id; | |
24 | 30 |
31 bool operator<(const SvId &other) const { return id < other.id; } | |
32 | |
33 QString toString() const { | |
34 return QString("%1").arg(id); | |
35 } | |
36 }; | |
37 | |
38 template <typename T> | |
25 class WithId | 39 class WithId |
26 { | 40 { |
27 public: | 41 public: |
42 typedef SvId<T> Id; | |
43 | |
28 WithId() : | 44 WithId() : |
29 m_id(getNextId()) { | 45 m_id(getNextId()) { |
30 } | 46 } |
31 | 47 |
48 /** | |
49 * Return an id for this object. The id is a unique identifier for | |
50 * this object among all objects that implement WithId within this | |
51 * single run of the application. | |
52 */ | |
32 Id getId() const { | 53 Id getId() const { |
33 return m_id; | 54 Id id; |
55 id.id = m_id; | |
56 return id; | |
34 } | 57 } |
35 | 58 |
36 private: | 59 private: |
37 Id m_id; | 60 int m_id; |
38 static int getNextId(); | 61 |
62 static int getNextId() { | |
63 static int nextId = 0; | |
64 static QMutex mutex; | |
65 QMutexLocker locker(&mutex); | |
66 int i = nextId; | |
67 if (nextId == INT_MAX) { | |
68 nextId = INT_MIN; | |
69 } | |
70 ++nextId; | |
71 return i; | |
72 } | |
39 }; | 73 }; |
40 | 74 |
41 template <typename Item> | 75 template <typename Item, typename Id> |
42 class ById | 76 class ById |
43 { | 77 { |
44 public: | 78 public: |
79 ~ById() { | |
80 QMutexLocker locker(&m_mutex); | |
81 for (const auto &p: m_items) { | |
82 if (p.second && p.second.use_count() > 0) { | |
83 std::cerr << "WARNING: ById map destroyed with use count of " | |
84 << p.second.use_count() << " for item with type " | |
85 << typeid(*p.second.get()).name() | |
86 << " and id " << p.first.id << std::endl; | |
87 } | |
88 } | |
89 } | |
90 | |
45 void add(std::shared_ptr<Item> item) { | 91 void add(std::shared_ptr<Item> item) { |
46 QMutexLocker locker(&m_mutex); | 92 QMutexLocker locker(&m_mutex); |
47 m_items[item->getId()] = item; | 93 m_items[item->getId()] = item; |
48 } | 94 } |
49 | 95 |
70 | 116 |
71 private: | 117 private: |
72 mutable QMutex m_mutex; | 118 mutable QMutex m_mutex; |
73 std::map<Id, std::shared_ptr<Item>> m_items; | 119 std::map<Id, std::shared_ptr<Item>> m_items; |
74 }; | 120 }; |
75 /* | |
76 class Imagined : public WithId { | |
77 }; | |
78 | 121 |
79 class ImaginedById | 122 template <typename Item, typename Id> |
123 class StaticById | |
80 { | 124 { |
81 public: | 125 public: |
82 static void add(std::shared_ptr<Imagined> imagined) { | 126 static void add(std::shared_ptr<Item> imagined) { |
83 m_byId.add(imagined); | 127 byId().add(imagined); |
84 } | 128 } |
85 | 129 |
86 static void release(Id id) { | 130 static void release(Id id) { |
87 m_byId.release(id); | 131 byId().release(id); |
88 } | 132 } |
89 | 133 |
90 static std::shared_ptr<Imagined> get(Id id) { | 134 static std::shared_ptr<Item> get(Id id) { |
91 return m_byId.get(id); | 135 return byId().get(id); |
92 } | 136 } |
93 | 137 |
94 template <typename Derived> | 138 template <typename Derived> |
95 static | 139 static |
96 std::shared_ptr<Derived> getAs(Id id) { | 140 std::shared_ptr<Derived> getAs(Id id) { |
97 return m_byId.getAs<Derived>(id); | 141 return std::dynamic_pointer_cast<Derived>(get(id)); |
98 } | 142 } |
99 | 143 |
100 private: | 144 private: |
101 static ById<Imagined> m_byId; | 145 static |
146 ById<Item, Id> &byId() { | |
147 static ById<Item, Id> b; | |
148 return b; | |
149 } | |
102 }; | 150 }; |
103 */ | 151 |
104 #endif | 152 #endif |
105 | 153 |