annotate base/ById.cpp @ 1742:52705a328b34 by-id

Rejig ById so as to put everything in a single pool, so that at the core you can go from numeric id (untyped) to anything the object can be dynamic_cast to. Useful for building other abstractions like PlayParameter-type registrations that don't know about e.g. Models. Probably some more tweaking needed. Also add tests
author Chris Cannam
date Fri, 28 Jun 2019 17:36:30 +0100
parents
children b92bdcd4954b
rev   line source
Chris@1742 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1742 2
Chris@1742 3 /*
Chris@1742 4 Sonic Visualiser
Chris@1742 5 An audio file viewer and annotation editor.
Chris@1742 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1742 7
Chris@1742 8 This program is free software; you can redistribute it and/or
Chris@1742 9 modify it under the terms of the GNU General Public License as
Chris@1742 10 published by the Free Software Foundation; either version 2 of the
Chris@1742 11 License, or (at your option) any later version. See the file
Chris@1742 12 COPYING included with this distribution for more information.
Chris@1742 13 */
Chris@1742 14
Chris@1742 15 #include "ById.h"
Chris@1742 16
Chris@1742 17 #include <unordered_map>
Chris@1742 18 #include <typeinfo>
Chris@1742 19
Chris@1742 20 int IdAlloc::getNextId()
Chris@1742 21 {
Chris@1742 22 static int nextId = 0;
Chris@1742 23 static QMutex mutex;
Chris@1742 24 QMutexLocker locker(&mutex);
Chris@1742 25 int i = nextId;
Chris@1742 26 if (nextId == INT_MAX) {
Chris@1742 27 nextId = INT_MIN;
Chris@1742 28 } else {
Chris@1742 29 ++nextId;
Chris@1742 30 if (nextId == 0 || nextId == NO_ID) {
Chris@1742 31 throw std::runtime_error("Internal ID limit exceeded!");
Chris@1742 32 }
Chris@1742 33 }
Chris@1742 34 return i;
Chris@1742 35 }
Chris@1742 36
Chris@1742 37 class AnyById::Impl
Chris@1742 38 {
Chris@1742 39 public:
Chris@1742 40 ~Impl() {
Chris@1742 41 QMutexLocker locker(&m_mutex);
Chris@1742 42 bool empty = true;
Chris@1742 43 for (const auto &p: m_items) {
Chris@1742 44 if (p.second && p.second.use_count() > 0) {
Chris@1742 45 empty = false;
Chris@1742 46 break;
Chris@1742 47 }
Chris@1742 48 }
Chris@1742 49 if (!empty) {
Chris@1742 50 SVCERR << "WARNING: ById map is not empty at close; some items have not been released" << endl;
Chris@1742 51 SVCERR << " Unreleased items are:" << endl;
Chris@1742 52 for (const auto &p: m_items) {
Chris@1742 53 if (p.second && p.second.use_count() > 0) {
Chris@1742 54 SVCERR << " - id #" << p.first
Chris@1742 55 << ": type " << typeid(*p.second.get()).name()
Chris@1742 56 << ", use count " << p.second.use_count() << endl;
Chris@1742 57 }
Chris@1742 58 }
Chris@1742 59 }
Chris@1742 60 }
Chris@1742 61
Chris@1742 62 void add(int id, std::shared_ptr<WithId> item) {
Chris@1742 63 QMutexLocker locker(&m_mutex);
Chris@1742 64 if (m_items.find(id) != m_items.end()) {
Chris@1742 65 SVCERR << "ById::add: item with id " << id
Chris@1742 66 << " is already recorded (existing item type is "
Chris@1742 67 << typeid(*m_items.find(id)->second.get()).name()
Chris@1742 68 << ", proposed is "
Chris@1742 69 << typeid(*item.get()).name() << ")" << endl;
Chris@1742 70 throw std::logic_error("item id is already recorded in add");
Chris@1742 71 }
Chris@1742 72 m_items[id] = item;
Chris@1742 73 }
Chris@1742 74
Chris@1742 75 void release(int id) {
Chris@1742 76 QMutexLocker locker(&m_mutex);
Chris@1742 77 if (m_items.find(id) == m_items.end()) {
Chris@1742 78 SVCERR << "ById::release: unknown item id " << id << endl;
Chris@1742 79 throw std::logic_error("unknown item id in release");
Chris@1742 80 }
Chris@1742 81 m_items.erase(id);
Chris@1742 82 }
Chris@1742 83
Chris@1742 84 std::shared_ptr<WithId> get(int id) const {
Chris@1742 85 QMutexLocker locker(&m_mutex);
Chris@1742 86 const auto &itr = m_items.find(id);
Chris@1742 87 if (itr != m_items.end()) {
Chris@1742 88 return itr->second;
Chris@1742 89 } else {
Chris@1742 90 return {};
Chris@1742 91 }
Chris@1742 92 }
Chris@1742 93
Chris@1742 94 private:
Chris@1742 95 mutable QMutex m_mutex;
Chris@1742 96 std::unordered_map<int, std::shared_ptr<WithId>> m_items;
Chris@1742 97 };
Chris@1742 98
Chris@1742 99 void
Chris@1742 100 AnyById::add(int id, std::shared_ptr<WithId> item)
Chris@1742 101 {
Chris@1742 102 impl().add(id, item);
Chris@1742 103 }
Chris@1742 104
Chris@1742 105 void
Chris@1742 106 AnyById::release(int id)
Chris@1742 107 {
Chris@1742 108 impl().release(id);
Chris@1742 109 }
Chris@1742 110
Chris@1742 111 std::shared_ptr<WithId>
Chris@1742 112 AnyById::get(int id)
Chris@1742 113 {
Chris@1742 114 return impl().get(id);
Chris@1742 115 }
Chris@1742 116
Chris@1742 117 AnyById::Impl &
Chris@1742 118 AnyById::impl()
Chris@1742 119 {
Chris@1742 120 static Impl impl;
Chris@1742 121 return impl;
Chris@1742 122 }