Tuple.cpp
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 // Header
25 #include "Tuple.h"
26 
27 #include <SimoxUtility/algorithm/string/string_conversion.h>
28 
29 namespace armarx::aron::type
30 {
31  // constructors
32  Tuple::Tuple(const Path& path) :
33  detail::ContainerVariant<type::dto::Tuple, Tuple>(type::Descriptor::TUPLE, path)
34  {
35  }
36 
37  Tuple::Tuple(const type::dto::Tuple& o, const Path& path) :
38  detail::ContainerVariant<type::dto::Tuple, Tuple>(o, type::Descriptor::TUPLE, path)
39  {
40  unsigned int i = 0;
41  for (const auto& t : o.elementTypes)
42  {
43  acceptedTypes.push_back(FromAronDTO(*t, path.withAcceptedTypeIndex(i++)));
44  }
45  }
46 
49  {
50  if (acceptedTypes.empty())
51  {
52  throw error::AronException(__PRETTY_FUNCTION__, "No accepted types set", getPath());
53  }
54  return this->aron;
55  }
56 
57  // public member functions
58  std::vector<VariantPtr>
60  {
61  return acceptedTypes;
62  }
63 
64  bool
65  Tuple::hasAcceptedType(unsigned int i) const
66  {
67  return i < acceptedTypes.size();
68  }
69 
71  Tuple::getAcceptedType(unsigned int i) const
72  {
73  return acceptedTypes[i];
74  }
75 
76  void
77  Tuple::setAcceptedTypes(const std::vector<VariantPtr>& acceptedTypes)
78  {
79  this->acceptedTypes = acceptedTypes;
80  for (const auto& acceptedType : acceptedTypes)
81  {
82  aron->elementTypes.push_back(acceptedType->toAronDTO());
83  }
84  }
85 
86  void
88  {
90  this->aron->elementTypes.push_back(v->toAronDTO());
91  acceptedTypes.push_back(v);
92  }
93 
94  // virtual implementations
95  std::vector<VariantPtr>
97  {
98  return acceptedTypes;
99  }
100 
101  size_t
103  {
104  return acceptedTypes.size();
105  }
106 
107  std::string
109  {
110  std::vector<std::string> names;
111  for (const auto& n : acceptedTypes)
112  {
113  names.push_back(n->getShortName());
114  }
115  return "Tuple<" + simox::alg::to_string(names, ", ") + ">";
116  }
117 
118  std::string
120  {
121  std::vector<std::string> names;
122  for (const auto& n : acceptedTypes)
123  {
124  names.push_back(n->getFullName());
125  }
126  return "armarx::aron::type::Tuple<" + simox::alg::to_string(names, ", ") + ">";
127  }
128 
129  std::string
131  {
132  return "armarx::aron::type::Tuple";
133  }
134 
135  std::string
137  {
138  return "Tuple";
139  }
140 
141  VariantPtr
142  Tuple::navigateAbsolute(const Path& path) const
143  {
144  if (!path.hasElement())
145  {
146  throw error::AronException(
147  __PRETTY_FUNCTION__, "Could not navigate without a valid path", path);
148  }
149 
150  std::string el = path.getFirstElement();
151  for (unsigned int i = 0; i < acceptedTypes.size(); ++i)
152  {
153  if (el == "::accepted_type_" + std::to_string(i))
154  {
155  if (path.size() == 1)
156  {
157  return acceptedTypes[i];
158  }
159  else
160  {
162  return acceptedTypes[i]->navigateAbsolute(next);
163  }
164  }
165  }
167  __PRETTY_FUNCTION__, "Could not find an element of a path.", el, path);
168  }
169 } // namespace armarx::aron::type
armarx::aron::Path::getFirstElement
std::string getFirstElement() const
Definition: Path.cpp:102
armarx::aron::error::AronException
A base class for aron exceptions.
Definition: Exception.h:42
armarx::aron::type::VariantPtr
std::shared_ptr< Variant > VariantPtr
Definition: forward_declarations.h:11
armarx::aron::type::Variant::FromAronDTO
static VariantPtr FromAronDTO(const type::dto::GenericType &, const Path &=Path())
create a variant object from an dto object
Definition: Variant.cpp:39
armarx::aron::Path::hasElement
bool hasElement() const
Definition: Path.cpp:113
armarx::aron::Path::withAcceptedTypeIndex
Path withAcceptedTypeIndex(int, bool escape=false) const
Definition: Path.cpp:183
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::Path::size
size_t size() const
Definition: Path.cpp:119
detail
Definition: OpenCVUtil.cpp:127
armarx::aron::type::TuplePtr
std::shared_ptr< class Tuple > TuplePtr
Definition: forward_declarations.h:17
armarx::aron::type::Tuple::hasAcceptedType
bool hasAcceptedType(unsigned int i) const
Definition: Tuple.cpp:65
armarx::aron::type::Tuple
The Tuple class.
Definition: Tuple.h:39
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::aron::type::Tuple::navigateAbsolute
VariantPtr navigateAbsolute(const Path &path) const override
naviate absolute
Definition: Tuple.cpp:142
armarx::aron::error::ValueNotValidException
The ValueNotValidException class.
Definition: Exception.h:145
armarx::aron::type::Tuple::Tuple
Tuple(const Path &path=Path())
Definition: Tuple.cpp:32
armarx::aron::type::detail::SpecializedVariantBase< type::dto::Tuple, Tuple >::aron
type::dto::Tuple ::PointerType aron
Definition: SpecializedVariant.h:137
armarx::aron::type::Tuple::getAcceptedTypes
std::vector< VariantPtr > getAcceptedTypes() const
Definition: Tuple.cpp:59
armarx::aron::type::Tuple::setAcceptedTypes
void setAcceptedTypes(const std::vector< VariantPtr > &)
Definition: Tuple.cpp:77
armarx::aron::type::Tuple::getChildren
std::vector< VariantPtr > getChildren() const override
get all child elements
Definition: Tuple.cpp:96
armarx::aron::type::Tuple::getShortName
std::string getShortName() const override
get a short name of this specific type
Definition: Tuple.cpp:108
armarx::aron::type
A convenience header to include all aron files (full include, not forward declared)
Definition: aron_conversions.cpp:9
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::aron::type::Tuple::getFullName
std::string getFullName() const override
get the full name of this specific type
Definition: Tuple.cpp:119
armarx::viz::data::ElementFlags::names
const simox::meta::IntEnumNames names
Definition: json_elements.cpp:14
armarx::aron::type::Tuple::toTupleDTO
type::dto::TuplePtr toTupleDTO() const
Definition: Tuple.cpp:48
armarx::aron::Path::withDetachedFirstElement
Path withDetachedFirstElement() const
Definition: Path.cpp:205
armarx::aron::type::Descriptor::TUPLE
@ TUPLE
armarx::aron::type::Tuple::childrenSize
size_t childrenSize() const override
Definition: Tuple.cpp:102
armarx::aron::type::Tuple::getAcceptedType
VariantPtr getAcceptedType(unsigned int i) const
Definition: Tuple.cpp:71
armarx::aron::type::Variant::path
const Path path
Definition: Variant.h:139
Tuple.h
armarx::aron::type::Tuple::addAcceptedType
void addAcceptedType(const VariantPtr &)
Definition: Tuple.cpp:87
armarx::aron::type::Tuple::GetFullNamePrefix
static std::string GetFullNamePrefix()
Definition: Tuple.cpp:130
armarx::aron::type::Descriptor
Descriptor
Definition: Descriptor.h:76
armarx::aron::type::Tuple::GetNamePrefix
static std::string GetNamePrefix()
Definition: Tuple.cpp:136
armarx::aron::type::Variant::getPath
Path getPath() const
Definition: Variant.h:99