# HG changeset patch # User Chris Cannam # Date 1561039059 -3600 # Node ID abd8b9673028e9397d2bc78b57e7f29e601853c6 # Parent 8efce64dd85ec5669cf094df639996eddb0bc78f Experiment toward working out an id-model store diff -r 8efce64dd85e -r abd8b9673028 base/ById.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/base/ById.cpp Thu Jun 20 14:57:39 2019 +0100 @@ -0,0 +1,33 @@ +/* -*- 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 + +#include + +int +WithId::getNextId() +{ + static int nextId = 0; + static QMutex mutex; + QMutexLocker locker(&mutex); + int i = nextId; + if (nextId == INT_MAX) { + nextId = INT_MIN; + } + ++nextId; + return i; +} diff -r 8efce64dd85e -r abd8b9673028 base/ById.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/base/ById.h Thu Jun 20 14:57:39 2019 +0100 @@ -0,0 +1,105 @@ +/* -*- 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. +*/ + +#ifndef SV_BY_ID_H +#define SV_BY_ID_H + +#include +#include + +#include + +typedef int Id; + +class WithId +{ +public: + WithId() : + m_id(getNextId()) { + } + + Id getId() const { + return m_id; + } + +private: + Id m_id; + static int getNextId(); +}; + +template +class ById +{ +public: + void add(std::shared_ptr item) { + QMutexLocker locker(&m_mutex); + m_items[item->getId()] = item; + } + + void + release(Id id) { + QMutexLocker locker(&m_mutex); + m_items.erase(id); + } + + std::shared_ptr get(Id id) const { + QMutexLocker locker(&m_mutex); + const auto &itr = m_items.find(id); + if (itr != m_items.end()) { + return itr->second; + } else { + return std::shared_ptr(); + } + } + + template + std::shared_ptr getAs(Id id) const { + return std::dynamic_pointer_cast(get(id)); + } + +private: + mutable QMutex m_mutex; + std::map> m_items; +}; +/* +class Imagined : public WithId { +}; + +class ImaginedById +{ +public: + static void add(std::shared_ptr imagined) { + m_byId.add(imagined); + } + + static void release(Id id) { + m_byId.release(id); + } + + static std::shared_ptr get(Id id) { + return m_byId.get(id); + } + + template + static + std::shared_ptr getAs(Id id) { + return m_byId.getAs(id); + } + +private: + static ById m_byId; +}; +*/ +#endif + diff -r 8efce64dd85e -r abd8b9673028 files.pri --- a/files.pri Thu Jun 20 11:09:36 2019 +0100 +++ b/files.pri Thu Jun 20 14:57:39 2019 +0100 @@ -3,6 +3,7 @@ base/AudioPlaySource.h \ base/AudioRecordTarget.h \ base/BaseTypes.h \ + base/ById.h \ base/Clipboard.h \ base/ColumnOp.h \ base/Command.h \ @@ -149,6 +150,7 @@ SVCORE_SOURCES = \ base/AudioLevel.cpp \ + base/ById.cpp \ base/Clipboard.cpp \ base/ColumnOp.cpp \ base/Command.cpp \