From fd291109be0ff5bfbac71fa89d29c2b8a528ec95 Mon Sep 17 00:00:00 2001
From: Rainer Kartmann <rainer.kartmann@kit.edu>
Date: Wed, 21 Apr 2021 17:49:42 +0200
Subject: [PATCH] Add armem::Visitor

---
 .../RobotAPI/libraries/armem/CMakeLists.txt   |   2 +
 .../RobotAPI/libraries/armem/core/Visitor.cpp | 151 ++++++++++++++++++
 .../RobotAPI/libraries/armem/core/Visitor.h   | 151 ++++++++++++++++++
 3 files changed, 304 insertions(+)
 create mode 100644 source/RobotAPI/libraries/armem/core/Visitor.cpp
 create mode 100644 source/RobotAPI/libraries/armem/core/Visitor.h

diff --git a/source/RobotAPI/libraries/armem/CMakeLists.txt b/source/RobotAPI/libraries/armem/CMakeLists.txt
index 0d1c51feb..da62a9ff9 100644
--- a/source/RobotAPI/libraries/armem/CMakeLists.txt
+++ b/source/RobotAPI/libraries/armem/CMakeLists.txt
@@ -29,6 +29,7 @@ set(LIB_FILES
     core/ice_conversions.cpp
     core/Memory.cpp
     core/ProviderSegment.cpp
+    core/Visitor.cpp
 
     core/detail/MemoryItem.cpp
     core/detail/MaxHistorySize.cpp
@@ -91,6 +92,7 @@ set(LIB_HEADERS
     core/EntitySnapshot.h
     core/ice_conversions.h
     core/Memory.h
+    core/Visitor.h
     core/ProviderSegment.h
 
     core/detail/EntityContainer.h
diff --git a/source/RobotAPI/libraries/armem/core/Visitor.cpp b/source/RobotAPI/libraries/armem/core/Visitor.cpp
new file mode 100644
index 000000000..8e6417391
--- /dev/null
+++ b/source/RobotAPI/libraries/armem/core/Visitor.cpp
@@ -0,0 +1,151 @@
+#include "Visitor.h"
+
+#include <ArmarXCore/core/exceptions/local/ExpressionException.h>
+
+#include <RobotAPI/libraries/armem/core/Memory.h>
+#include <RobotAPI/libraries/armem/core/error.h>
+
+
+namespace armarx::armem
+{
+
+    Visitor::Visitor()
+    {
+    }
+
+    Visitor::~Visitor()
+    {
+    }
+
+    bool Visitor::applyTo(Memory& memory)
+    {
+        for (auto& [_, coreSeg] : memory)
+        {
+            if (!applyTo(coreSeg))
+            {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    bool Visitor::applyTo(CoreSegment& coreSegment)
+    {
+        for (auto& [_, provSeg] : coreSegment)
+        {
+            if (!applyTo(provSeg))
+            {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    bool Visitor::applyTo(ProviderSegment& providerSegment)
+    {
+        for (auto& [_, entity] : providerSegment)
+        {
+            if (!applyTo(entity))
+            {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    bool Visitor::applyTo(Entity& entity)
+    {
+        for (auto& [_, snapshot] : entity)
+        {
+            if (!applyTo(snapshot))
+            {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    bool Visitor::applyTo(EntitySnapshot& snapshot)
+    {
+        for (auto& instance : snapshot)
+        {
+            if (!applyTo(instance))
+            {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    bool Visitor::applyTo(EntityInstance& instance)
+    {
+        return visit(instance);
+    }
+
+
+    bool Visitor::applyTo(const Memory& memory)
+    {
+        for (const auto& [_, coreSeg] : memory)
+        {
+            if (!applyTo(coreSeg))
+            {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    bool Visitor::applyTo(const CoreSegment& coreSegment)
+    {
+        for (const auto& [_, provSeg] : coreSegment)
+        {
+            if (!applyTo(provSeg))
+            {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    bool Visitor::applyTo(const ProviderSegment& providerSegment)
+    {
+        for (const auto& [_, entity] : providerSegment)
+        {
+            if (!applyTo(entity))
+            {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    bool Visitor::applyTo(const Entity& entity)
+    {
+        for (const auto& [_, snapshot] : entity)
+        {
+            if (!applyTo(snapshot))
+            {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    bool Visitor::applyTo(const EntitySnapshot& snapshot)
+    {
+        for (const auto& instance : snapshot)
+        {
+            if (!applyTo(instance))
+            {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    bool Visitor::applyTo(const EntityInstance& instance)
+    {
+        return visit(instance);
+    }
+
+}
diff --git a/source/RobotAPI/libraries/armem/core/Visitor.h b/source/RobotAPI/libraries/armem/core/Visitor.h
new file mode 100644
index 000000000..e9ec9629d
--- /dev/null
+++ b/source/RobotAPI/libraries/armem/core/Visitor.h
@@ -0,0 +1,151 @@
+#pragma once
+
+namespace armarx::armem
+{
+    class Memory;
+    class CoreSegment;
+    class ProviderSegment;
+    class Entity;
+    class EntitySnapshot;
+    class EntityInstance;
+
+
+    /**
+     * @brief A visitor for the hierarchical Memory data structure.
+     */
+    class Visitor
+    {
+
+    public:
+
+        Visitor();
+        virtual ~Visitor();
+
+        bool applyTo(Memory& memory);
+        bool applyTo(CoreSegment& coreSegment);
+        bool applyTo(ProviderSegment& providerSegment);
+        bool applyTo(Entity& entity);
+        bool applyTo(EntitySnapshot& snapshot);
+        bool applyTo(EntityInstance& instance);
+
+
+        virtual bool visitEnter(Memory& memory)
+        {
+            return visitEnter(const_cast<const Memory&>(memory));
+        }
+        virtual bool visitEnter(CoreSegment& coreSegment)
+        {
+            return visitEnter(const_cast<const CoreSegment&>(coreSegment));
+        }
+        virtual bool visitEnter(ProviderSegment& providerSegment)
+        {
+            return visitEnter(const_cast<const ProviderSegment&>(providerSegment));
+        }
+        virtual bool visitEnter(Entity& entity)
+        {
+            return visitEnter(const_cast<const Entity&>(entity));
+        }
+        virtual bool visitEnter(EntitySnapshot& snapshot)
+        {
+            return visitEnter(const_cast<const EntitySnapshot&>(snapshot));
+        }
+
+        virtual bool visitExit(Memory& memory)
+        {
+            return visitExit(const_cast<const Memory&>(memory));
+        }
+        virtual bool visitExit(CoreSegment& coreSegment)
+        {
+            return visitExit(const_cast<const CoreSegment&>(coreSegment));
+        }
+        virtual bool visitExit(ProviderSegment& providerSegment)
+        {
+            return visitExit(const_cast<const ProviderSegment&>(providerSegment));
+        }
+        virtual bool visitExit(Entity& entity)
+        {
+            return visitExit(const_cast<const Entity&>(entity));
+        }
+        virtual bool visitExit(EntitySnapshot& snapshot)
+        {
+            return visitExit(const_cast<const EntitySnapshot&>(snapshot));
+        }
+
+        virtual bool visit(EntityInstance& instance)
+        {
+            return visit(const_cast<const EntityInstance&>(instance));
+        }
+
+
+
+        // Const versions
+
+        bool applyTo(const Memory& memory);
+        bool applyTo(const CoreSegment& coreSegment);
+        bool applyTo(const ProviderSegment& providerSegment);
+        bool applyTo(const Entity& entity);
+        bool applyTo(const EntitySnapshot& snapshot);
+        bool applyTo(const EntityInstance& instance);
+
+
+        virtual bool visitEnter(const Memory& memory)
+        {
+            (void) memory;
+            return true;
+        }
+        virtual bool visitEnter(const CoreSegment& coreSegment)
+        {
+            (void) coreSegment;
+            return true;
+        }
+        virtual bool visitEnter(const ProviderSegment& providerSegment)
+        {
+            (void) providerSegment;
+            return true;
+        }
+        virtual bool visitEnter(const Entity& entity)
+        {
+            (void) entity;
+            return true;
+        }
+        virtual bool visitEnter(const EntitySnapshot& snapshot)
+        {
+            (void) snapshot;
+            return true;
+        }
+
+        virtual bool visitExit(const Memory& memory)
+        {
+            (void) memory;
+            return true;
+        }
+        virtual bool visitExit(const CoreSegment& coreSegment)
+        {
+            (void) coreSegment;
+            return true;
+        }
+        virtual bool visitExit(const ProviderSegment& providerSegment)
+        {
+            (void) providerSegment;
+            return true;
+        }
+        virtual bool visitExit(const Entity& entity)
+        {
+            (void) entity;
+            return true;
+        }
+        virtual bool visitExit(const EntitySnapshot& snapshot)
+        {
+            (void) snapshot;
+            return true;
+        }
+
+        virtual bool visit(const EntityInstance& instance)
+        {
+            (void) instance;
+            return true;
+        }
+
+    };
+
+}
-- 
GitLab