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 
23 #include "VariantHelperFactory.h"
24 
27 
29 
31 {
32  template <>
33  std::string
35  {
37  return simox::alg::to_string(data::Int::DynamicCast(ptr)->getValue());
38  }
39 
40  template <>
41  std::string
43  {
45  return simox::alg::to_string(data::Bool::DynamicCast(ptr)->getValue());
46  }
47 
48  template <>
49  std::string
51  {
53  return data::String::DynamicCast(ptr)->getValue();
54  }
55 
56  template <>
57  std::string
59  {
61  return simox::alg::to_string(data::Float::DynamicCast(ptr)->getValue());
62  }
63 
64  template <>
65  std::string
67  {
69  // TODO: this is should use the name of the enum value
70  return simox::alg::to_string(data::Int::DynamicCast(ptr)->getValue());
71  }
72 
73  template <>
74  std::string
76  {
78  return simox::alg::to_string(data::Double::DynamicCast(ptr)->getValue());
79  }
80 
81  template <>
84  const std::string& string,
85  const armarx::aron::Path& path) const
86  {
88  int i = string.empty() ? 0 : std::stoi(string);
89  return make_int(i, path);
90  }
91 
92  template <>
95  const std::string& string,
96  const armarx::aron::Path& path) const
97  {
99  float f = string.empty() ? 0 : std::stof(string);
100  return make_float(f, path);
101  }
102 
103  template <>
106  const std::string& string,
107  const armarx::aron::Path& path) const
108  {
109  ARMARX_TRACE;
110  double d = string.empty() ? 0 : std::stod(string);
111  return make_double(d, path);
112  }
113 
114  template <>
117  const std::string& string,
118  const armarx::aron::Path& path) const
119  {
120  ARMARX_TRACE;
121  if (string == "true")
122  {
123  return make_bool(true, path);
124  }
125  else if (string == "false" or string.empty())
126  {
127  return make_bool(false, path);
128  }
129  throw armarx::InvalidArgumentException("Boolean string has to be either true or false");
130  }
131 
132  template <>
135  const std::string& string,
136  const armarx::aron::Path& path) const
137  {
138  // TODO: this might not work
139  ARMARX_TRACE;
140  return make_int(string.empty() ? 0 : std::stoi(string), path);
141  }
142 
143  template <>
146  const std::string& string,
147  const armarx::aron::Path& path) const
148  {
149  ARMARX_TRACE;
150  std::string formatted_string = "";
151  if (not string.empty())
152  {
153  formatted_string =
155  }
156  return make_string(formatted_string, path);
157  }
158 
159  template <>
160  void
162  const armarx::aron::data::VariantPtr& variant,
163  const std::string& string) const
164  {
165  ARMARX_TRACE;
166  data::Int::DynamicCast(variant)->getValue() = std::stoi(string);
167  }
168 
169  template <>
170  void
172  const armarx::aron::data::VariantPtr& variant,
173  const std::string& string) const
174  {
175  ARMARX_TRACE;
176  data::Float::DynamicCast(variant)->getValue() = std::stof(string);
177  }
178 
179  template <>
180  void
182  const armarx::aron::data::VariantPtr& variant,
183  const std::string& string) const
184  {
185  ARMARX_TRACE;
186  data::Double::DynamicCast(variant)->getValue() = std::stod(string);
187  }
188 
189  template <>
190  void
192  const armarx::aron::data::VariantPtr& variant,
193  const std::string& string) const
194  {
195  ARMARX_TRACE;
196  data::Bool::DynamicCast(variant)->getValue() = string == "true";
197  }
198 
199  template <>
200  void
202  const armarx::aron::data::VariantPtr& variant,
203  const std::string& string) const
204  {
205  ARMARX_TRACE;
206  data::String::DynamicCast(variant)->getValue() = string;
207  }
208 
209  template <>
210  void
212  const armarx::aron::data::VariantPtr& variant,
213  const std::string& string) const
214  {
215  ARMARX_TRACE;
216  data::Int::DynamicCast(variant)->getValue() = std::stoi(string);
217  }
218 
225 } // namespace armarx::aron::component_config::products
armarx::aron::make_string
aron::data::StringPtr make_string(_Args &&... args)
Definition: String.h:76
armarx::aron::make_float
aron::data::FloatPtr make_float(_Args &&... args)
Definition: Float.h:77
armarx::aron::component_config::products::VariantHelper::from_string
aron::data::VariantPtr from_string(const std::string &, const armarx::aron::Path &path) const override
cxxopts::empty
bool empty(const std::string &s)
Definition: cxxopts.hpp:255
VariantHelperFactory.h
armarx::aron::data::detail::SpecializedVariantBase< data::dto::AronInt, Int >::DynamicCast
static PointerType DynamicCast(const VariantPtr &n)
Definition: SpecializedVariant.h:117
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::aron::data::VariantPtr
std::shared_ptr< Variant > VariantPtr
Definition: forward_declarations.h:11
armarx::aron::make_bool
aron::data::BoolPtr make_bool(_Args &&... args)
Definition: Bool.h:77
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
Property.h
armarx::aron::component_config::products::VariantHelper::set_value_from_string
void set_value_from_string(const armarx::aron::data::VariantPtr &variant, const std::string &string) const override
All.h
armarx::aron::make_double
aron::data::DoublePtr make_double(_Args &&... args)
Definition: Double.h:77
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::aron::component_config::products::VariantHelper
Definition: VariantHelperFactory.h:56
PropertyDefinitionContainer.h
armarx::aron::component_config::products::VariantHelper::to_string
std::string to_string(const data::VariantPtr &ptr) const override
armarx::FileSystemPathBuilder_ApplyFormattingAndResolveEnvAndCMakeVars
std::string FileSystemPathBuilder_ApplyFormattingAndResolveEnvAndCMakeVars(std::string const &value)
Definition: Property.cpp:39
armarx::aron::component_config::products
Definition: VariantHelperFactory.cpp:30
armarx::aron::make_int
aron::data::IntPtr make_int(_Args &&... args)
Definition: Int.h:78