various ToString improvements
parent
a49716034e
commit
c1be7f097d
|
@ -183,22 +183,24 @@ namespace octa {
|
|||
return r;
|
||||
}
|
||||
|
||||
using std::initializer_list;
|
||||
|
||||
template<typename T>
|
||||
inline T min(InitializerList<T> il) {
|
||||
inline T min(initializer_list<T> il) {
|
||||
return min_element(each(il)).first();
|
||||
}
|
||||
template<typename T, typename C>
|
||||
inline T min(InitializerList<T> il, C compare) {
|
||||
inline T min(initializer_list<T> il, C compare) {
|
||||
return min_element(each(il), compare).first();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline T max(InitializerList<T> il) {
|
||||
inline T max(initializer_list<T> il) {
|
||||
return max_element(each(il)).first();
|
||||
}
|
||||
|
||||
template<typename T, typename C>
|
||||
inline T max(InitializerList<T> il, C compare) {
|
||||
inline T max(initializer_list<T> il, C compare) {
|
||||
return max_element(each(il), compare).first();
|
||||
}
|
||||
|
||||
|
|
|
@ -20,16 +20,6 @@ namespace std {
|
|||
|
||||
initializer_list(const T *v, size_t n): p_buf(v), p_len(n) {}
|
||||
public:
|
||||
typedef size_t SizeType;
|
||||
typedef ptrdiff_t DiffType;
|
||||
typedef T ValType;
|
||||
typedef const T &RefType;
|
||||
typedef const T &ConstRefType;
|
||||
typedef const T *PtrType;
|
||||
typedef const T *ConstPtrType;
|
||||
typedef octa::PointerRange<const T> RangeType;
|
||||
typedef octa::PointerRange<const T> ConstRangeType;
|
||||
|
||||
initializer_list(): p_buf(nullptr), p_len(0) {}
|
||||
|
||||
size_t size() const { return p_len; }
|
||||
|
@ -42,16 +32,15 @@ namespace std {
|
|||
#include <initializer_list>
|
||||
#endif
|
||||
|
||||
namespace std {
|
||||
namespace octa {
|
||||
using std::initializer_list;
|
||||
|
||||
template<typename T> using InitializerList = std::initializer_list<T>;
|
||||
|
||||
template<typename T>
|
||||
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
|
|
@ -14,6 +14,8 @@
|
|||
#include "octa/vector.h"
|
||||
|
||||
namespace octa {
|
||||
using std::initializer_list;
|
||||
|
||||
static constexpr size_t npos = -1;
|
||||
|
||||
template<typename T>
|
||||
|
@ -160,6 +162,16 @@ namespace octa {
|
|||
return move(ret);
|
||||
}
|
||||
|
||||
template<typename T, typename F>
|
||||
String concat(initializer_list<T> il, String sep, F func) {
|
||||
return concat(each(il), sep, func);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
String concat(initializer_list<T> il, String sep = " ") {
|
||||
return concat(each(il), sep);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
struct __OctaToStringTest {
|
||||
template<typename U, String (U::*)() const> struct __OctaTest {};
|
||||
|
@ -181,13 +193,27 @@ namespace octa {
|
|||
|
||||
template<typename U>
|
||||
static String __octa_to_str(const U &v,
|
||||
EnableIf<!__OctaToStringTest<U>::value, bool> = true
|
||||
EnableIf<!__OctaToStringTest<U>::value && !IsScalar<U>::value,
|
||||
bool> = true
|
||||
) {
|
||||
return concat(each(v), ", ", ToString<RangeReference<U>>());
|
||||
String ret("{");
|
||||
ret += concat(each(v), ", ", ToString<RangeReference<
|
||||
decltype(each(v))
|
||||
>>());
|
||||
ret += "}";
|
||||
return move(ret);
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
static String __octa_to_str(const U &v,
|
||||
EnableIf<!__OctaToStringTest<U>::value && IsScalar<U>::value,
|
||||
bool> = true
|
||||
) {
|
||||
return ToString<U>()(v);
|
||||
}
|
||||
|
||||
String operator()(const T &v) {
|
||||
return move(__octa_to_str(v));
|
||||
return move(__octa_to_str<RemoveCv<RemoveReference<T>>>(v));
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -238,6 +264,7 @@ namespace octa {
|
|||
};
|
||||
|
||||
__OCTA_TOSTR_NUM(int, "%d")
|
||||
__OCTA_TOSTR_NUM(int &, "%d")
|
||||
__OCTA_TOSTR_NUM(uint, "%u")
|
||||
__OCTA_TOSTR_NUM(long, "%ld")
|
||||
__OCTA_TOSTR_NUM(ulong, "%lu")
|
||||
|
@ -249,11 +276,21 @@ namespace octa {
|
|||
|
||||
#undef __OCTA_TOSTR_NUM
|
||||
|
||||
template<typename T> struct ToString<T *> {
|
||||
typedef T *ArgType;
|
||||
typedef String ResultType;
|
||||
String operator()(ArgType v) {
|
||||
String ret;
|
||||
__octa_str_printf((Vector<char> *)&ret, "%p", v);
|
||||
return move(ret);
|
||||
}
|
||||
};
|
||||
|
||||
template<> struct ToString<String> {
|
||||
typedef const String &ArgType;
|
||||
typedef String ResultType;
|
||||
String operator()(ArgType s) {
|
||||
return String(s);
|
||||
return s;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -262,7 +299,7 @@ namespace octa {
|
|||
typedef String ResultType;
|
||||
String operator()(ArgType v) {
|
||||
String ret("{");
|
||||
ret += concat(v.each(), ", ", ToString<T>());
|
||||
ret += concat(v.each(), ", ", ToString<RemoveCv<RemoveReference<T>>>());
|
||||
ret += "}";
|
||||
return move(ret);
|
||||
}
|
||||
|
@ -273,17 +310,30 @@ namespace octa {
|
|||
typedef String ResultType;
|
||||
String operator()(ArgType v) {
|
||||
String ret("{");
|
||||
ret += ToString<T>()(v.first);
|
||||
ret += ToString<RemoveCv<RemoveReference<T>>>()(v.first);
|
||||
ret += ", ";
|
||||
ret += ToString<U>()(v.second);
|
||||
ret += ToString<RemoveCv<RemoveReference<U>>>()(v.second);
|
||||
ret += "}";
|
||||
return move(ret);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T> struct ToString<initializer_list<T>> {
|
||||
typedef initializer_list<T> ArgType;
|
||||
typedef String ResultType;
|
||||
String operator()(ArgType il) {
|
||||
return move(ToString<decltype(each(il))>()(each(il)));
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
String to_string(const T &v) {
|
||||
return move(ToString<T>()(v));
|
||||
return move(ToString<RemoveCv<RemoveReference<T>>>()(v));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
String to_string(initializer_list<T> init) {
|
||||
return move(ToString<initializer_list<T>>()(init));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue