From ff6abd9559d307fe2eda11c2e19499c0facadd6c Mon Sep 17 00:00:00 2001
From: Fabian Reister <fabian.reister@kit.edu>
Date: Thu, 27 May 2021 08:23:04 +0200
Subject: [PATCH] ArViz: scoped client that automatically cleans up layers when
 destroyed

---
 .../RobotAPI/components/ArViz/CMakeLists.txt  |  2 +
 .../RobotAPI/components/ArViz/Client/Client.h |  2 +-
 .../components/ArViz/Client/ScopedClient.cpp  | 45 +++++++++++++++++++
 .../components/ArViz/Client/ScopedClient.h    | 44 ++++++++++++++++++
 4 files changed, 92 insertions(+), 1 deletion(-)
 create mode 100644 source/RobotAPI/components/ArViz/Client/ScopedClient.cpp
 create mode 100644 source/RobotAPI/components/ArViz/Client/ScopedClient.h

diff --git a/source/RobotAPI/components/ArViz/CMakeLists.txt b/source/RobotAPI/components/ArViz/CMakeLists.txt
index d79d9a4ba..23bc56875 100644
--- a/source/RobotAPI/components/ArViz/CMakeLists.txt
+++ b/source/RobotAPI/components/ArViz/CMakeLists.txt
@@ -16,6 +16,7 @@ set(SOURCES
     Client/elements/Robot.cpp
     Client/elements/RobotHand.cpp
     Client/drawer/ArVizDrawerBase.cpp
+    Client/ScopedClient.cpp
 
     Coin/ElementVisualizer.cpp
 
@@ -65,6 +66,7 @@ set(HEADERS
     Client/Layer.h
     Client/Elements.h
     Client/Client.h
+    Client/ScopedClient.h
     Client/ClientCGALExtensions.h
     Client/Color.h
 
diff --git a/source/RobotAPI/components/ArViz/Client/Client.h b/source/RobotAPI/components/ArViz/Client/Client.h
index cc8e7289a..71e4197cc 100644
--- a/source/RobotAPI/components/ArViz/Client/Client.h
+++ b/source/RobotAPI/components/ArViz/Client/Client.h
@@ -51,7 +51,7 @@ namespace armarx::viz
 
         // ////////////////////////////////////////////////////////////////// //
         //layer
-        Layer layer(std::string const& name) const
+        virtual Layer layer(std::string const& name) const
         {
             return Layer(componentName, name);
         }
diff --git a/source/RobotAPI/components/ArViz/Client/ScopedClient.cpp b/source/RobotAPI/components/ArViz/Client/ScopedClient.cpp
new file mode 100644
index 000000000..f3347c8c8
--- /dev/null
+++ b/source/RobotAPI/components/ArViz/Client/ScopedClient.cpp
@@ -0,0 +1,45 @@
+/*
+ * This file is part of ArmarX.
+ *
+ * ArmarX is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * ArmarX is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author     Fabian Reister ( fabian dot reister at kit dot edu )
+ * @date       2021
+ * @copyright  http://www.gnu.org/licenses/gpl-2.0.txt
+ *             GNU General Public License
+ */
+
+
+#include "ScopedClient.h"
+
+namespace armarx::viz
+{
+
+    ScopedClient::ScopedClient(const Client& client) : Client(client) {}
+
+    Layer ScopedClient::layer(std::string const& name) const
+    {
+        layers.insert(name);
+
+        return Client::layer(name);
+    }
+
+    virtual ScopedClient::~ScopedClient()
+    {
+        for (const auto& layer : layers)
+        {
+            Client::commitDeleteLayer(layer);
+        }
+    }
+
+}  // namespace armarx::viz
\ No newline at end of file
diff --git a/source/RobotAPI/components/ArViz/Client/ScopedClient.h b/source/RobotAPI/components/ArViz/Client/ScopedClient.h
new file mode 100644
index 000000000..e4ee71792
--- /dev/null
+++ b/source/RobotAPI/components/ArViz/Client/ScopedClient.h
@@ -0,0 +1,44 @@
+/*
+ * This file is part of ArmarX.
+ *
+ * ArmarX is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * ArmarX is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author     Fabian Reister ( fabian dot reister at kit dot edu )
+ * @date       2021
+ * @copyright  http://www.gnu.org/licenses/gpl-2.0.txt
+ *             GNU General Public License
+ */
+
+#include <set>
+
+#include "Client.h"
+
+namespace armarx::viz
+{
+    /**
+     * @brief viz::Client that will cleanup layers when destroyed.
+     *
+     */
+    class ScopedClient: virtual public Client
+    {
+    public:
+        ScopedClient(const Client& client);
+
+        Layer layer(std::string const& name) const override;
+
+        virtual ~ScopedClient();
+
+    private:
+        mutable std::set<std::string> layers;
+    };
+}  // namespace armarx::viz
\ No newline at end of file
-- 
GitLab