StringHelperTemplates.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2017, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarX
19  * @author Mirko Waechter( mirko.waechter at kit dot edu)
20  * @date 2017
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #pragma once
25 
26 #include <boost/lexical_cast.hpp>
27 
28 #include "StringHelpers.h"
30 
31 namespace armarx
32 {
33 
34  template <typename T>
35  inline std::vector<T>
36  Split(const std::string& source,
37  const std::string& splitBy,
38  bool trimElements = false,
39  bool removeEmptyElements = false)
40  {
41  if (source.empty())
42  {
43  return std::vector<T>();
44  }
45 
46  std::vector<std::string> components =
47  Split(source, splitBy, trimElements, removeEmptyElements);
48  // boost::split(components, source, boost::is_any_of(splitBy));
49 
50  std::vector<T> result(components.size());
51  std::transform(components.begin(),
52  components.end(),
53  result.begin(),
54  boost::lexical_cast<T, std::string>);
55  return result;
56  }
57 
58  namespace detail
59  {
60  template <class>
62 
63  template <std::size_t... Is>
64  struct ToStringFTuple<meta::IndexSequence<Is...>>
65  {
66  template <class... Ts>
67  static std::string
68  Format(const std::string& form, const std::tuple<Ts...>& tuple)
69  {
70  //this has to be done via a string
71 #pragma GCC diagnostic push
72 #pragma GCC diagnostic ignored "-Wformat-security"
73  char buff[512];
74  int sz = std::snprintf(buff, sizeof(buff), form.c_str(), std::get<Is>(tuple)...);
75  if (0 < sz && static_cast<std::size_t>(sz) < sizeof(buff))
76  {
77  return buff;
78  }
79  std::vector<char> buffd(sz + 1); // note +1 for null terminator
80  if (std::snprintf(buff, sizeof(buff), form.c_str(), std::get<Is>(tuple)...) != sz)
81  {
82  throw std::logic_error{__FILE__ " " + std::to_string(__LINE__) +
83  " std::snprintf behaved unexpectedly"};
84  }
85 #pragma GCC diagnostic pop
86  return buffd.data();
87  }
88 
89  static std::string
90  Format(const std::string& form, std::tuple<>)
91  {
92  return form;
93  }
94  };
95 
96  } // namespace detail
97 
98  template <std::size_t From, std::size_t To, class... Ts>
99  std::string
100  TupleToStringF(const std::string& form, const std::tuple<Ts...>& tuple)
101  {
103  }
104 
105  template <std::size_t From, class... Ts>
106  std::string
107  TupleToStringF(const std::string& form, const std::tuple<Ts...>& tuple)
108  {
109  return TupleToStringF<From, sizeof...(Ts)>(form, tuple);
110  }
111 
112  template <class... Ts>
113  std::string
114  TupleToStringF(const std::string& form, const std::tuple<Ts...>& tuple)
115  {
116  return TupleToStringF<0>(form, tuple);
117  }
118 
119 } // namespace armarx
visionx::imrec::Format
Format
Supported recording Formats.
Definition: public_api.h:53
armarx::detail::ToStringFTuple
Definition: StringHelperTemplates.h:61
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:36
detail
Definition: OpenCVUtil.cpp:128
StringHelpers.h
TemplateMetaProgramming.h
armarx::detail::ToStringFTuple< meta::IndexSequence< Is... > >::Format
static std::string Format(const std::string &form, std::tuple<>)
Definition: StringHelperTemplates.h:90
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:661
armarx::detail::ToStringFTuple< meta::IndexSequence< Is... > >::Format
static std::string Format(const std::string &form, const std::tuple< Ts... > &tuple)
Definition: StringHelperTemplates.h:68
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
armarx::transform
auto transform(const Container< InputT, Alloc > &in, OutputT(*func)(InputT const &)) -> Container< OutputT, typename std::allocator_traits< Alloc >::template rebind_alloc< OutputT >>
Convenience function (with less typing) to transform a container of type InputT into the same contain...
Definition: algorithm.h:351
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::TupleToStringF
std::string TupleToStringF(const std::string &form, const std::tuple< Ts... > &tuple)
Definition: StringHelperTemplates.h:100