SpecializedVariant.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T),
5 * Karlsruhe Institute of Technology (KIT), all rights reserved.
6 *
7 * ArmarX is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * ArmarX is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * @author Fabian Peller-Konrad (fabian dot peller-konrad at kit dot edu)
20 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21 * GNU General Public License
22 */
23
24#pragma once
25
26// STD/STL
27#include <memory>
28#include <optional>
29
30// Base class
31#include "../Variant.h"
32
33// ArmarX
35
37{
38 template <typename AronDataT, typename DerivedT>
40 {
41 public:
43 using PointerType = std::shared_ptr<DerivedT>;
44 using AronDataType = AronDataT;
45
46 public:
53
54 SpecializedVariantBase(const typename AronDataType::PointerType& o,
56 const Path& path = Path()) :
58 {
60 }
61
62 virtual ~SpecializedVariantBase() = default;
63
64 // operators
65 operator typename AronDataType::PointerType()
66 {
67 return aron;
68 }
69
70 bool
71 operator==(const Variant& other) const override
72 {
73 const auto& n = DerivedT::DynamicCast(other);
74 return *this == n;
75 }
76
77 virtual bool operator==(const DerivedT&) const = 0;
78 virtual bool operator==(const PointerType& other) const = 0;
79
80 // new interface methods
81 virtual PointerType
82 clone() const
83 {
84 return clone(getPath());
85 }
86
87 virtual PointerType clone(const Path& newPath) const = 0;
88
89 // virtual implementations
91 cloneAsVariant() const override
92 {
93 return clone();
94 }
95
97 cloneAsVariant(const Path& newPath) const override
98 {
99 return clone(newPath);
100 }
101
102 data::dto::GenericDataPtr
103 toAronDTO() const override
104 {
105 return aron;
106 }
107
108 void
109 setImportance(const std::optional<float>& importance) override
110 {
111 if (!importance.has_value())
112 {
113 aron->importance = IceUtil::None;
114 return;
115 }
116
117 ARMARX_CHECK_GREATER_EQUAL(*importance, 0.f);
118 ARMARX_CHECK_LESS_EQUAL(*importance, 1.f);
119 aron->importance = *importance;
120 }
121
122 std::optional<float>
123 getImportance() const override
124 {
125 const auto importance = aron->importance;
126 if (!importance)
127 {
128 return std::nullopt;
129 }
130 return *importance;
131 }
132
134 navigateRelative(const Path& path) const override
135 {
136 Path absoluteFromHere = path.getWithoutPrefix(getPath());
137 return navigateAbsolute(absoluteFromHere);
138 }
139
140 // static methods
141 static PointerType
143 {
144 return std::dynamic_pointer_cast<DerivedT>(n);
145 }
146
147 static DerivedT&
149 {
150 return dynamic_cast<DerivedT&>(n);
151 }
152
153 static const DerivedT&
155 {
156 return dynamic_cast<const DerivedT&>(n);
157 }
158
159 static PointerType
161 {
162 if (!n)
163 {
164 return nullptr;
165 }
166
167 PointerType casted = DerivedT::DynamicCast(n);
169 << "The path was: " << n->getPath().toString() << ".\n"
170 << "It has the descriptor: '"
172 << "'.\n"
173 << "And the typeid: " << typeid(n).name() << ". Tried to cast to "
174 << typeid(PointerType).name();
175 return casted;
176 }
177
178 protected:
179 typename AronDataType::PointerType aron;
180 };
181} // namespace armarx::aron::data::detail
The Path class.
Definition Path.h:36
The Variant class.
Definition Variant.h:62
const data::Descriptor descriptor
Definition Variant.h:166
virtual VariantPtr navigateAbsolute(const Path &path) const =0
naviate absolute
Path getPath() const
get the path
Definition Variant.h:111
data::dto::GenericDataPtr toAronDTO() const override
convert the variant to the ice representation
static const DerivedT & DynamicCast(const Variant &n)
SpecializedVariantBase(const typename AronDataType::PointerType &o, const data::Descriptor descriptor, const Path &path=Path())
virtual bool operator==(const DerivedT &) const =0
bool operator==(const Variant &other) const override
VariantPtr navigateRelative(const Path &path) const override
navigate relative
SpecializedVariantBase(const data::Descriptor descriptor, const Path &path=Path())
std::optional< float > getImportance() const override
get optional importance metadata for this data variant.
void setImportance(const std::optional< float > &importance) override
set optional importance metadata in [0, 1] for this data variant.
static PointerType DynamicCastAndCheck(const VariantPtr &n)
static PointerType DynamicCast(const VariantPtr &n)
VariantPtr cloneAsVariant(const Path &newPath) const override
virtual PointerType clone(const Path &newPath) const =0
virtual bool operator==(const PointerType &other) const =0
VariantPtr cloneAsVariant() const override
get a pointer to a copy of this variant
#define ARMARX_CHECK_LESS_EQUAL(lhs, rhs)
This macro evaluates whether lhs is less or equal (<=) rhs and if it turns out to be false it will th...
#define ARMARX_CHECK_GREATER_EQUAL(lhs, rhs)
This macro evaluates whether lhs is greater or equal (>=) rhs and if it turns out to be false it will...
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
const std::map< data::Descriptor, std::string > Descriptor2String
Definition Descriptor.h:207
std::shared_ptr< Variant > VariantPtr