io_utils.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2020 CNRS-UM LIRMM, CNRS-AIST JRL
3  */
4 
5 #pragma once
6 #include <algorithm>
7 #include <functional>
8 #include <limits>
9 #include <sstream>
10 #include <string>
11 
12 namespace mc_rtc
13 {
14 namespace io
15 {
16 
29 template<typename Container,
30  typename std::enable_if<!std::is_arithmetic<typename Container::value_type>::value, int>::type = 0>
31 std::string to_string(const Container & c, const std::string & delimiter = ", ")
32 {
33  if(c.cbegin() == c.cend()) { return ""; }
34  std::string out = *c.cbegin();
35  for(auto it = std::next(c.cbegin()); it != c.cend(); ++it)
36  {
37  out += delimiter;
38  out += *it;
39  }
40  return out;
41 }
42 
49 template<typename Container,
50  typename std::enable_if<std::is_arithmetic<typename Container::value_type>::value, int>::type = 0>
51 std::string to_string(const Container & c,
52  const std::string & delimiter = ", ",
53  const unsigned precision = std::numeric_limits<typename Container::value_type>::digits10)
54 {
55  if(c.cbegin() == c.cend()) { return ""; }
56  std::ostringstream out;
57  out.precision(precision);
58  out << std::fixed << *c.cbegin();
59  for(auto it = std::next(c.cbegin()); it != c.cend(); ++it)
60  {
61  out << delimiter;
62  out << std::fixed << *it;
63  }
64  return out.str();
65 }
66 
84 template<typename Container,
85  typename Callback,
86  typename std::enable_if<!std::is_convertible<Callback, std::string>::value, int>::type = 0>
87 std::string to_string(const Container & c, Callback && get_value, const std::string & delimiter = ", ")
88 {
89  if(c.cbegin() == c.cend()) { return ""; }
90  std::string out = get_value(*c.cbegin());
91  for(auto it = std::next(c.cbegin()); it != c.cend(); ++it)
92  {
93  out += delimiter;
94  out += get_value(*it);
95  }
96  return out;
97 }
98 
99 } // namespace io
100 } // namespace mc_rtc
mc_rtc::io::to_string
std::string to_string(const Container &c, const std::string &delimiter=", ")
Definition: io_utils.h:31
mc_rtc
Definition: Contact.h:87