Variant.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 // ArmarX
32 #include <RobotAPI/interface/aron.h>
37 
38 namespace armarx::aron::data
39 {
41  typedef std::unique_ptr<VariantFactory> VariantFactoryPtr;
42 
43  class Variant;
44  typedef std::shared_ptr<Variant> VariantPtr;
45 
46  /**
47  * @brief The Variant class. It represents a data object (a variant containing data).
48  * Every data variant inherits from this class. It provdes basic methods for cast/conversion and
49  * holds a descriptor, specifying the data type.
50  *
51  * If you have a unknown variant and you want to run a specific method based on the type, we suggest to use the visitor pattern (@see data/visitor/Visitor.h)
52  *
53  * Note that a data variant differs from a type variant. A data variant contains data (e.g. if you have a list the data object contains the list elements) while
54  * a type object holds the static type information (e.g. the accepted type of the list).
55  * The elements of a list variant without type information may have different types (e.g. element [0] is an int variant whil element [1] is a string variant)
56  *
57  * Also note, that while a type variant should always be defined (will not be NULL!), a data object can be NULL.
58  * This happens, if a maybe type (e.g. optional) is not set. The underlying ice representation will have a nullptr instead.
59  */
60  class Variant
61  {
62  public:
64 
65  public:
66  // constructors
67  Variant() = delete;
68 
71  {
72  }
73 
74  virtual ~Variant() = default;
75 
76  // operators
77  virtual bool operator==(const Variant& other) const = 0;
78 
79  bool
80  operator==(const VariantPtr& other) const
81  {
82  if (!other)
83  {
84  return false;
85  }
86 
87  return *this == *other;
88  }
89 
90  // static methods
91  /// create a variant from a dto object
92  static VariantPtr FromAronDTO(const data::dto::GenericDataPtr&, const Path& = Path());
93 
94  /// create a list of variants from a list of dto objects
95  static std::vector<VariantPtr> FromAronDTO(const std::vector<data::dto::GenericDataPtr>&,
96  const Path& = Path());
97 
98  /// return a list of dto objects from a list of variant objects
99  static std::vector<data::dto::GenericDataPtr> ToAronDTO(const std::vector<VariantPtr>&);
100 
101  /// getter for the descriptor enum
104  {
105  return descriptor;
106  }
107 
108  /// get the path
109  Path
110  getPath() const
111  {
112  return path;
113  }
114 
115  /// get the path as string
116  std::string
117  pathToString() const
118  {
119  return path.toString();
120  }
121 
122  // virtual definitions
123  /// get a pointer to a copy of this variant
124  virtual VariantPtr cloneAsVariant() const = 0;
125  virtual VariantPtr cloneAsVariant(const Path& newPath) const = 0;
126 
127  /// get the children of a data variant
128  virtual std::vector<VariantPtr> getChildren() const = 0;
129 
130  /// get the children size of a data variant
131  virtual size_t childrenSize() const = 0;
132 
133  /// recalculate the type of a data variant. Please not tha the mapping ist NOT bijective, so the calculated type may be wrong
134  virtual type::VariantPtr recalculateType() const = 0;
135 
136  /// checks, if the current data variant fullfills the given type
137  virtual bool fullfillsType(const type::VariantPtr&) const = 0;
138 
139  /// naviate absolute
140  virtual VariantPtr navigateAbsolute(const Path& path) const = 0;
141 
142  /// navigate relative
143  virtual VariantPtr navigateRelative(const Path& path) const = 0;
144 
145  /// get a short str representation of this variant
146  virtual std::string getShortName() const = 0;
147 
148  /// get the full str representation of this variant
149  virtual std::string getFullName() const = 0;
150 
151  /// convert the variant to the ice representation
152  virtual data::dto::GenericDataPtr toAronDTO() const = 0;
153 
154  protected:
156  const Path path;
157 
158  private:
159  static const VariantFactoryPtr FACTORY;
160  };
161 
162  template <class T>
164 } // namespace armarx::aron::data
armarx::aron::data::Variant::fullfillsType
virtual bool fullfillsType(const type::VariantPtr &) const =0
checks, if the current data variant fullfills the given type
armarx::aron::data::VariantFactoryPtr
std::unique_ptr< VariantFactory > VariantFactoryPtr
Definition: Variant.h:40
armarx::aron::data::Variant::descriptor
const data::Descriptor descriptor
Definition: Variant.h:155
armarx::aron::type::VariantPtr
std::shared_ptr< Variant > VariantPtr
Definition: forward_declarations.h:11
Descriptor.h
armarx::aron::data::Variant::getFullName
virtual std::string getFullName() const =0
get the full str representation of this variant
armarx::aron::data::Variant::FromAronDTO
static VariantPtr FromAronDTO(const data::dto::GenericDataPtr &, const Path &=Path())
create a variant from a dto object
Definition: Variant.cpp:39
Path.h
armarx::aron::data::VariantFactory
The VariantFactory class.
Definition: Factory.h:39
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::Variant::navigateAbsolute
virtual VariantPtr navigateAbsolute(const Path &path) const =0
naviate absolute
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::Variant::getChildren
virtual std::vector< VariantPtr > getChildren() const =0
get the children of a data variant
Variant.h
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::aron::data::Variant::operator==
virtual bool operator==(const Variant &other) const =0
armarx::aron::data::Variant
The Variant class.
Definition: Variant.h:60
armarx::aron::data::Variant::getDescriptor
data::Descriptor getDescriptor() const
getter for the descriptor enum
Definition: Variant.h:103
armarx::aron::data::Variant::Variant
Variant()=delete
armarx::aron::data::Variant::childrenSize
virtual size_t childrenSize() const =0
get the children size of a data variant
armarx::aron::data::Variant::toAronDTO
virtual data::dto::GenericDataPtr toAronDTO() const =0
convert the variant to the ice representation
armarx::aron::data::isVariant
concept isVariant
Definition: Variant.h:163
armarx::aron::data
A convenience header to include all aron files (full include, not forward declared)
Definition: aron_conversions.cpp:3
armarx::aron::data::Variant::ToAronDTO
static std::vector< data::dto::GenericDataPtr > ToAronDTO(const std::vector< VariantPtr > &)
return a list of dto objects from a list of variant objects
Definition: Variant.cpp:54
Exception.h
armarx::aron::data::Variant::recalculateType
virtual type::VariantPtr recalculateType() const =0
recalculate the type of a data variant. Please not tha the mapping ist NOT bijective,...
armarx::aron::data::Variant::cloneAsVariant
virtual VariantPtr cloneAsVariant() const =0
get a pointer to a copy of this variant
armarx::aron::data::Variant::~Variant
virtual ~Variant()=default
armarx::aron::data::Variant::navigateRelative
virtual VariantPtr navigateRelative(const Path &path) const =0
navigate relative
armarx::armem::server::ltm::detail::mixin::Path
std::filesystem::path Path
Definition: DiskStorageMixin.h:17
armarx::aron::data::Variant::Variant
Variant(const data::Descriptor &descriptor, const Path &path)
Definition: Variant.h:69
armarx::aron::data::Variant::getShortName
virtual std::string getShortName() const =0
get a short str representation of this variant
armarx::aron::data::Variant::operator==
bool operator==(const VariantPtr &other) const
Definition: Variant.h:80
armarx::aron::data::Variant::pathToString
std::string pathToString() const
get the path as string
Definition: Variant.h:117
armarx::aron::Path::toString
std::string toString() const
Definition: Path.cpp:125