object_finders.h
Go to the documentation of this file.
1/**
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package RobotAPI::libraries::aron
17 * @author Philip Scherer ( ulila@student.kit.edu )
18 * @date 2021
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23#pragma once
24
25#include <map>
26#include <vector>
27
28#include <SimoxUtility/algorithm/string.h>
29
33
34namespace armarx::aron
35{
36 /**
37 * Finds aron objects with a given type name prefix in aron variants.
38 *
39 * Construct it with the prefix you want to search for,
40 * then use `visitRecursive` to run the finder over the variant to search.
41 * Get a map of paths to data and type of the found objects using `getFoundObjects`.
42 */
44 {
45 public:
46 SubObjectFinder(const std::string& typeNamePrefix);
47
48 ~SubObjectFinder() override = default;
49
50 /**
51 * Get the objects that have been found.
52 *
53 * The keys are strings instead of `aron::Path`s because those don't support comparisons.
54 *
55 * @return map of paths to pair of data and type variants
56 */
57 const std::map<std::string, std::pair<aron::data::VariantPtr, aron::type::VariantPtr>>&
59
60 void visitObjectOnEnter(DataInput& elementData, TypeInput& elementType) override;
61
62 void visitUnknown(DataInput& elementData, TypeInput& elementType) override;
63
64 MapElements getObjectElements(DataInput& elementData, TypeInput& elementType) override;
65
66 private:
67 std::string typeNamePrefix;
68 std::map<std::string, std::pair<aron::data::VariantPtr, aron::type::VariantPtr>>
69 foundSubObjects;
70 };
71
72 /**
73 * Finds aron objects with a given type name prefix in aron variants and returns them as BOs.
74 *
75 * Construct it with the prefix you want to search for,
76 * then use `visitRecursive` to run the finder over the variant to search.
77 * Get a map of paths to your BOs using `getFoundObjects`.
78 *
79 * \tparam DTOType the _generated_ aron type of your data.
80 * \tparam BOType the type of your final BO.
81 */
82 template <typename DTOType, typename BOType>
84 {
85 public:
86 BOSubObjectFinder() = default;
87 ~BOSubObjectFinder() override = default;
88
89 /**
90 * Get the objects that have been found.
91 *
92 * The keys are strings instead of `aron::Path`s because those don't support comparisons.
93 *
94 * @return map of paths to BOs.
95 */
96 const std::map<std::string, BOType>&
98 {
99 return foundSubObjects;
100 }
101
102 void
103 visitObjectOnEnter(DataInput& elementData, TypeInput& elementType) override
104 {
105 if (elementType->getFullName() == DTOType::ToAronType()->getFullName())
106 {
107 auto dict = armarx::aron::data::Dict::DynamicCastAndCheck(elementData);
108 DTOType dto;
109 dto.fromAron(dict);
110 BOType boObj = armarx::aron::fromAron<BOType, DTOType>(dto);
111 foundSubObjects.emplace(elementData->getPath().toString(), boObj);
112 }
113 }
114
115 void
116 visitUnknown(DataInput& elementData, TypeInput& elementType) override
117 {
118 // Ignore (the base class throws an exception here)
119 }
120
122 getObjectElements(DataInput& elementData, TypeInput& elementType) override
123 {
124 return RecursiveConstTypedVariantVisitor::GetObjectElementsWithNullType(elementData,
125 elementType);
126 }
127
128 private:
129 std::map<std::string, BOType> foundSubObjects;
130 };
131
132} // namespace armarx::aron
MapElements getObjectElements(DataInput &elementData, TypeInput &elementType) override
const std::map< std::string, BOType > & getFoundObjects()
Get the objects that have been found.
~BOSubObjectFinder() override=default
void visitUnknown(DataInput &elementData, TypeInput &elementType) override
void visitObjectOnEnter(DataInput &elementData, TypeInput &elementType) override
const std::map< std::string, std::pair< aron::data::VariantPtr, aron::type::VariantPtr > > & getFoundObjects()
Get the objects that have been found.
MapElements getObjectElements(DataInput &elementData, TypeInput &elementType) override
~SubObjectFinder() override=default
SubObjectFinder(const std::string &typeNamePrefix)
void visitUnknown(DataInput &elementData, TypeInput &elementType) override
void visitObjectOnEnter(DataInput &elementData, TypeInput &elementType) override
void fromAron(const T &dto, T &bo)
typename TypedVisitorBase< const data::VariantPtr, const type::VariantPtr >::TypeInput TypeInput
typename TypedVisitorBase< const data::VariantPtr, const type::VariantPtr >::DataInput DataInput
std::map< std::string, std::pair< DataInputNonConst, TypeInputNonConst > > MapElements