comparison base/ById.h @ 1738:4abc0f08adf9 by-id

More on alignment models and paths
author Chris Cannam
date Wed, 26 Jun 2019 10:21:15 +0100
parents d9082ed16931
children 565575463752
comparison
equal deleted inserted replaced
1737:5d631f6129fe 1738:4abc0f08adf9
23 #include <iostream> 23 #include <iostream>
24 #include <climits> 24 #include <climits>
25 25
26 #include <QMutex> 26 #include <QMutex>
27 #include <QString> 27 #include <QString>
28
29 #include "XmlExportable.h"
28 30
29 template <typename T> 31 template <typename T>
30 struct SvId { 32 struct SvId {
31 33
32 int id; 34 int id;
137 139
138 template <typename Derived> 140 template <typename Derived>
139 std::shared_ptr<Derived> getAs(Id id) const { 141 std::shared_ptr<Derived> getAs(Id id) const {
140 return std::dynamic_pointer_cast<Derived>(get(id)); 142 return std::dynamic_pointer_cast<Derived>(get(id));
141 } 143 }
144
145 /**
146 * 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
148 * allocated when first requested, so objects that are never
149 * exported don't get one.
150 */
151 int getExportId(Id id) const {
152 auto exportable = getAs<XmlExportable>(id);
153 if (exportable) {
154 return exportable->getExportId();
155 } else {
156 return XmlExportable::NO_ID;
157 }
158 }
142 159
143 private: 160 private:
144 mutable QMutex m_mutex; 161 mutable QMutex m_mutex;
145 std::map<Id, std::shared_ptr<Item>> m_items; 162 std::map<Id, std::shared_ptr<Item>> m_items;
146 }; 163 };
165 static 182 static
166 std::shared_ptr<Derived> getAs(Id id) { 183 std::shared_ptr<Derived> getAs(Id id) {
167 return std::dynamic_pointer_cast<Derived>(get(id)); 184 return std::dynamic_pointer_cast<Derived>(get(id));
168 } 185 }
169 186
187 static int getExportId(Id id) {
188 return byId().getExportId(id);
189 }
190
170 private: 191 private:
171 static 192 static
172 ById<Item, Id> &byId() { 193 ById<Item, Id> &byId() {
173 static ById<Item, Id> b; 194 static ById<Item, Id> b;
174 return b; 195 return b;