diff base/ById.cpp @ 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 b92bdcd4954b
children b679bae1627b
line wrap: on
line diff
--- a/base/ById.cpp	Thu Jul 04 18:04:21 2019 +0100
+++ b/base/ById.cpp	Fri Jul 05 15:28:07 2019 +0100
@@ -59,9 +59,10 @@
         }
     }
         
-    void add(int id, std::shared_ptr<WithId> item) {
+    int add(std::shared_ptr<WithId> item) {
+        int id = item->getUntypedId();
         if (id == IdAlloc::NO_ID) {
-            throw std::logic_error("cannot add item with id of NO_ID");
+            throw std::logic_error("item id should never be NO_ID");
         }
         QMutexLocker locker(&m_mutex);
         if (m_items.find(id) != m_items.end()) {
@@ -73,12 +74,14 @@
             throw std::logic_error("item id is already recorded in add");
         }
         m_items[id] = item;
+        return id;
     }
 
     void release(int id) {
         if (id == IdAlloc::NO_ID) {
             return;
         }
+        SVCERR << "ById::release(" << id << ")" << endl;
         QMutexLocker locker(&m_mutex);
         if (m_items.find(id) == m_items.end()) {
             SVCERR << "ById::release: unknown item id " << id << endl;
@@ -105,10 +108,10 @@
     std::unordered_map<int, std::shared_ptr<WithId>> m_items;
 };
 
-void
-AnyById::add(int id, std::shared_ptr<WithId> item)
+int
+AnyById::add(std::shared_ptr<WithId> item)
 {
-    impl().add(id, item);
+    return impl().add(item);
 }
 
 void