CNORXZ
Container with Native Operation Routines and Expressions
Loading...
Searching...
No Matches
to_string.cc.h
Go to the documentation of this file.
1// -*- C++ -*-
14#ifndef __cxz_to_string_cc_h__
15#define __cxz_to_string_cc_h__
16
17#include "to_string.h"
18#include "iter.h"
19#include <sstream>
20
21namespace CNORXZ
22{
23 template <typename T>
25 {
26 std::stringstream ss;
27 ss << a;
28 return ss.str();
29 }
30
31 template <typename T>
33 {
34 std::stringstream ss;
35 ss << "[";
36 if(a.size() == 0){
37 ss << "]";
38 }
39 else {
40 auto it = a.begin();
41 for(; it != a.end()-1; ++it){
42 ss << toString(*it) << ",";
43 }
44 ss << toString(*it) << "]";
45 }
46 return ss.str();
47 }
48
49 template <typename T, size_t N>
51 {
52 std::stringstream ss;
53 ss << "(";
54 if constexpr(N == 0){
55 ss << ")";
56 }
57 else {
58 auto it = a.begin();
59 for(; it != a.end()-1; ++it){
60 ss << toString(*it) << ",";
61 }
62 ss << toString(*it) << ")";
63 }
64 return ss.str();
65 }
66
67 template <typename... Ts>
68 String ToString<Tuple<Ts...>>::func(const Tuple<Ts...>& t)
69 {
70 const String blim = "(";
71 const String elim = ")";
72 const String dlim = ",";
73 return iter<1,sizeof...(Ts)>
74 ( [&](auto i) { return toString(std::get<i>(t)); },
75 [&](const auto&... xs) {
76 return blim + toString(std::get<0>(t)) + ( (dlim + xs) + ... ) + elim;
77 } );
78 }
79
80 template <typename T, typename S>
81 String ToString<std::pair<T,S>>::func(const std::pair<T,S>& p)
82 {
83 return String("(") + toString(p.first) + "," + toString(p.second) + ")";
84 }
85
86 template <typename T, typename S>
87 String ToString<std::map<T,S>>::func(const std::map<T,S>& p)
88 {
89 std::stringstream ss;
90 ss << "{";
91 if(p.size() == 0){
92 ss << "}";
93 }
94 else {
95 auto it = p.begin();
96 auto e = p.end();
97 e--;
98 for(; it != e; ++it){
99 ss << toString(it->first) << ":" << toString(it->second) << ",";
100 }
101 ss << toString(it->first) << ":" << toString(it->second) << "}";
102 }
103 return ss.str();
104 }
105
106 template <typename T>
107 String toString(const T& a)
108 {
109 return ToString<T>::func(a);
110 }
111}
112
113#endif
Static for-loops.
std::string String
Definition types.h:42
std::vector< T, Allocator< T > > Vector
Definition types.h:310
Sptr< Range > rangeCast(const RangePtr r)
std::tuple< T... > Tuple
Definition types.h:60
std::array< T, N > Arr
Definition types.h:45
String toString(const T &a)
constexpr decltype(auto) iter(const G &g, const F &f)
Definition iter.cc.h:34
static String func(const T &a)
String converter declarations.