cnorxz/src/include/ranges/x_to_string.h

128 lines
2.6 KiB
C
Raw Normal View History

#ifndef __x_to_string_h__
#define __x_to_string_h__
#include <string>
#include <vector>
#include <array>
#include <tuple>
#include <algorithm>
2018-10-29 14:19:42 +01:00
#include "ranges/dynamic_meta.h"
namespace MultiArrayHelper
{
2018-09-17 11:06:24 +02:00
template <typename T>
inline std::string xToString(const T& x);
template <>
inline std::string xToString<char>(const char& x);
template <>
inline std::string xToString<std::string>(const std::string& x);
2018-10-29 14:19:42 +01:00
using MultiArrayTools::DynamicMetaT;
template <>
inline std::string xToString<DynamicMetaT>(const DynamicMetaT& x);
2018-09-17 11:06:24 +02:00
template <typename T>
2019-02-13 21:59:13 +01:00
inline std::string xToString(const vector<T>& x);
2018-09-17 11:06:24 +02:00
template <typename T, size_t N>
inline std::string xToString(const std::array<T,N>& x);
template <typename... Ts>
inline std::string xToString(const std::tuple<Ts...>& tp);
// TEMPLATE CODE
template <typename T>
inline std::string xToString(const T& x)
{
std::string out = std::to_string(x);
std::replace(out.begin(), out.end(), ',', '.');
return out;
}
template <size_t N>
struct TupleToString
{
template <typename... Ts>
static inline std::string mk(const std::tuple<Ts...>& tp)
{
return TupleToString<N-1>::mk(tp) + "," + xToString(std::get<N>(tp));
}
};
template <>
struct TupleToString<0>
{
template <typename... Ts>
static inline std::string mk(const std::tuple<Ts...>& tp)
{
return xToString(std::get<0>(tp));
}
};
template <>
inline std::string xToString<char>(const char& x)
{
std::string out = "";
return out += x;
}
template <>
inline std::string xToString<std::string>(const std::string& x)
{
return x;
}
2018-10-29 14:19:42 +01:00
template <>
inline std::string xToString<DynamicMetaT>(const DynamicMetaT& x)
{
std::string out = "[";
2018-10-29 14:19:42 +01:00
for(size_t i = 0; i != x.size(); ++i){
out += x[i].first;
out += ",";
2018-10-29 14:19:42 +01:00
}
//out.pop_back();
2018-10-29 14:19:42 +01:00
out.back() = ']';
return out;
}
template <typename T>
2019-02-13 21:59:13 +01:00
inline std::string xToString(const vector<T>& x)
{
std::string out = "[";
for(auto& y: x){
out += xToString(y) + ",";
}
//out.pop_back();
out.back() = ']';
return out;
}
template <typename T, size_t N>
inline std::string xToString(const std::array<T,N>& x)
{
std::string out = "[";
for(auto& y: x){
out += xToString(y) + ",";
}
//out.pop_back();
out.back() = ']';
return out;
}
template <typename... Ts>
inline std::string xToString(const std::tuple<Ts...>& tp)
{
return "{" + TupleToString<sizeof...(Ts)-1>::mk(tp) + "}";
}
}
#endif