Mercurial > hg > svcore
comparison base/ById.h @ 1739:565575463752 by-id
Some work on models and transformers
author | Chris Cannam |
---|---|
date | Wed, 26 Jun 2019 14:59:09 +0100 |
parents | 4abc0f08adf9 |
children | 52705a328b34 |
comparison
equal
deleted
inserted
replaced
1738:4abc0f08adf9 | 1739:565575463752 |
---|---|
50 | 50 |
51 QString toString() const { | 51 QString toString() const { |
52 return QString("%1").arg(id); | 52 return QString("%1").arg(id); |
53 } | 53 } |
54 }; | 54 }; |
55 | |
56 template <typename T> | |
57 std::ostream & | |
58 operator<<(std::ostream &ostr, const SvId<T> &id) | |
59 { | |
60 // For diagnostic purposes only. Do not use these IDs for | |
61 // serialisation - see XmlExportable instead. | |
62 if (id.isNone()) { | |
63 return (ostr << "<none>"); | |
64 } else { | |
65 return (ostr << "#" << id.id); | |
66 } | |
67 } | |
55 | 68 |
56 template <typename T> | 69 template <typename T> |
57 class WithId | 70 class WithId |
58 { | 71 { |
59 public: | 72 public: |
111 } | 124 } |
112 | 125 |
113 void add(std::shared_ptr<Item> item) { | 126 void add(std::shared_ptr<Item> item) { |
114 QMutexLocker locker(&m_mutex); | 127 QMutexLocker locker(&m_mutex); |
115 auto id = item->getId(); | 128 auto id = item->getId(); |
129 if (id.isNone()) { | |
130 throw std::logic_error("item id should never be None"); | |
131 } | |
116 if (m_items.find(id) != m_items.end()) { | 132 if (m_items.find(id) != m_items.end()) { |
117 SVCERR << "WARNING: ById::add: item with id " << id | 133 SVCERR << "WARNING: ById::add: item with id " << id |
118 << " is already recorded, replacing it (item type is " | 134 << " is already recorded, replacing it (item type is " |
119 << typeid(*item.get()).name() << ")" << endl; | 135 << typeid(*item.get()).name() << ")" << endl; |
120 } | 136 } |
126 QMutexLocker locker(&m_mutex); | 142 QMutexLocker locker(&m_mutex); |
127 m_items.erase(id); | 143 m_items.erase(id); |
128 } | 144 } |
129 | 145 |
130 std::shared_ptr<Item> get(Id id) const { | 146 std::shared_ptr<Item> get(Id id) const { |
147 if (id.isNone()) return {}; // this id is never issued: avoid locking | |
131 QMutexLocker locker(&m_mutex); | 148 QMutexLocker locker(&m_mutex); |
132 const auto &itr = m_items.find(id); | 149 const auto &itr = m_items.find(id); |
133 if (itr != m_items.end()) { | 150 if (itr != m_items.end()) { |
134 return itr->second; | 151 return itr->second; |
135 } else { | 152 } else { |
136 return std::shared_ptr<Item>(); | 153 return {}; |
137 } | 154 } |
138 } | 155 } |
139 | 156 |
140 template <typename Derived> | 157 template <typename Derived> |
141 std::shared_ptr<Derived> getAs(Id id) const { | 158 std::shared_ptr<Derived> getAs(Id id) const { |
142 return std::dynamic_pointer_cast<Derived>(get(id)); | 159 return std::dynamic_pointer_cast<Derived>(get(id)); |
143 } | 160 } |
144 | 161 |
145 /** | 162 /** |
146 * If the Item type is an XmlExportable, return the export ID of | 163 * If the Item type is an XmlExportable, return the export ID of |
147 * the given item ID. The export ID is a simple int, and is only | 164 * the given item ID. A call to this function will fail to compile |
148 * allocated when first requested, so objects that are never | 165 * if the Item is not an XmlExportable. |
149 * exported don't get one. | 166 * |
167 * The export ID is a simple int, and is only allocated when first | |
168 * requested, so objects that are never exported don't get one. | |
150 */ | 169 */ |
151 int getExportId(Id id) const { | 170 int getExportId(Id id) const { |
152 auto exportable = getAs<XmlExportable>(id); | 171 auto exportable = getAs<XmlExportable>(id); |
153 if (exportable) { | 172 if (exportable) { |
154 return exportable->getExportId(); | 173 return exportable->getExportId(); |