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#include <SimoxUtility/algorithm/get_map_keys_values.h>
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.at(s);
126 }
127
128 std::string
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
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 << "Valid values are: " << ::simox::alg::get_keys(this->aron->acceptedValues);
161 return this->aron->acceptedValues.at(this->aron->defaultValue);
162 }
163
164 void
166 {
167 for (const auto& [key, value] : this->aron->acceptedValues)
168 {
169 if (value == v)
170 {
171 this->aron->defaultValue = key;
172 }
173 }
175 __PRETTY_FUNCTION__, "Got invalid input for enum value", std::to_string(v));
176 }
177
178 void
179 setDefaultValueName(const std::string& v)
180 {
181 ARMARX_CHECK(this->aron->acceptedValues.count(v) > 0)
182 << "Trying to set an invalid default value for aron enum type. Got: "
183 << this->aron->defaultValue;
184 this->aron->defaultValue = v;
185 }
186 };
187} // namespace armarx::aron::type::detail
The Path class.
Definition Path.h:36
A base class for aron exceptions.
Definition Exception.h:37
The ValueNotValidException class.
Definition Exception.h:99
size_t childrenSize() const override
Definition EnumVariant.h:67
void setEnumName(const std::string &s)
std::vector< std::string > getAcceptedValueNames() const
Definition EnumVariant.h:85
std::map< std::string, int > getAcceptedValueMap() const
Definition EnumVariant.h:79
void setDefaultValue(const ValueType &v)
std::string getValueName(ValueType i) const
SpecializedVariantBase< AronTypeT, DerivedT > Base
Definition EnumVariant.h:43
SpecializedVariantBase(const type::Descriptor &descriptor, const Path &path)
void setAcceptedValueMap(const std::map< std::string, ValueType > &valueMap)
Definition EnumVariant.h:73
void addAcceptedValue(const std::string &s, ValueType i)
std::vector< ValueType > getAcceptedValues() const
Definition EnumVariant.h:96
VariantPtr navigateAbsolute(const Path &path) const override
naviate absolute
Definition EnumVariant.h:51
std::vector< VariantPtr > getChildren() const override
get all child elements
Definition EnumVariant.h:61
int getValue(const std::string &s) const
void setDefaultValueName(const std::string &v)
SpecializedVariantBase(const type::Descriptor &descriptor, const Path &path)
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
std::shared_ptr< Variant > VariantPtr