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#include <memory>
27#include <optional>
28
30
31#include "../Variant.h"
32
34{
35 template <typename AronTypeT, typename DerivedT>
37 {
38
39 public:
41 Variant(descriptor, path), aron(new AronTypeT())
42 {
43 }
44
45 SpecializedVariantBase(const AronTypeT& o,
47 const Path& path) :
48 Variant(descriptor, path), aron(new AronTypeT(o))
49 {
50 }
51
52 virtual ~SpecializedVariantBase() = default;
53
54 // operators
55 operator AronTypeT()
56 {
57 return *aron;
58 }
59
60 bool
61 operator==(const Variant& other) const override
62 {
63 const auto& n = dynamic_cast<const DerivedT&>(other);
64 return *this == n;
65 }
66
67 bool
68 operator==(const DerivedT& other) const
69 {
70 return *(this->aron) == *(other.aron);
71 }
72
73 // virtual implementations
74 type::dto::GenericTypePtr
75 toAronDTO() const override
76 {
78 return aron;
79 }
80
81 void
82 setMaybe(const type::Maybe m) override
83 {
84 aron->maybe = m;
85 }
86
87 type::Maybe
88 getMaybe() const override
89 {
90 return aron->maybe;
91 }
92
93 void
94 setImportance(const std::optional<float>& importance) override
95 {
96 if (!importance.has_value())
97 {
98 aron->importance = IceUtil::None;
99 return;
100 }
101
102 ARMARX_CHECK_GREATER_EQUAL(*importance, 0.f);
103 ARMARX_CHECK_LESS_EQUAL(*importance, 1.f);
104 aron->importance = *importance;
105 }
106
107 std::optional<float>
108 getImportance() const override
109 {
110 const auto importance = aron->importance;
111 if (!importance)
112 {
113 return std::nullopt;
114 }
115 return *importance;
116 }
117
119 navigateRelative(const Path& path) const override
120 {
121 Path absoluteFromHere = path.getWithoutPrefix(getPath());
122 return navigateAbsolute(absoluteFromHere);
123 }
124
125 // static methods
126 static DerivedT&
128 {
129 return dynamic_cast<DerivedT&>(n);
130 }
131
132 static const DerivedT&
134 {
135 return dynamic_cast<const DerivedT&>(n);
136 }
137
138 static std::shared_ptr<DerivedT>
140 {
141 return std::dynamic_pointer_cast<DerivedT>(n);
142 }
143
144 static std::shared_ptr<DerivedT>
146 {
147 if (!n)
148 {
149 return nullptr;
150 }
151
152 auto casted = std::dynamic_pointer_cast<DerivedT>(n);
153 ARMARX_CHECK_NOT_NULL(casted);
154 return casted;
155 }
156
157 protected:
158 typename AronTypeT::PointerType aron;
159 };
160} // namespace armarx::aron::type::detail
The Path class.
Definition Path.h:36
The Variant class.
Definition Variant.h:55
virtual VariantPtr navigateAbsolute(const Path &path) const =0
naviate absolute
Variant(const type::Descriptor &descriptor, const Path &path=Path())
Definition Variant.cpp:45
const type::Descriptor descriptor
Definition Variant.h:119
static const DerivedT & DynamicCast(const Variant &n)
SpecializedVariantBase(const AronTypeT &o, const type::Descriptor &descriptor, const Path &path)
type::Maybe getMaybe() const override
get the maybe type
static std::shared_ptr< DerivedT > DynamicCastAndCheck(const VariantPtr &n)
bool operator==(const Variant &other) const override
VariantPtr navigateRelative(const Path &path) const override
navigate relative
void setMaybe(const type::Maybe m) override
set the maybetype of this type
std::optional< float > getImportance() const override
get optional importance metadata for this type variant.
void setImportance(const std::optional< float > &importance) override
set optional importance metadata in [0, 1] for this type variant.
SpecializedVariantBase(const type::Descriptor &descriptor, const Path &path)
static std::shared_ptr< DerivedT > DynamicCast(const VariantPtr &n)
type::dto::GenericTypePtr toAronDTO() const override
convert this variant to a dto object.
#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...
std::shared_ptr< Variant > VariantPtr