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