comparison base/ById.h @ 1734:bffccc8de3c1 by-id

Warn for already-existing id
author Chris Cannam
date Mon, 24 Jun 2019 18:42:28 +0100
parents 601851995f4b
children d9082ed16931
comparison
equal deleted inserted replaced
1733:c3b5564cfb78 1734:bffccc8de3c1
12 COPYING included with this distribution for more information. 12 COPYING included with this distribution for more information.
13 */ 13 */
14 14
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
18 #include "Debug.h"
17 19
18 #include <memory> 20 #include <memory>
19 #include <map> 21 #include <map>
20 #include <typeinfo> 22 #include <typeinfo>
21 #include <iostream> 23 #include <iostream>
78 public: 80 public:
79 ~ById() { 81 ~ById() {
80 QMutexLocker locker(&m_mutex); 82 QMutexLocker locker(&m_mutex);
81 for (const auto &p: m_items) { 83 for (const auto &p: m_items) {
82 if (p.second && p.second.use_count() > 0) { 84 if (p.second && p.second.use_count() > 0) {
83 std::cerr << "WARNING: ById map destroyed with use count of " 85 SVCERR << "WARNING: ById map destroyed with use count of "
84 << p.second.use_count() << " for item with type " 86 << p.second.use_count() << " for item with type "
85 << typeid(*p.second.get()).name() 87 << typeid(*p.second.get()).name()
86 << " and id " << p.first.id << std::endl; 88 << " and id " << p.first.id << endl;
87 } 89 }
88 } 90 }
89 } 91 }
90 92
91 void add(std::shared_ptr<Item> item) { 93 void add(std::shared_ptr<Item> item) {
92 QMutexLocker locker(&m_mutex); 94 QMutexLocker locker(&m_mutex);
93 m_items[item->getId()] = item; 95 auto id = item->getId();
96 if (m_items.find(id) != m_items.end()) {
97 SVCERR << "WARNING: ById::add: item with id " << id
98 << " is already recorded, replacing it (item type is "
99 << typeid(*item.get()).name() << ")" << endl;
100 }
101 m_items[id] = item;
94 } 102 }
95 103
96 void 104 void
97 release(Id id) { 105 release(Id id) {
98 QMutexLocker locker(&m_mutex); 106 QMutexLocker locker(&m_mutex);