comparison base/ById.h @ 1752:6d09d68165a4 by-id

Further review of ById: make IDs only available when adding a model to the ById store, not by querying the item directly. This means any id encountered in the wild must have been added to the store at some point (even if later released), which simplifies reasoning about lifecycles
author Chris Cannam
date Fri, 05 Jul 2019 15:28:07 +0100
parents d0ef65d8dd89
children d954dfccd922
comparison
equal deleted inserted replaced
1751:77543124651b 1752:6d09d68165a4
93 m_id(IdAlloc::getNextId()) { 93 m_id(IdAlloc::getNextId()) {
94 } 94 }
95 virtual ~WithId() { 95 virtual ~WithId() {
96 } 96 }
97 97
98 protected:
99 friend class AnyById;
100
98 /** 101 /**
99 * Return an id for this object. The id is a unique number for 102 * Return an id for this object. The id is a unique number for
100 * this object among all objects that implement WithId within this 103 * this object among all objects that implement WithId within this
101 * single run of the application. 104 * single run of the application.
102 */ 105 */
114 public: 117 public:
115 typedef TypedId<T> Id; 118 typedef TypedId<T> Id;
116 119
117 WithTypedId() : WithId() { } 120 WithTypedId() : WithId() { }
118 121
122 protected:
123 template <typename Item, typename Id>
124 friend class TypedById;
125
119 /** 126 /**
120 * Return an id for this object. The id is a unique value for this 127 * Return an id for this object. The id is a unique value for this
121 * object among all objects that implement WithTypedId within this 128 * object among all objects that implement WithTypedId within this
122 * single run of the application. 129 * single run of the application.
123 */ 130 */
129 }; 136 };
130 137
131 class AnyById 138 class AnyById
132 { 139 {
133 public: 140 public:
134 static void add(int, std::shared_ptr<WithId>); 141 static int add(std::shared_ptr<WithId>);
135 static void release(int); 142 static void release(int);
136 static std::shared_ptr<WithId> get(int); 143 static std::shared_ptr<WithId> get(int);
137 144
138 template <typename Derived> 145 template <typename Derived>
139 static bool isa(int id) { 146 static bool isa(int id) {
155 template <typename Item, typename Id> 162 template <typename Item, typename Id>
156 class TypedById 163 class TypedById
157 { 164 {
158 public: 165 public:
159 static Id add(std::shared_ptr<Item> item) { 166 static Id add(std::shared_ptr<Item> item) {
160 auto id = item->getId(); 167 Id id;
161 if (id.isNone()) { 168 id.untyped = AnyById::add(item);
162 throw std::logic_error("item id should never be None");
163 }
164 AnyById::add(id.untyped, item);
165 return id; 169 return id;
166 } 170 }
167 171
168 static void release(Id id) { 172 static void release(Id id) {
169 AnyById::release(id.untyped); 173 AnyById::release(id.untyped);
183 } 187 }
184 188
185 static std::shared_ptr<Item> get(Id id) { 189 static std::shared_ptr<Item> get(Id id) {
186 return getAs<Item>(id); 190 return getAs<Item>(id);
187 } 191 }
188 192
189 /** 193 /**
190 * If the Item type is an XmlExportable, return the export ID of 194 * If the Item type is an XmlExportable, return the export ID of
191 * the given item ID. A call to this function will fail to compile 195 * the given item ID. A call to this function will fail to compile
192 * if the Item is not an XmlExportable. 196 * if the Item is not an XmlExportable.
193 * 197 *