changeset 1750:d0ef65d8dd89 by-id

Return id from add(), + comment
author Chris Cannam
date Thu, 04 Jul 2019 14:30:48 +0100
parents 356be36b2391
children 77543124651b
files base/ById.h base/XmlExportable.h
diffstat 2 files changed, 17 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/base/ById.h	Thu Jul 04 14:30:41 2019 +0100
+++ b/base/ById.h	Thu Jul 04 14:30:48 2019 +0100
@@ -29,6 +29,18 @@
 
 //!!! todo: docs
 
+//!!! further possibilities:
+//
+//    - id can only be queried when an object is added to ById (maybe
+//      add() returns the id, and there is a ById::idOf, but the
+//      object has no public getId())
+//
+//    - get() returns a pointer wrapper that cannot be shared/copied
+//      again by the caller (except by the usual C++ trickery)
+//
+// also to do: review how often we are calling getAs<...> when we
+// could just be using get
+
 struct IdAlloc {
 
     // The value NO_ID (-1) is never allocated
@@ -144,12 +156,13 @@
 class TypedById
 {
 public:
-    static void add(std::shared_ptr<Item> item) {
+    static Id add(std::shared_ptr<Item> item) {
         auto id = item->getId();
         if (id.isNone()) {
             throw std::logic_error("item id should never be None");
         }
         AnyById::add(id.untyped, item);
+        return id;
     }
 
     static void release(Id id) {
--- a/base/XmlExportable.h	Thu Jul 04 14:30:41 2019 +0100
+++ b/base/XmlExportable.h	Thu Jul 04 14:30:48 2019 +0100
@@ -30,6 +30,8 @@
         NO_ID = -1
     };
 
+    typedef int ExportId;
+    
     XmlExportable() : m_exportId(NO_ID) { }
     virtual ~XmlExportable() { }
 
@@ -38,7 +40,7 @@
      * allocated the first time this is called, so objects on which
      * this is never called do not get allocated one.
      */
-    int getExportId() const;
+    ExportId getExportId() const;
 
     /**
      * Stream this exportable object out to XML on a text stream.