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 
34 namespace 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
armarx::aron::BOSubObjectFinder
Finds aron objects with a given type name prefix in aron variants and returns them as BOs.
Definition: object_finders.h:83
armarx::aron::SubObjectFinder::visitUnknown
void visitUnknown(DataInput &elementData, TypeInput &elementType) override
Definition: object_finders.cpp:49
armarx::aron::BOSubObjectFinder::visitUnknown
void visitUnknown(DataInput &elementData, TypeInput &elementType) override
Definition: object_finders.h:116
armarx::aron::data::RecursiveTypedVisitor< const data::VariantPtr, const type::VariantPtr >::MapElements
std::map< std::string, std::pair< DataInputNonConst, TypeInputNonConst > > MapElements
Definition: RecursiveVisitor.h:86
armarx::aron::BOSubObjectFinder::BOSubObjectFinder
BOSubObjectFinder()=default
armarx::aron::data::detail::SpecializedVariantBase< data::dto::Dict, Dict >::DynamicCastAndCheck
static PointerType DynamicCastAndCheck(const VariantPtr &n)
Definition: SpecializedVariant.h:135
aron_conversions.h
Dict.h
armarx::aron::SubObjectFinder::getObjectElements
MapElements getObjectElements(DataInput &elementData, TypeInput &elementType) override
Definition: object_finders.cpp:55
armarx::aron::data::RecursiveTypedVisitor< const data::VariantPtr, const type::VariantPtr >::TypeInput
typename TypedVisitorBase< const data::VariantPtr, const type::VariantPtr >::TypeInput TypeInput
Definition: RecursiveVisitor.h:81
armarx::aron
Definition: DataDisplayVisitor.cpp:5
armarx::aron::SubObjectFinder::getFoundObjects
const std::map< std::string, std::pair< aron::data::VariantPtr, aron::type::VariantPtr > > & getFoundObjects()
Get the objects that have been found.
Definition: object_finders.cpp:33
armarx::aron::SubObjectFinder
Finds aron objects with a given type name prefix in aron variants.
Definition: object_finders.h:43
armarx::aron::BOSubObjectFinder::getFoundObjects
const std::map< std::string, BOType > & getFoundObjects()
Get the objects that have been found.
Definition: object_finders.h:97
armarx::aron::BOSubObjectFinder::visitObjectOnEnter
void visitObjectOnEnter(DataInput &elementData, TypeInput &elementType) override
Definition: object_finders.h:103
armarx::aron::data::RecursiveTypedVisitor< const data::VariantPtr, const type::VariantPtr >::DataInput
typename TypedVisitorBase< const data::VariantPtr, const type::VariantPtr >::DataInput DataInput
Definition: RecursiveVisitor.h:80
armarx::aron::BOSubObjectFinder::~BOSubObjectFinder
~BOSubObjectFinder() override=default
armarx::aron::SubObjectFinder::visitObjectOnEnter
void visitObjectOnEnter(DataInput &elementData, TypeInput &elementType) override
Definition: object_finders.cpp:39
VariantVisitor.h
armarx::aron::SubObjectFinder::SubObjectFinder
SubObjectFinder(const std::string &typeNamePrefix)
Definition: object_finders.cpp:27
armarx::aron::SubObjectFinder::~SubObjectFinder
~SubObjectFinder() override=default
armarx::aron::data::RecursiveConstTypedVariantVisitor
Definition: VariantVisitor.h:192
armarx::aron::BOSubObjectFinder::getObjectElements
MapElements getObjectElements(DataInput &elementData, TypeInput &elementType) override
Definition: object_finders.h:122