Chris@1729
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@1729
|
2
|
Chris@1729
|
3 /*
|
Chris@1729
|
4 Sonic Visualiser
|
Chris@1729
|
5 An audio file viewer and annotation editor.
|
Chris@1729
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@1729
|
7
|
Chris@1729
|
8 This program is free software; you can redistribute it and/or
|
Chris@1729
|
9 modify it under the terms of the GNU General Public License as
|
Chris@1729
|
10 published by the Free Software Foundation; either version 2 of the
|
Chris@1729
|
11 License, or (at your option) any later version. See the file
|
Chris@1729
|
12 COPYING included with this distribution for more information.
|
Chris@1729
|
13 */
|
Chris@1729
|
14
|
Chris@1729
|
15 #ifndef SV_BY_ID_H
|
Chris@1729
|
16 #define SV_BY_ID_H
|
Chris@1729
|
17
|
Chris@1729
|
18 #include <memory>
|
Chris@1729
|
19 #include <map>
|
Chris@1729
|
20
|
Chris@1729
|
21 #include <QMutex>
|
Chris@1729
|
22
|
Chris@1729
|
23 typedef int Id;
|
Chris@1729
|
24
|
Chris@1729
|
25 class WithId
|
Chris@1729
|
26 {
|
Chris@1729
|
27 public:
|
Chris@1729
|
28 WithId() :
|
Chris@1729
|
29 m_id(getNextId()) {
|
Chris@1729
|
30 }
|
Chris@1729
|
31
|
Chris@1729
|
32 Id getId() const {
|
Chris@1729
|
33 return m_id;
|
Chris@1729
|
34 }
|
Chris@1729
|
35
|
Chris@1729
|
36 private:
|
Chris@1729
|
37 Id m_id;
|
Chris@1729
|
38 static int getNextId();
|
Chris@1729
|
39 };
|
Chris@1729
|
40
|
Chris@1729
|
41 template <typename Item>
|
Chris@1729
|
42 class ById
|
Chris@1729
|
43 {
|
Chris@1729
|
44 public:
|
Chris@1729
|
45 void add(std::shared_ptr<Item> item) {
|
Chris@1729
|
46 QMutexLocker locker(&m_mutex);
|
Chris@1729
|
47 m_items[item->getId()] = item;
|
Chris@1729
|
48 }
|
Chris@1729
|
49
|
Chris@1729
|
50 void
|
Chris@1729
|
51 release(Id id) {
|
Chris@1729
|
52 QMutexLocker locker(&m_mutex);
|
Chris@1729
|
53 m_items.erase(id);
|
Chris@1729
|
54 }
|
Chris@1729
|
55
|
Chris@1729
|
56 std::shared_ptr<Item> get(Id id) const {
|
Chris@1729
|
57 QMutexLocker locker(&m_mutex);
|
Chris@1729
|
58 const auto &itr = m_items.find(id);
|
Chris@1729
|
59 if (itr != m_items.end()) {
|
Chris@1729
|
60 return itr->second;
|
Chris@1729
|
61 } else {
|
Chris@1729
|
62 return std::shared_ptr<Item>();
|
Chris@1729
|
63 }
|
Chris@1729
|
64 }
|
Chris@1729
|
65
|
Chris@1729
|
66 template <typename Derived>
|
Chris@1729
|
67 std::shared_ptr<Derived> getAs(Id id) const {
|
Chris@1729
|
68 return std::dynamic_pointer_cast<Derived>(get(id));
|
Chris@1729
|
69 }
|
Chris@1729
|
70
|
Chris@1729
|
71 private:
|
Chris@1729
|
72 mutable QMutex m_mutex;
|
Chris@1729
|
73 std::map<Id, std::shared_ptr<Item>> m_items;
|
Chris@1729
|
74 };
|
Chris@1729
|
75 /*
|
Chris@1729
|
76 class Imagined : public WithId {
|
Chris@1729
|
77 };
|
Chris@1729
|
78
|
Chris@1729
|
79 class ImaginedById
|
Chris@1729
|
80 {
|
Chris@1729
|
81 public:
|
Chris@1729
|
82 static void add(std::shared_ptr<Imagined> imagined) {
|
Chris@1729
|
83 m_byId.add(imagined);
|
Chris@1729
|
84 }
|
Chris@1729
|
85
|
Chris@1729
|
86 static void release(Id id) {
|
Chris@1729
|
87 m_byId.release(id);
|
Chris@1729
|
88 }
|
Chris@1729
|
89
|
Chris@1729
|
90 static std::shared_ptr<Imagined> get(Id id) {
|
Chris@1729
|
91 return m_byId.get(id);
|
Chris@1729
|
92 }
|
Chris@1729
|
93
|
Chris@1729
|
94 template <typename Derived>
|
Chris@1729
|
95 static
|
Chris@1729
|
96 std::shared_ptr<Derived> getAs(Id id) {
|
Chris@1729
|
97 return m_byId.getAs<Derived>(id);
|
Chris@1729
|
98 }
|
Chris@1729
|
99
|
Chris@1729
|
100 private:
|
Chris@1729
|
101 static ById<Imagined> m_byId;
|
Chris@1729
|
102 };
|
Chris@1729
|
103 */
|
Chris@1729
|
104 #endif
|
Chris@1729
|
105
|