comparison base/ById.cpp @ 1744:b92bdcd4954b by-id

Update FFT model to ById
author Chris Cannam
date Tue, 02 Jul 2019 11:49:28 +0100
parents 52705a328b34
children 6d09d68165a4
comparison
equal deleted inserted replaced
1743:7001b9570e37 1744:b92bdcd4954b
58 } 58 }
59 } 59 }
60 } 60 }
61 61
62 void add(int id, std::shared_ptr<WithId> item) { 62 void add(int id, std::shared_ptr<WithId> item) {
63 if (id == IdAlloc::NO_ID) {
64 throw std::logic_error("cannot add item with id of NO_ID");
65 }
63 QMutexLocker locker(&m_mutex); 66 QMutexLocker locker(&m_mutex);
64 if (m_items.find(id) != m_items.end()) { 67 if (m_items.find(id) != m_items.end()) {
65 SVCERR << "ById::add: item with id " << id 68 SVCERR << "ById::add: item with id " << id
66 << " is already recorded (existing item type is " 69 << " is already recorded (existing item type is "
67 << typeid(*m_items.find(id)->second.get()).name() 70 << typeid(*m_items.find(id)->second.get()).name()
71 } 74 }
72 m_items[id] = item; 75 m_items[id] = item;
73 } 76 }
74 77
75 void release(int id) { 78 void release(int id) {
79 if (id == IdAlloc::NO_ID) {
80 return;
81 }
76 QMutexLocker locker(&m_mutex); 82 QMutexLocker locker(&m_mutex);
77 if (m_items.find(id) == m_items.end()) { 83 if (m_items.find(id) == m_items.end()) {
78 SVCERR << "ById::release: unknown item id " << id << endl; 84 SVCERR << "ById::release: unknown item id " << id << endl;
79 throw std::logic_error("unknown item id in release"); 85 throw std::logic_error("unknown item id in release");
80 } 86 }
81 m_items.erase(id); 87 m_items.erase(id);
82 } 88 }
83 89
84 std::shared_ptr<WithId> get(int id) const { 90 std::shared_ptr<WithId> get(int id) const {
91 if (id == IdAlloc::NO_ID) {
92 return {}; // this id cannot be added: avoid locking
93 }
85 QMutexLocker locker(&m_mutex); 94 QMutexLocker locker(&m_mutex);
86 const auto &itr = m_items.find(id); 95 const auto &itr = m_items.find(id);
87 if (itr != m_items.end()) { 96 if (itr != m_items.end()) {
88 return itr->second; 97 return itr->second;
89 } else { 98 } else {