VariantHelperFactory.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package RobotAPI
17 * @author Christoph Pohl ( christoph dot pohl at kit dot edu )
18 * @date 04.11.22
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
24
25#include <SimoxUtility/algorithm/string/string_conversion.h>
26
29#include <ArmarXCore/interface/core/UserException.h>
30
32
34{
35 template <>
36 std::string
38 {
40 return simox::alg::to_string(data::Int::DynamicCast(ptr)->getValue());
41 }
42
43 template <>
44 std::string
46 {
48 return simox::alg::to_string(data::Bool::DynamicCast(ptr)->getValue());
49 }
50
51 template <>
52 std::string
58
59 template <>
60 std::string
62 {
64 return simox::alg::to_string(data::Float::DynamicCast(ptr)->getValue());
65 }
66
67 template <>
68 std::string
70 {
72 // TODO: this is should use the name of the enum value
73 return simox::alg::to_string(data::Int::DynamicCast(ptr)->getValue());
74 }
75
76 template <>
77 std::string
79 {
81 return simox::alg::to_string(data::Double::DynamicCast(ptr)->getValue());
82 }
83
84 template <>
87 const std::string& string,
88 const armarx::aron::Path& path) const
89 {
91 int i = string.empty() ? 0 : std::stoi(string);
92 return make_int(i, path);
93 }
94
95 template <>
98 const std::string& string,
99 const armarx::aron::Path& path) const
100 {
102 float f = string.empty() ? 0 : std::stof(string);
103 return make_float(f, path);
104 }
105
106 template <>
109 const std::string& string,
110 const armarx::aron::Path& path) const
111 {
113 double d = string.empty() ? 0 : std::stod(string);
114 return make_double(d, path);
115 }
116
117 template <>
120 const std::string& string,
121 const armarx::aron::Path& path) const
122 {
124 if (string == "true")
125 {
126 return make_bool(true, path);
127 }
128 else if (string == "false" or string.empty())
129 {
130 return make_bool(false, path);
131 }
132 throw armarx::InvalidArgumentException("Boolean string has to be either true or false");
133 }
134
135 template <>
138 const std::string& string,
139 const armarx::aron::Path& path) const
140 {
141 // TODO: this might not work
143 return make_int(string.empty() ? 0 : std::stoi(string), path);
144 }
145
146 template <>
149 const std::string& string,
150 const armarx::aron::Path& path) const
151 {
153 std::string formatted_string = "";
154 if (not string.empty())
155 {
156 formatted_string =
158 }
159 return make_string(formatted_string, path);
160 }
161
162 template <>
163 void
165 const armarx::aron::data::VariantPtr& variant,
166 const std::string& string) const
167 {
169 data::Int::DynamicCast(variant)->getValue() = std::stoi(string);
170 }
171
172 template <>
173 void
175 const armarx::aron::data::VariantPtr& variant,
176 const std::string& string) const
177 {
179 data::Float::DynamicCast(variant)->getValue() = std::stof(string);
180 }
181
182 template <>
183 void
185 const armarx::aron::data::VariantPtr& variant,
186 const std::string& string) const
187 {
189 data::Double::DynamicCast(variant)->getValue() = std::stod(string);
190 }
191
192 template <>
193 void
195 const armarx::aron::data::VariantPtr& variant,
196 const std::string& string) const
197 {
199 data::Bool::DynamicCast(variant)->getValue() = string == "true";
200 }
201
202 template <>
203 void
205 const armarx::aron::data::VariantPtr& variant,
206 const std::string& string) const
207 {
209 data::String::DynamicCast(variant)->getValue() = string;
210 }
211
212 template <>
213 void
215 const armarx::aron::data::VariantPtr& variant,
216 const std::string& string) const
217 {
219 data::Int::DynamicCast(variant)->getValue() = std::stoi(string);
220 }
221
222 template struct products::VariantHelper<type::Descriptor::INT>;
223 template struct products::VariantHelper<type::Descriptor::STRING>;
224 template struct products::VariantHelper<type::Descriptor::BOOL>;
225 template struct products::VariantHelper<type::Descriptor::FLOAT>;
226 template struct products::VariantHelper<type::Descriptor::DOUBLE>;
227 template struct products::VariantHelper<type::Descriptor::INT_ENUM>;
228} // namespace armarx::aron::component_config::products
#define make_int(lowbyte, mid1, mid2, highbyte)
#define make_float(result, byteptr)
The Path class.
Definition Path.h:36
std::shared_ptr< Variant > VariantPtr
aron::data::StringPtr make_string(_Args &&... args)
Definition String.h:76
aron::data::BoolPtr make_bool(_Args &&... args)
Definition Bool.h:77
aron::data::DoublePtr make_double(_Args &&... args)
Definition Double.h:77
std::string FileSystemPathBuilder_ApplyFormattingAndResolveEnvAndCMakeVars(std::string const &value)
Definition Property.cpp:39
bool empty(const std::string &s)
Definition cxxopts.hpp:234
std::string to_string(const data::VariantPtr &ptr) const override
aron::data::VariantPtr from_string(const std::string &, const armarx::aron::Path &path) const override
void set_value_from_string(const armarx::aron::data::VariantPtr &variant, const std::string &string) const override
#define ARMARX_TRACE
Definition trace.h:77