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 "StringHelpers.h"
28 
29 #include <boost/lexical_cast.hpp>
30 
31 namespace armarx
32 {
33 
34  template<typename T>
35  inline std::vector<T> Split(const std::string& source, const std::string& splitBy, bool trimElements = false, bool removeEmptyElements = false)
36  {
37  if (source.empty())
38  {
39  return std::vector<T>();
40  }
41 
42  std::vector<std::string> components = Split(source, splitBy, trimElements, removeEmptyElements);
43  // boost::split(components, source, boost::is_any_of(splitBy));
44 
45  std::vector<T> result(components.size());
46  std::transform(components.begin(), components.end(), result.begin(), boost::lexical_cast<T, std::string>);
47  return result;
48  }
49 
50  namespace detail
51  {
52  template<class> struct ToStringFTuple;
53 
54  template<std::size_t...Is>
55  struct ToStringFTuple<meta::IndexSequence<Is...>>
56  {
57  template<class...Ts>
58  static std::string Format(const std::string& form, const std::tuple<Ts...>& tuple)
59  {
60  //this has to be done via a string
61 #pragma GCC diagnostic push
62 #pragma GCC diagnostic ignored "-Wformat-security"
63  char buff[512];
64  int sz = std::snprintf(buff, sizeof(buff), form.c_str(), std::get<Is>(tuple)...);
65  if (0 < sz && static_cast<std::size_t>(sz) < sizeof(buff))
66  {
67  return buff;
68  }
69  std::vector<char> buffd(sz + 1); // note +1 for null terminator
70  if (std::snprintf(buff, sizeof(buff), form.c_str(), std::get<Is>(tuple)...) != sz)
71  {
72  throw std::logic_error
73  {
74  __FILE__ " " + std::to_string(__LINE__) +
75  " std::snprintf behaved unexpectedly"
76  };
77  }
78 #pragma GCC diagnostic pop
79  return buffd.data();
80  }
81  static std::string Format(const std::string& form, std::tuple<>)
82  {
83  return form;
84  }
85  };
86 
87  }
88 
89  template<std::size_t From, std::size_t To, class...Ts>
90  std::string TupleToStringF(const std::string& form, const std::tuple<Ts...>& tuple)
91  {
93  }
94 
95  template<std::size_t From, class...Ts>
96  std::string TupleToStringF(const std::string& form, const std::tuple<Ts...>& tuple)
97  {
98  return TupleToStringF<From, sizeof...(Ts)>(form, tuple);
99  }
100 
101  template<class...Ts>
102  std::string TupleToStringF(const std::string& form, const std::tuple<Ts...>& tuple)
103  {
104  return TupleToStringF<0>(form, tuple);
105  }
106 
107 }
108 
109 
visionx::imrec::Format
Format
Supported recording Formats.
Definition: public_api.h:55
armarx::detail::ToStringFTuple
Definition: StringHelperTemplates.h:52
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:35
detail
Definition: OpenCVUtil.cpp:127
StringHelpers.h
TemplateMetaProgramming.h
armarx::detail::ToStringFTuple< meta::IndexSequence< Is... > >::Format
static std::string Format(const std::string &form, std::tuple<>)
Definition: StringHelperTemplates.h:81
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:681
armarx::detail::ToStringFTuple< meta::IndexSequence< Is... > >::Format
static std::string Format(const std::string &form, const std::tuple< Ts... > &tuple)
Definition: StringHelperTemplates.h:58
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
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:315
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::TupleToStringF
std::string TupleToStringF(const std::string &form, const std::tuple< Ts... > &tuple)
Definition: StringHelperTemplates.h:90