Commit b6da4171 authored by Tom Niget's avatar Tom Niget

Fix str and repr for all objects

parent 4827539e
......@@ -6,7 +6,7 @@
namespace view = std::views;
#include "print.hpp"
#include "str.hpp"
#include <python/basedef.hpp>
namespace typon {
......@@ -24,6 +24,14 @@ struct TyCell__oo : classtype<_Base0, TyCell__oo<>> {
};
template <typename T> auto operator()(T val) const { return Obj<T>{val}; }
struct : method {
auto operator()(auto self) const {
std::stringstream s;
s << "Cell(" << repr(self->val)->value << ')';
return typon::TyStr__oo<>::Obj(s.str());
}
} static constexpr oo__str__oo{};
};
template <typename _Base0 = object>
......
......@@ -248,6 +248,8 @@ struct TyStr__oo : classtype<_Base0, TyStr__oo<>> {
constexpr Obj() : value() {}
constexpr Obj(std::string value) : value(value) {}
constexpr Obj(const std::string_view &value) : value(value) {}
constexpr Obj(const char* value) : value(value) {}
constexpr Obj(const char* value, size_t count) : value(value, count) {}
operator std::string() const { return value; }
operator std::string_view() const { return value; }
......@@ -287,14 +289,23 @@ template <NoStr T> auto str(const T &x) {
*/
template <typename T>
concept HasStr = requires(T x) { dot(x, oo__str__oo)(); };
concept HasStr = requires(T x) { x->oo__str__oo; };
template <typename T>
concept HasRepr = requires(T x) { dot(x, oo__repr__oo)(); };
template <typename T>
concept HasRefModelRepresentation = requires {
std::declval<T>()->repr;
};
template <typename T> auto str(const T &x) {
if constexpr (HasStr<T>) {
return dot(x, oo__str__oo)();
} else if constexpr (HasRepr<T>) {
return dot(x, oo__repr__oo)();
} else if constexpr (HasRefModelRepresentation<T>) {
return "<"_ps + typon::TyStr__oo<>::Obj(x->repr) + ">"_ps;
} else {
std::stringstream s;
s << x;
......@@ -305,6 +316,8 @@ template <typename T> auto str(const T &x) {
template <typename T> auto repr(const T &x) {
if constexpr (HasRepr<T>) {
return dot(x, oo__repr__oo)();
} else if constexpr (HasRefModelRepresentation<T>) {
return "<"_ps + typon::TyStr__oo<>::Obj(x->repr) + ">"_ps;
} else {
std::stringstream s;
s << x;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment