1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Sonic Visualiser
An audio file viewer and annotation editor.
Centre for Digital Music, Queen Mary, University of London.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#include "ById.h"
#include <unordered_map>
#include <typeinfo>
#include <QObject>
//#define DEBUG_BY_ID 1
namespace sv {
int IdAlloc::getNextId()
{
static int nextId = 0;
static QMutex mutex;
QMutexLocker locker(&mutex);
int i = nextId;
if (nextId == INT_MAX) {
nextId = INT_MIN;
} else {
++nextId;
if (nextId == 0 || nextId == NO_ID) {
throw std::runtime_error("Internal ID limit exceeded!");
}
}
return i;
}
// "warning: expression with side effects will be evaluated despite
// being used as an operand to 'typeid'"
#ifdef __clang__
#pragma clang diagnostic ignored "-Wpotentially-evaluated-expression"
#endif
class AnyById::Impl
{
public:
~Impl() {
QMutexLocker locker(&m_mutex);
bool empty = true;
for (const auto &p: m_items) {
if (p.second && p.second.use_count() > 0) {
empty = false;
break;
}
}
if (!empty) {
SVCERR << "WARNING: ById map is not empty at close; some items have not been released" << endl;
SVCERR << " Unreleased items are:" << endl;
for (const auto &p: m_items) {
auto ptr = p.second;
if (ptr && ptr.use_count() > 0) {
QString message = QString("id #%1: type %2")
.arg(p.first).arg(typeid(*ptr.get()).name());
if (auto qobj = std::dynamic_pointer_cast<QObject>(ptr)) {
message += QString(", object name \"%1\"")
.arg(qobj->objectName());
}
message += QString(", use count %1").arg(ptr.use_count());
SVCERR << " - " << message << endl;
}
}
}
}
int add(std::shared_ptr<WithId> item) {
int id = item->getUntypedId();
if (id == IdAlloc::NO_ID) {
throw std::logic_error("item id should never be NO_ID");
}
#ifdef DEBUG_BY_ID
SVCERR << "ById::add(#" << id << ") of type "
<< typeid(*item.get()).name() << endl;
#endif
QMutexLocker locker(&m_mutex);
if (m_items.find(id) != m_items.end()) {
SVCERR << "ById::add: item with id " << id
<< " is already recorded (existing item type is "
<< typeid(*m_items.find(id)->second.get()).name()
<< ", proposed is "
<< typeid(*item.get()).name() << ")" << endl;
throw std::logic_error("item id is already recorded in add");
}
m_items[id] = item;
return id;
}
void release(int id) {
if (id == IdAlloc::NO_ID) {
return;
}
#ifdef DEBUG_BY_ID
SVCERR << "ById::release(#" << id << ")" << endl;
#endif
QMutexLocker locker(&m_mutex);
if (m_items.find(id) == m_items.end()) {
SVCERR << "ById::release: unknown item id " << id << endl;
throw std::logic_error("unknown item id in release");
}
m_items.erase(id);
}
std::shared_ptr<WithId> get(int id) const {
if (id == IdAlloc::NO_ID) {
return {}; // this id cannot be added: avoid locking
}
QMutexLocker locker(&m_mutex);
const auto &itr = m_items.find(id);
if (itr != m_items.end()) {
return itr->second;
} else {
return {};
}
}
private:
mutable QMutex m_mutex;
std::unordered_map<int, std::shared_ptr<WithId>> m_items;
};
int
AnyById::add(std::shared_ptr<WithId> item)
{
return impl().add(item);
}
void
AnyById::release(int id)
{
impl().release(id);
}
std::shared_ptr<WithId>
AnyById::get(int id)
{
return impl().get(id);
}
AnyById::Impl &
AnyById::impl()
{
static Impl impl;
return impl;
}
} // end namespace sv
|