Skip to content
Snippets Groups Projects
Commit 04ac72d4 authored by Rainer Kartmann's avatar Rainer Kartmann
Browse files

Added Collector

parent e3469b0e
No related branches found
No related tags found
No related merge requests found
...@@ -329,6 +329,7 @@ SET(SOURCES ...@@ -329,6 +329,7 @@ SET(SOURCES
MJCF/elements/types/statistic.cpp MJCF/elements/types/statistic.cpp
MJCF/elements/types/tendon.cpp MJCF/elements/types/tendon.cpp
MJCF/elements/types/visual.cpp MJCF/elements/types/visual.cpp
MJCF/visitors/Collector.cpp
Nodes/CameraSensor.cpp Nodes/CameraSensor.cpp
Nodes/CameraSensorFactory.cpp Nodes/CameraSensorFactory.cpp
...@@ -538,6 +539,8 @@ SET(INCLUDES ...@@ -538,6 +539,8 @@ SET(INCLUDES
MJCF/elements/types/statistic.h MJCF/elements/types/statistic.h
MJCF/elements/types/tendon.h MJCF/elements/types/tendon.h
MJCF/elements/types/visual.h MJCF/elements/types/visual.h
MJCF/visitors.h
MJCF/visitors/Collector.h
Nodes/CameraSensor.h Nodes/CameraSensor.h
Nodes/CameraSensorFactory.h Nodes/CameraSensorFactory.h
......
#pragma once
#include "visitors/Collector.h"
#include "Collector.h"
namespace mjcf
{
}
#pragma once
#include "../elements.h"
namespace mjcf
{
/**
* @brief A visitor that collects all elements of the specified type.
*/
template <class ElementT>
class Collector : public Visitor
{
public:
static std::vector<ElementT> collect(Document& document, AnyElement root);
Collector(Document& document) : Visitor(document) {}
// Visitor interface
virtual bool visitEnter(const AnyElement& element) override
{
if (element.is<ElementT>())
{
collected.push_back(element.as<ElementT>());
}
return true;
}
std::vector<ElementT>& getCollected() { return collected; }
const std::vector<ElementT>& getCollected() const { return collected; }
private:
std::vector<ElementT> collected;
};
template <class ElementT>
std::vector<ElementT> Collector<ElementT>::collect(Document& document, AnyElement root)
{
mjcf::Collector<ElementT> collector(document);
root.accept(collector);
return collector.getCollected();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment