begin to_string infra

master
Daniel Kolesa 2015-05-28 02:38:52 +01:00
parent 84336cabef
commit a49716034e
4 changed files with 90 additions and 6 deletions

View File

@ -10,6 +10,7 @@
#include "octa/algorithm.h"
#include "octa/range.h"
#include "octa/string.h"
namespace octa {
template<typename T, size_t N>
@ -60,6 +61,13 @@ namespace octa {
swap_ranges(each(), v.each());
}
String to_string() const {
String ret("{");
ret += concat(each(), ", ", ToString<T>());
ret += "}";
return move(ret);
}
T p_buf[(N > 0) ? N : 1];
};
}

View File

@ -42,13 +42,16 @@ namespace std {
#include <initializer_list>
#endif
namespace octa {
template<typename T> using InitializerList = std::initializer_list<T>;
namespace std {
template<typename T>
PointerRange<const T> each(InitializerList<T> init) {
return PointerRange<const T>(init.begin(), init.end());
octa::PointerRange<const T> each(initializer_list<T> init) {
return octa::PointerRange<const T>(init.begin(), init.end());
}
}
namespace octa {
template<typename T> using InitializerList = std::initializer_list<T>;
using std::each;
}
#endif

View File

@ -158,6 +158,14 @@ namespace octa {
SizeType pop_last_n(SizeType n) {
return __octa_pop_last_n<B>(*((B *)this), n);
}
B each() {
return B(*((B *)this));
}
B each() const {
return B(*((B *)this));
}
};
template<typename V, typename R = V &, typename S = size_t>

View File

@ -160,10 +160,35 @@ namespace octa {
return move(ret);
}
template<typename T>
struct __OctaToStringTest {
template<typename U, String (U::*)() const> struct __OctaTest {};
template<typename U> static char __octa_test(__OctaTest<U, &U::to_string> *);
template<typename U> static int __octa_test(...);
static constexpr bool value = (sizeof(__octa_test<T>(0)) == sizeof(char));
};
template<typename T> struct ToString {
typedef T ArgType;
typedef String ResultType;
String operator()(const T &) { return ""; }
template<typename U>
static String __octa_to_str(const U &v,
EnableIf<__OctaToStringTest<U>::value, bool> = true
) {
return v.to_string();
}
template<typename U>
static String __octa_to_str(const U &v,
EnableIf<!__OctaToStringTest<U>::value, bool> = true
) {
return concat(each(v), ", ", ToString<RangeReference<U>>());
}
String operator()(const T &v) {
return move(__octa_to_str(v));
}
};
template<typename T>
@ -183,6 +208,14 @@ namespace octa {
*(((size_t *)s) + 1) = n + 1;
}
template<> struct ToString<bool> {
typedef bool ArgType;
typedef String ResultType;
String operator()(bool b) {
return b ? "true" : "false";
}
};
template<> struct ToString<char> {
typedef char ArgType;
typedef String ResultType;
@ -216,6 +249,38 @@ namespace octa {
#undef __OCTA_TOSTR_NUM
template<> struct ToString<String> {
typedef const String &ArgType;
typedef String ResultType;
String operator()(ArgType s) {
return String(s);
}
};
template<typename T> struct ToString<Vector<T>> {
typedef const Vector<T> &ArgType;
typedef String ResultType;
String operator()(ArgType v) {
String ret("{");
ret += concat(v.each(), ", ", ToString<T>());
ret += "}";
return move(ret);
}
};
template<typename T, typename U> struct ToString<Pair<T, U>> {
typedef const Pair<T, U> &ArgType;
typedef String ResultType;
String operator()(ArgType v) {
String ret("{");
ret += ToString<T>()(v.first);
ret += ", ";
ret += ToString<U>()(v.second);
ret += "}";
return move(ret);
}
};
template<typename T>
String to_string(const T &v) {
return move(ToString<T>()(v));