From 0e6a013f22e198b34f05124bfe20900a21b62418 Mon Sep 17 00:00:00 2001
From: Rainer Kartmann <rainer.kartmann@kit.edu>
Date: Tue, 14 Jun 2022 11:37:41 +0200
Subject: [PATCH] Unify overloads of _getAllClients()

---
 .../armem/client/MemoryNameSystem.cpp         | 49 ++++++++++---------
 .../libraries/armem/client/MemoryNameSystem.h |  5 +-
 2 files changed, 30 insertions(+), 24 deletions(-)

diff --git a/source/RobotAPI/libraries/armem/client/MemoryNameSystem.cpp b/source/RobotAPI/libraries/armem/client/MemoryNameSystem.cpp
index 60a39400d..6440155ce 100644
--- a/source/RobotAPI/libraries/armem/client/MemoryNameSystem.cpp
+++ b/source/RobotAPI/libraries/armem/client/MemoryNameSystem.cpp
@@ -207,40 +207,44 @@ namespace armarx::armem::client
 
     template <class ClientT>
     std::map<std::string, ClientT>
-    MemoryNameSystem::_getAllClients(auto&& getProxyFn) const
+    MemoryNameSystem::_getAllClients(ClientFactory<ClientT>&& factory) const
     {
         std::map<std::string, ClientT> result;
         for (const auto& [name, server] : servers)
         {
-            if (auto proxy = getProxyFn(server))
+            if (std::optional<ClientT> client = factory(server))
             {
-                result[name] = ClientT(proxy);
+                result[name] = client.value();
             }
         }
         return result;
     }
 
 
-    template <class ClientT>
-    std::map<std::string, ClientT>
-    MemoryNameSystem::_getAllClients(auto&& getProxyFn, auto&& secondProxyFn) const
+    std::optional<Reader> readerFactory(const mns::dto::MemoryServerInterfaces& server)
     {
-        std::map<std::string, ClientT> result;
-        for (const auto& [name, server] : servers)
+        if (auto read = server.reading)
         {
-            if (auto proxy = getProxyFn(server))
+            if (auto predict = server.prediction)
             {
-                if (auto secondProxy = secondProxyFn(server))
-                {
-                    result[name] = ClientT(proxy, secondProxy);
-                }
-                else
-                {
-                    result[name] = ClientT(proxy);
-                }
+                return Reader(read, predict);
+            }
+            else
+            {
+                return Reader(read);
             }
         }
-        return result;
+        return std::nullopt;
+    }
+
+
+    std::optional<Writer> writerFactory(const mns::dto::MemoryServerInterfaces& server)
+    {
+        if (auto write = server.writing)
+        {
+            return Writer(write);
+        }
+        return std::nullopt;
     }
 
 
@@ -250,13 +254,14 @@ namespace armarx::armem::client
         {
             this->update();
         }
-        return _getAllClients<Reader>(&mns::getReadingInterface, &mns::getPredictionInterface);
+
+        return _getAllClients<Reader>(readerFactory);
     }
 
 
     std::map<std::string, Reader> MemoryNameSystem::getAllReaders() const
     {
-        return _getAllClients<Reader>(&mns::getReadingInterface, &mns::getPredictionInterface);
+        return _getAllClients<Reader>(readerFactory);
     }
 
 
@@ -296,13 +301,13 @@ namespace armarx::armem::client
         {
             this->update();
         }
-        return _getAllClients<Writer>(&mns::getWritingInterface);
+        return _getAllClients<Writer>(writerFactory);
     }
 
 
     std::map<std::string, Writer> MemoryNameSystem::getAllWriters() const
     {
-        return _getAllClients<Writer>(&mns::getWritingInterface);
+        return _getAllClients<Writer>(writerFactory);
     }
 
 
diff --git a/source/RobotAPI/libraries/armem/client/MemoryNameSystem.h b/source/RobotAPI/libraries/armem/client/MemoryNameSystem.h
index b5a0fe2ae..50e90444e 100644
--- a/source/RobotAPI/libraries/armem/client/MemoryNameSystem.h
+++ b/source/RobotAPI/libraries/armem/client/MemoryNameSystem.h
@@ -251,11 +251,12 @@ namespace armarx::armem::client
 
 
     private:
+
         template <class ClientT>
-        std::map<std::string, ClientT> _getAllClients(auto&& proxyFn) const;
+        using ClientFactory = std::function<std::optional<ClientT>(const mns::dto::MemoryServerInterfaces& server)>;
 
         template <class ClientT>
-        std::map<std::string, ClientT> _getAllClients(auto&& proxyFn, auto&& secondProxyFn) const;
+        std::map<std::string, ClientT> _getAllClients(ClientFactory<ClientT>&& factory) const;
 
 
         /// The MNS proxy.
-- 
GitLab