From 34a48c8bcbfc4d8e9f66fcb10c7ca5bf4ca5f841 Mon Sep 17 00:00:00 2001
From: Rainer Kartmann <rainer.kartmann@kit.edu>
Date: Mon, 16 Aug 2021 09:48:39 +0200
Subject: [PATCH] Fix names being empty when using move-add()

---
 .../armem/core/base/CoreSegmentBase.h         |  3 +-
 .../libraries/armem/core/base/EntityBase.h    | 29 ++++++++++++-------
 .../libraries/armem/core/base/MemoryBase.h    |  3 +-
 .../armem/core/base/ProviderSegmentBase.h     |  6 ++--
 4 files changed, 24 insertions(+), 17 deletions(-)

diff --git a/source/RobotAPI/libraries/armem/core/base/CoreSegmentBase.h b/source/RobotAPI/libraries/armem/core/base/CoreSegmentBase.h
index aa3a1c903..f4d0b530f 100644
--- a/source/RobotAPI/libraries/armem/core/base/CoreSegmentBase.h
+++ b/source/RobotAPI/libraries/armem/core/base/CoreSegmentBase.h
@@ -284,7 +284,8 @@ namespace armarx::armem::base
         /// Move and insert a provider segment.
         ProviderSegmentT& addProviderSegment(ProviderSegmentT&& providerSegment)
         {
-            return this->_derived().addProviderSegment(providerSegment.name(), std::move(providerSegment));
+            const std::string name = providerSegment.name();  // Copy before move.
+            return this->_derived().addProviderSegment(name, std::move(providerSegment));
         }
 
         /// Insert a provider segment in-place.
diff --git a/source/RobotAPI/libraries/armem/core/base/EntityBase.h b/source/RobotAPI/libraries/armem/core/base/EntityBase.h
index ce366164f..ca9fdd8d6 100644
--- a/source/RobotAPI/libraries/armem/core/base/EntityBase.h
+++ b/source/RobotAPI/libraries/armem/core/base/EntityBase.h
@@ -529,26 +529,33 @@ namespace armarx::armem::base
             });
         }
 
-        /**
-         * @brief Add a single snapshot with data.
-         * @param snapshot The snapshot.
-         * @return The stored snapshot.
-         */
+
+        /// Add a snapshot at the given time.
+        EntitySnapshotT& addSnapshot(const Time& timestamp)
+        {
+            return this->addSnapshot(timestamp, EntitySnapshotT(timestamp));
+        }
+
+        /// Copy and insert a snapshot
         EntitySnapshotT& addSnapshot(const EntitySnapshotT& snapshot)
         {
-            return addSnapshot(EntitySnapshotT(snapshot));
+            return this->addSnapshot(snapshot.time(), EntitySnapshotT(snapshot));
         }
 
+        /// Move and insert a snapshot
         EntitySnapshotT& addSnapshot(EntitySnapshotT&& snapshot)
         {
-            auto it = this->_container.emplace_hint(this->_container.end(), snapshot.time(), std::move(snapshot));
-            it->second.id().setEntityID(this->id());
-            return it->second;
+            Time timestamp = snapshot.time();  // Copy before move.
+            return this->addSnapshot(timestamp, std::move(snapshot));
         }
 
-        EntitySnapshotT& addSnapshot(const Time& timestamp)
+        /// Insert a snapshot in-place.
+        template <class ...Args>
+        EntitySnapshotT& addSnapshot(const Time& timestamp, Args... args)
         {
-            return addSnapshot(EntitySnapshotT(timestamp, this->id()));
+            auto it = this->_container.emplace_hint(this->_container.end(), timestamp, args...);
+            it->second.id() = this->id().withTimestamp(timestamp);
+            return it->second;
         }
 
 
diff --git a/source/RobotAPI/libraries/armem/core/base/MemoryBase.h b/source/RobotAPI/libraries/armem/core/base/MemoryBase.h
index e73db7d84..e1729bbcd 100644
--- a/source/RobotAPI/libraries/armem/core/base/MemoryBase.h
+++ b/source/RobotAPI/libraries/armem/core/base/MemoryBase.h
@@ -223,7 +223,8 @@ namespace armarx::armem::base
         /// Move and insert a core segment.
         CoreSegmentT& addCoreSegment(CoreSegmentT&& coreSegment)
         {
-            return this->_derived().addCoreSegment(coreSegment.name(), std::move(coreSegment));
+            const std::string name = coreSegment.name();  // Copy before move.
+            return this->_derived().addCoreSegment(name, std::move(coreSegment));
         }
 
         /// Move and insert a core segment.
diff --git a/source/RobotAPI/libraries/armem/core/base/ProviderSegmentBase.h b/source/RobotAPI/libraries/armem/core/base/ProviderSegmentBase.h
index ba9b091a3..6a5497444 100644
--- a/source/RobotAPI/libraries/armem/core/base/ProviderSegmentBase.h
+++ b/source/RobotAPI/libraries/armem/core/base/ProviderSegmentBase.h
@@ -231,8 +231,6 @@ namespace armarx::armem::base
 
         void append(const DerivedT& m)
         {
-            // ARMARX_INFO << "ProviderSegment: Merge name '" << m.name() << "' into '" << name() << "'";
-
             m.forEachEntity([this](const EntityT & entity)
             {
                 auto it = this->_container.find(entity.name());
@@ -261,7 +259,8 @@ namespace armarx::armem::base
         /// Move and insert an entity.
         EntityT& addEntity(EntityT&& entity)
         {
-            return this->_derived().addEntity(entity.name(), std::move(entity));
+            const std::string name = entity.name();  // Copy before move.
+            return this->_derived().addEntity(name, std::move(entity));
         }
 
         /// Insert an entity in-place.
@@ -278,7 +277,6 @@ namespace armarx::armem::base
 
         bool equalsDeep(const DerivedT& other) const
         {
-            //std::cout << "ProviderSegment::equalsDeep" << std::endl;
             if (this->size() != other.size())
             {
                 return false;
-- 
GitLab