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 #include <string>
30 #include <unordered_map>
31 
32 // Base class
33 #include "../Variant.h"
34 
35 // ArmarX
36 
38 {
39  template <typename AronTypeT, typename DerivedT>
41  {
42 
43  public:
45  Variant(descriptor, path), aron(new AronTypeT())
46  {
47  }
48 
49  SpecializedVariantBase(const AronTypeT& o,
51  const Path& path) :
52  Variant(descriptor, path), aron(new AronTypeT(o))
53  {
54  }
55 
56  virtual ~SpecializedVariantBase() = default;
57 
58  // operators
59  operator AronTypeT()
60  {
61  return *aron;
62  }
63 
64  bool
65  operator==(const Variant& other) const override
66  {
67  const auto& n = dynamic_cast<const DerivedT&>(other);
68  return *this == n;
69  }
70 
71  bool
72  operator==(const DerivedT& other) const
73  {
74  return *(this->aron) == *(other.aron);
75  }
76 
77  // virtual implementations
78  type::dto::GenericTypePtr
79  toAronDTO() const override
80  {
82  return aron;
83  }
84 
85  void
86  setMaybe(const type::Maybe m) override
87  {
88  aron->maybe = m;
89  }
90 
91  type::Maybe
92  getMaybe() const override
93  {
94  return aron->maybe;
95  }
96 
98  navigateRelative(const Path& path) const override
99  {
100  Path absoluteFromHere = path.getWithoutPrefix(getPath());
101  return navigateAbsolute(absoluteFromHere);
102  }
103 
104  // static methods
105  static DerivedT&
107  {
108  return dynamic_cast<DerivedT&>(n);
109  }
110 
111  static const DerivedT&
113  {
114  return dynamic_cast<const DerivedT&>(n);
115  }
116 
117  static std::shared_ptr<DerivedT>
119  {
120  return std::dynamic_pointer_cast<DerivedT>(n);
121  }
122 
123  static std::shared_ptr<DerivedT>
125  {
126  if (!n)
127  {
128  return nullptr;
129  }
130 
131  auto casted = std::dynamic_pointer_cast<DerivedT>(n);
132  ARMARX_CHECK_NOT_NULL(casted);
133  return casted;
134  }
135 
136  protected:
137  typename AronTypeT::PointerType aron;
138  };
139 } // namespace armarx::aron::type::detail
armarx::aron::type::detail::SpecializedVariantBase::toAronDTO
type::dto::GenericTypePtr toAronDTO() const override
convert this variant to a dto object.
Definition: SpecializedVariant.h:79
armarx::aron::type::VariantPtr
std::shared_ptr< Variant > VariantPtr
Definition: forward_declarations.h:11
armarx::aron::type::detail
Definition: AnyVariant.h:36
armarx::aron::type::detail::SpecializedVariantBase::getMaybe
type::Maybe getMaybe() const override
get the maybe type
Definition: SpecializedVariant.h:92
ARMARX_CHECK_NOT_NULL
#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...
Definition: ExpressionException.h:206
armarx::aron::type::detail::SpecializedVariantBase::operator==
bool operator==(const Variant &other) const override
Definition: SpecializedVariant.h:65
armarx::aron::type::detail::SpecializedVariantBase::navigateRelative
VariantPtr navigateRelative(const Path &path) const override
navigate relative
Definition: SpecializedVariant.h:98
armarx::aron::type::Variant::navigateAbsolute
virtual VariantPtr navigateAbsolute(const Path &path) const =0
naviate absolute
armarx::aron::type::detail::SpecializedVariantBase::operator==
bool operator==(const DerivedT &other) const
Definition: SpecializedVariant.h:72
armarx::aron::type::detail::SpecializedVariantBase
Definition: SpecializedVariant.h:40
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::aron::type::Variant
The Variant class.
Definition: Variant.h:59
armarx::aron::type::detail::SpecializedVariantBase::setMaybe
void setMaybe(const type::Maybe m) override
set the maybetype of this type
Definition: SpecializedVariant.h:86
armarx::aron::type::detail::SpecializedVariantBase::SpecializedVariantBase
SpecializedVariantBase(const AronTypeT &o, const type::Descriptor &descriptor, const Path &path)
Definition: SpecializedVariant.h:49
armarx::aron::type::detail::SpecializedVariantBase::DynamicCast
static DerivedT & DynamicCast(Variant &n)
Definition: SpecializedVariant.h:106
armarx::aron::type::detail::SpecializedVariantBase::aron
AronTypeT::PointerType aron
Definition: SpecializedVariant.h:137
armarx::aron::type::detail::SpecializedVariantBase::DynamicCastAndCheck
static std::shared_ptr< DerivedT > DynamicCastAndCheck(const VariantPtr &n)
Definition: SpecializedVariant.h:124
armarx::aron::type::Variant::path
const Path path
Definition: Variant.h:139
armarx::aron::type::Descriptor
Descriptor
Definition: Descriptor.h:76
armarx::aron::Path::getWithoutPrefix
Path getWithoutPrefix(const Path &) const
Definition: Path.cpp:216
armarx::aron::type::detail::SpecializedVariantBase::DynamicCast
static std::shared_ptr< DerivedT > DynamicCast(const VariantPtr &n)
Definition: SpecializedVariant.h:118
armarx::aron::type::detail::SpecializedVariantBase::~SpecializedVariantBase
virtual ~SpecializedVariantBase()=default
armarx::aron::type::detail::SpecializedVariantBase::SpecializedVariantBase
SpecializedVariantBase(const type::Descriptor &descriptor, const Path &path)
Definition: SpecializedVariant.h:44
armarx::aron::type::Variant::descriptor
const type::Descriptor descriptor
Definition: Variant.h:138
armarx::aron::type::detail::SpecializedVariantBase::DynamicCast
static const DerivedT & DynamicCast(const Variant &n)
Definition: SpecializedVariant.h:112
armarx::aron::type::Variant::getPath
Path getPath() const
Definition: Variant.h:99