EnumVariant.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 <string>
28 
29 // Base class
30 #include "SpecializedVariant.h"
31 
32 // ArmarX
33 
35 {
36  template <typename AronTypeT, typename DerivedT, typename ValueT>
37  class EnumVariant : public SpecializedVariantBase<AronTypeT, DerivedT>
38  {
39  public:
40  using ValueType = ValueT;
42 
44 
45  virtual ~EnumVariant() = default;
46 
47  /* virtual implementations */
49  navigateAbsolute(const Path& path) const override
50  {
52  __PRETTY_FUNCTION__,
53  "Could not navigate through a non container navigator. The input path was: " +
54  path.toString(),
56  }
57 
58  std::vector<VariantPtr>
59  getChildren() const override
60  {
61  return {};
62  }
63 
64  size_t
65  childrenSize() const override
66  {
67  return 0;
68  }
69 
70  void
71  setAcceptedValueMap(const std::map<std::string, ValueType>& valueMap)
72  {
73  this->aron->acceptedValues = valueMap;
74  }
75 
76  std::map<std::string, int>
78  {
79  return this->aron->acceptedValues;
80  }
81 
82  std::vector<std::string>
84  {
85  std::vector<std::string> names;
86  for (const auto& [k, _] : this->aron->acceptedValues)
87  {
88  names.push_back(k);
89  }
90  return names;
91  }
92 
93  std::vector<ValueType>
95  {
96  std::vector<ValueType> vals;
97  for (const auto& [_, i] : this->aron->acceptedValues)
98  {
99  vals.push_back(i);
100  }
101  return vals;
102  }
103 
104  std::string
106  {
107  for (const auto& [k, v] : this->aron->acceptedValues)
108  {
109  if (v == i)
110  {
111  return k;
112  }
113  }
114  throw error::AronException(__PRETTY_FUNCTION__,
115  "Enum could not be resolved. Input was: " +
116  std::to_string(i),
117  this->getPath());
118  }
119 
120  int
121  getValue(const std::string& s) const
122  {
123  return this->aron->acceptedValues[s];
124  }
125 
126  std::string
127  getEnumName() const
128  {
129  return this->aron->enumName;
130  }
131 
132  void
133  setEnumName(const std::string& s)
134  {
135  this->aron->enumName = s;
136  }
137 
138  void
139  addAcceptedValue(const std::string& s, ValueType i)
140  {
141  this->aron->acceptedValues[s] = i;
142  }
143 
144  std::string
146  {
147  return this->aron->defaultValue;
148  }
149 
150  ValueType
152  {
153  // ARMARX_CHECK(std::find(this->aron->acceptedValues.begin(),
154  // this->aron->acceptedValues.end(),
155  // this->aron->defaultValue) != this->aron->acceptedValues.end())
156  // << "An aron enum type has an invalid default value set. Got: "
157  // << this->aron->defaultValue;
158  return this->aron->acceptedValues[this->aron->defaultValue];
159  }
160 
161  void
163  {
164  for (const auto& [key, value] : this->aron->acceptedValues)
165  {
166  if (value == v)
167  {
168  this->aron->defaultValue = key;
169  }
170  }
172  __PRETTY_FUNCTION__, "Got invalid input for enum value", std::to_string(v));
173  }
174 
175  void
176  setDefaultValueName(const std::string& v)
177  {
178  // ARMARX_CHECK(std::find(this->aron->acceptedValues.begin(),
179  // this->aron->acceptedValues.end(),
180  // this->aron->defaultValue) != this->aron->acceptedValues.end())
181  // << "Trying to set an invalid default value for aron enum type. Got: "
182  // << this->aron->defaultValue;
183  this->aron->defaultValue = v;
184  }
185  };
186 } // namespace armarx::aron::type::detail
armarx::aron::type::detail::EnumVariant::childrenSize
size_t childrenSize() const override
Definition: EnumVariant.h:65
armarx::aron::error::AronException
A base class for aron exceptions.
Definition: Exception.h:36
armarx::aron::type::VariantPtr
std::shared_ptr< Variant > VariantPtr
Definition: forward_declarations.h:11
armarx::aron::type::detail::EnumVariant::getAcceptedValueNames
std::vector< std::string > getAcceptedValueNames() const
Definition: EnumVariant.h:83
armarx::aron::type::detail
Definition: AnyVariant.h:32
armarx::aron::type::detail::EnumVariant::getEnumName
std::string getEnumName() const
Definition: EnumVariant.h:127
armarx::aron::type::detail::EnumVariant::addAcceptedValue
void addAcceptedValue(const std::string &s, ValueType i)
Definition: EnumVariant.h:139
armarx::aron::type::detail::EnumVariant::getAcceptedValues
std::vector< ValueType > getAcceptedValues() const
Definition: EnumVariant.h:94
armarx::aron::type::detail::EnumVariant::setDefaultValueName
void setDefaultValueName(const std::string &v)
Definition: EnumVariant.h:176
armarx::aron::type::detail::EnumVariant::getChildren
std::vector< VariantPtr > getChildren() const override
get all child elements
Definition: EnumVariant.h:59
SpecializedVariant.h
armarx::aron::type::detail::EnumVariant::getValue
int getValue(const std::string &s) const
Definition: EnumVariant.h:121
armarx::aron::type::detail::SpecializedVariantBase
Definition: SpecializedVariant.h:35
armarx::aron::type::detail::EnumVariant::getDefaultValueName
std::string getDefaultValueName() const
Definition: EnumVariant.h:145
armarx::aron::Path
The Path class.
Definition: Path.h:35
armarx::aron::error::ValueNotValidException
The ValueNotValidException class.
Definition: Exception.h:98
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::aron::type::detail::EnumVariant::getValueName
std::string getValueName(ValueType i) const
Definition: EnumVariant.h:105
armarx::aron::type::detail::SpecializedVariantBase::aron
AronTypeT::PointerType aron
Definition: SpecializedVariant.h:132
armarx::aron::type::detail::EnumVariant::setDefaultValue
void setDefaultValue(const ValueType &v)
Definition: EnumVariant.h:162
armarx::aron::type::detail::EnumVariant
Definition: EnumVariant.h:37
armarx::aron::type::detail::EnumVariant< type::dto::IntEnum, IntEnum, int >::ValueType
int ValueType
Definition: EnumVariant.h:40
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::viz::data::ElementFlags::names
const simox::meta::IntEnumNames names
Definition: json_elements.cpp:13
armarx::aron::type::detail::EnumVariant::setAcceptedValueMap
void setAcceptedValueMap(const std::map< std::string, ValueType > &valueMap)
Definition: EnumVariant.h:71
armarx::aron::type::Variant::path
const Path path
Definition: Variant.h:109
armarx::aron::type::detail::EnumVariant::setEnumName
void setEnumName(const std::string &s)
Definition: EnumVariant.h:133
armarx::aron::type::detail::EnumVariant::getDefaultValue
ValueType getDefaultValue() const
Definition: EnumVariant.h:151
armarx::aron::type::detail::EnumVariant::getAcceptedValueMap
std::map< std::string, int > getAcceptedValueMap() const
Definition: EnumVariant.h:77
armarx::aron::type::detail::EnumVariant::~EnumVariant
virtual ~EnumVariant()=default
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::aron::type::detail::EnumVariant::navigateAbsolute
VariantPtr navigateAbsolute(const Path &path) const override
naviate absolute
Definition: EnumVariant.h:49
armarx::aron::type::detail::SpecializedVariantBase::SpecializedVariantBase
SpecializedVariantBase(const type::Descriptor &descriptor, const Path &path)
Definition: SpecializedVariant.h:39
armarx::aron::Path::toString
std::string toString() const
Definition: Path.cpp:127
armarx::aron::type::Variant::getPath
Path getPath() const
Definition: Variant.cpp:68