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 <string>
29 #include <unordered_map>
30 
31 // Base class
32 #include "../Variant.h"
33 
34 // 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:
47  SpecializedVariantBase() = delete;
48 
51  {
52  }
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  VariantPtr
109  navigateRelative(const Path& path) const override
110  {
111  Path absoluteFromHere = path.getWithoutPrefix(getPath());
112  return navigateAbsolute(absoluteFromHere);
113  }
114 
115  // static methods
116  static PointerType
118  {
119  return std::dynamic_pointer_cast<DerivedT>(n);
120  }
121 
122  static DerivedT&
124  {
125  return dynamic_cast<DerivedT&>(n);
126  }
127 
128  static const DerivedT&
130  {
131  return dynamic_cast<const DerivedT&>(n);
132  }
133 
134  static PointerType
136  {
137  if (!n)
138  {
139  return nullptr;
140  }
141 
142  PointerType casted = DerivedT::DynamicCast(n);
143  ARMARX_CHECK_NOT_NULL(casted)
144  << "The path was: " << n->getPath().toString() << ".\n"
145  << "It has the descriptor: '"
147  << "'.\n"
148  << "And the typeid: " << typeid(n).name() << ". Tried to cast to "
149  << typeid(PointerType).name();
150  return casted;
151  }
152 
153  protected:
154  typename AronDataType::PointerType aron;
155  };
156 } // namespace armarx::aron::data::detail
armarx::aron::data::Variant::descriptor
const data::Descriptor descriptor
Definition: Variant.h:155
armarx::aron::data::detail::SpecializedVariantBase::navigateRelative
VariantPtr navigateRelative(const Path &path) const override
navigate relative
Definition: SpecializedVariant.h:109
armarx::aron::data::defaultconversion::string::Descriptor2String
const std::map< data::Descriptor, std::string > Descriptor2String
Definition: Descriptor.h:224
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::data::Variant::getPath
Path getPath() const
get the path
Definition: Variant.h:110
armarx::aron::data::Variant::PointerType
VariantPtr PointerType
Definition: Variant.h:63
armarx::aron::data::Variant::path
const Path path
Definition: Variant.h:156
armarx::aron::data::Descriptor
Descriptor
Definition: Descriptor.h:193
armarx::aron::data::detail::SpecializedVariantBase::DynamicCastAndCheck
static PointerType DynamicCastAndCheck(const VariantPtr &n)
Definition: SpecializedVariant.h:135
armarx::aron::data::detail::SpecializedVariantBase::clone
virtual PointerType clone() const
Definition: SpecializedVariant.h:82
armarx::aron::data::Variant::navigateAbsolute
virtual VariantPtr navigateAbsolute(const Path &path) const =0
naviate absolute
armarx::aron::data::detail::SpecializedVariantBase::SpecializedVariantBase
SpecializedVariantBase(const typename AronDataType::PointerType &o, const data::Descriptor descriptor, const Path &path=Path())
Definition: SpecializedVariant.h:54
armarx::aron::data::detail::SpecializedVariantBase::DynamicCast
static PointerType DynamicCast(const VariantPtr &n)
Definition: SpecializedVariant.h:117
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::aron::data::VariantPtr
std::shared_ptr< Variant > VariantPtr
Definition: forward_declarations.h:11
armarx::aron::data::detail
Definition: ComplexVariant.h:34
armarx::aron::data::detail::SpecializedVariantBase::SpecializedVariantBase
SpecializedVariantBase()=delete
armarx::aron::data::detail::SpecializedVariantBase::~SpecializedVariantBase
virtual ~SpecializedVariantBase()=default
armarx::aron::data::detail::SpecializedVariantBase::DynamicCast
static const DerivedT & DynamicCast(const Variant &n)
Definition: SpecializedVariant.h:129
armarx::aron::data::Variant
The Variant class.
Definition: Variant.h:60
armarx::aron::data::detail::SpecializedVariantBase::cloneAsVariant
VariantPtr cloneAsVariant(const Path &newPath) const override
Definition: SpecializedVariant.h:97
armarx::aron::data::detail::SpecializedVariantBase::DynamicCast
static DerivedT & DynamicCast(Variant &n)
Definition: SpecializedVariant.h:123
armarx::aron::data::detail::SpecializedVariantBase::toAronDTO
data::dto::GenericDataPtr toAronDTO() const override
convert the variant to the ice representation
Definition: SpecializedVariant.h:103
armarx::aron::data::detail::SpecializedVariantBase::operator==
bool operator==(const Variant &other) const override
Definition: SpecializedVariant.h:71
armarx::aron::data::detail::SpecializedVariantBase
Definition: SpecializedVariant.h:39
armarx::armem::server::ltm::detail::mixin::Path
std::filesystem::path Path
Definition: DiskStorageMixin.h:17
armarx::aron::Path::getWithoutPrefix
Path getWithoutPrefix(const Path &) const
Definition: Path.cpp:216
armarx::aron::data::detail::SpecializedVariantBase< data::dto::AronInt, Int >::AronDataType
data::dto::AronInt AronDataType
Definition: SpecializedVariant.h:44
armarx::aron::data::detail::SpecializedVariantBase::cloneAsVariant
VariantPtr cloneAsVariant() const override
get a pointer to a copy of this variant
Definition: SpecializedVariant.h:91
armarx::aron::data::detail::SpecializedVariantBase::aron
AronDataType::PointerType aron
Definition: SpecializedVariant.h:154
armarx::aron::data::detail::SpecializedVariantBase::SpecializedVariantBase
SpecializedVariantBase(const data::Descriptor descriptor, const Path &path=Path())
Definition: SpecializedVariant.h:49
armarx::aron::data::detail::SpecializedVariantBase::PointerType
std::shared_ptr< DerivedT > PointerType
Definition: SpecializedVariant.h:43