ById.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version. See the file
12  COPYING included with this distribution for more information.
13 */
14 
15 #ifndef SV_BY_ID_H
16 #define SV_BY_ID_H
17 
18 #include "Debug.h"
19 
20 #include <memory>
21 #include <iostream>
22 #include <climits>
23 #include <stdexcept>
24 
25 #include <QMutex>
26 #include <QString>
27 
28 #include "XmlExportable.h"
29 
30 /*
31  * ById - central pool of objects to be retrieved by persistent id.
32  *
33  * This is a pretty simple mechanism for obtaining safe "borrowed"
34  * references to shared objects, including across threads, based on an
35  * object ID.
36  *
37  * A class (call it C) inherits WithTypedId<C>. This produces a type
38  * C::Id containing a numerical id. Each instance of C (or subclass
39  * thereof) has an internal id of type C::Id whose value is unique
40  * among all ids ever possessed by any instances of all classes that
41  * use this id mechanism (within a single run of the program).
42  *
43  * Then we have a static store of type TypedById<C, C::Id>. This holds
44  * a set of heap-allocated C objects (or subclass thereof) and hands
45  * out shared_ptr references to them when queried by id. The
46  * application calls add() to pass an object to the store (which takes
47  * ownership of it), and the application calls release() when it
48  * thinks it has finished with an object, to request the store to
49  * delete it.
50  *
51  * Note that an object's id can't (without shenanigans) be queried
52  * directly from that object - it is returned when the object is added
53  * to a ById store. So if you have an object id, you know that the
54  * object must have been added to a store at some point.
55  *
56  * The goal is to improve code that would previously have retained a
57  * bare pointer to a heap-allocated object that it did not own. For
58  * example, in Sonic Visualiser we have a Model hierarchy of complex
59  * mutable objects, and any given model may be referred to by many
60  * different layers, transforms (as both source and target) etc. Using
61  * bare pointers for those references means that we need everything to
62  * be notified (and act properly on the notification) if a model is
63  * about to be deleted. Using a Model::Id instead gives the code a
64  * guarantee: if the model has been deleted since you last looked at
65  * it, then the ById store will return a null shared_ptr from its
66  * get() function for that id; but if it returns a non-null
67  * shared_ptr, then the object being pointed to can't be deleted while
68  * that shared_ptr is in scope.
69  *
70  * The result is like using weak_ptr references to a shared_ptr whose
71  * ownership is within a "layer" (at "application level") rather than
72  * managed by a specific object. A human-readable id representation is
73  * also quite useful for debugging.
74  *
75  * Example:
76  *
77  * class Thing : public WithTypedId<Thing> { Thing(int x) { } };
78  * typedef TypedById<Thing, Thing::Id> ThingById;
79  *
80  * // application creates a new Thing
81  * // ...
82  * auto thing = std::make_shared<Thing>(10);
83  * auto thingId = ThingById::add(thing);
84  *
85  * // application then passes thingId to something else, without
86  * // storing the shared_ptr anywhere - the ById store manages that
87  *
88  * // code elsewhere now has the thingId, and needs to use the Thing
89  * // ...
90  * void doSomething() {
91  * auto thing = ThingById::get(m_thingId);
92  * if (!thing) { // the Thing has been deleted, stop acting on it
93  * return; // (this may be an error or it may be unexceptional)
94  * }
95  * // now we have a guarantee that the thing ptr will be valid
96  * // until it goes out of scope when doSomething returns
97  * }
98  *
99  * // application wants to be rid of the Thing
100  * ThingById::release(thingId);
101  */
102 
104 // just be using get
105 
106 struct IdAlloc {
107 
108  // The value NO_ID (-1) is never allocated
109  static const int NO_ID = -1;
110 
111  static int getNextId();
112 };
113 
114 template <typename T>
115 struct TypedId {
116 
117  int untyped;
118 
119  TypedId() : untyped(IdAlloc::NO_ID) {}
120 
121  TypedId(const TypedId &) =default;
122  TypedId &operator=(const TypedId &) =default;
123 
124  bool operator==(const TypedId &other) const {
125  return untyped == other.untyped;
126  }
127  bool operator!=(const TypedId &other) const {
128  return untyped != other.untyped;
129  }
130  bool operator<(const TypedId &other) const {
131  return untyped < other.untyped;
132  }
133  bool isNone() const {
134  return untyped == IdAlloc::NO_ID;
135  }
136 };
137 
138 template <typename T>
139 std::ostream &
140 operator<<(std::ostream &ostr, const TypedId<T> &id)
141 {
142  // For diagnostic purposes only. Do not use these IDs for
143  // serialisation - see XmlExportable instead.
144  if (id.isNone()) {
145  return (ostr << "<none>");
146  } else {
147  return (ostr << "#" << id.untyped);
148  }
149 }
150 
151 class WithId
152 {
153 public:
154  WithId() :
155  m_id(IdAlloc::getNextId()) {
156  }
157  virtual ~WithId() {
158  }
159 
160 protected:
161  friend class AnyById;
162 
168  int getUntypedId() const {
169  return m_id;
170  }
171 
172 private:
173  int m_id;
174 };
175 
176 template <typename T>
177 class WithTypedId : virtual public WithId
178 {
179 public:
180  typedef TypedId<T> Id;
181 
182  WithTypedId() : WithId() { }
183 
184 protected:
185  template <typename Item, typename Id>
186  friend class TypedById;
187 
193  Id getId() const {
194  Id id;
195  id.untyped = getUntypedId();
196  return id;
197  }
198 };
199 
200 class AnyById
201 {
202 public:
203  static int add(std::shared_ptr<WithId>);
204  static void release(int);
205  static std::shared_ptr<WithId> get(int);
206 
207  template <typename Derived>
208  static bool isa(int id) {
209  std::shared_ptr<WithId> p = get(id);
210  return bool(std::dynamic_pointer_cast<Derived>(p));
211  }
212 
213  template <typename Derived>
214  static std::shared_ptr<Derived> getAs(int id) {
215  std::shared_ptr<WithId> p = get(id);
216  return std::dynamic_pointer_cast<Derived>(p);
217  }
218 
219 private:
220  class Impl;
221  static Impl &impl();
222 };
223 
224 template <typename Item, typename Id>
226 {
227 public:
228  static Id add(std::shared_ptr<Item> item) {
229  Id id;
230  id.untyped = AnyById::add(item);
231  return id;
232  }
233 
234  static void release(Id id) {
235  AnyById::release(id.untyped);
236  }
237  static void release(std::shared_ptr<Item> item) {
238  release(item->getId());
239  }
240 
241  template <typename Derived>
242  static bool isa(Id id) {
243  return AnyById::isa<Derived>(id.untyped);
244  }
245 
246  template <typename Derived>
247  static std::shared_ptr<Derived> getAs(Id id) {
248  return AnyById::getAs<Derived>(id.untyped);
249  }
250 
251  static std::shared_ptr<Item> get(Id id) {
252  return getAs<Item>(id);
253  }
254 
263  static int getExportId(Id id) {
264  auto exportable = getAs<XmlExportable>(id);
265  if (exportable) {
266  return exportable->getExportId();
267  } else {
268  return XmlExportable::NO_ID;
269  }
270  }
271 };
272 
273 #endif
274 
bool isNone() const
Definition: ById.h:133
static void release(std::shared_ptr< Item > item)
Definition: ById.h:237
static const int NO_ID
Definition: ById.h:109
WithTypedId()
Definition: ById.h:182
static std::shared_ptr< Derived > getAs(Id id)
Definition: ById.h:247
WithId()
Definition: ById.h:154
int getUntypedId() const
Return an id for this object.
Definition: ById.h:168
int untyped
Definition: ById.h:117
static std::shared_ptr< Derived > getAs(int id)
Definition: ById.h:214
Definition: ById.h:151
Id getId() const
Return an id for this object.
Definition: ById.h:193
static void release(int)
Definition: ById.cpp:138
static Id add(std::shared_ptr< Item > item)
Definition: ById.h:228
static int add(std::shared_ptr< WithId >)
Definition: ById.cpp:132
TypedId()
Definition: ById.h:119
bool operator!=(const TypedId &other) const
Definition: ById.h:127
Definition: ById.h:200
bool operator<(const TypedId &other) const
Definition: ById.h:130
bool operator==(const TypedId &other) const
Definition: ById.h:124
static bool isa(Id id)
Definition: ById.h:242
TypedId< T > Id
Definition: ById.h:180
static void release(Id id)
Definition: ById.h:234
!! to do: review how often we are calling getAs<...> when we could
Definition: ById.h:106
static int getExportId(Id id)
If the Item type is an XmlExportable, return the export ID of the given item ID.
Definition: ById.h:263
int m_id
Definition: ById.h:173
Definition: ById.h:115
static bool isa(int id)
Definition: ById.h:208
virtual ~WithId()
Definition: ById.h:157
static int getNextId()
Definition: ById.cpp:22