Commit b6da4171 authored by Tom Niget's avatar Tom Niget

Fix str and repr for all objects

parent 4827539e
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
namespace view = std::views; namespace view = std::views;
#include "print.hpp" #include "str.hpp"
#include <python/basedef.hpp> #include <python/basedef.hpp>
namespace typon { namespace typon {
...@@ -24,6 +24,14 @@ struct TyCell__oo : classtype<_Base0, TyCell__oo<>> { ...@@ -24,6 +24,14 @@ struct TyCell__oo : classtype<_Base0, TyCell__oo<>> {
}; };
template <typename T> auto operator()(T val) const { return Obj<T>{val}; } 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> template <typename _Base0 = object>
......
...@@ -248,6 +248,8 @@ struct TyStr__oo : classtype<_Base0, TyStr__oo<>> { ...@@ -248,6 +248,8 @@ struct TyStr__oo : classtype<_Base0, TyStr__oo<>> {
constexpr Obj() : value() {} constexpr Obj() : value() {}
constexpr Obj(std::string value) : value(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) {} constexpr Obj(const char* value, size_t count) : value(value, count) {}
operator std::string() const { return value; } operator std::string() const { return value; }
operator std::string_view() const { return value; } operator std::string_view() const { return value; }
...@@ -287,14 +289,23 @@ template <NoStr T> auto str(const T &x) { ...@@ -287,14 +289,23 @@ template <NoStr T> auto str(const T &x) {
*/ */
template <typename T> 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> template <typename T>
concept HasRepr = requires(T x) { dot(x, oo__repr__oo)(); }; 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) { template <typename T> auto str(const T &x) {
if constexpr (HasStr<T>) { if constexpr (HasStr<T>) {
return dot(x, oo__str__oo)(); 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 { } else {
std::stringstream s; std::stringstream s;
s << x; s << x;
...@@ -305,6 +316,8 @@ template <typename T> auto str(const T &x) { ...@@ -305,6 +316,8 @@ template <typename T> auto str(const T &x) {
template <typename T> auto repr(const T &x) { template <typename T> auto repr(const T &x) {
if constexpr (HasRepr<T>) { if constexpr (HasRepr<T>) {
return dot(x, oo__repr__oo)(); return dot(x, oo__repr__oo)();
} else if constexpr (HasRefModelRepresentation<T>) {
return "<"_ps + typon::TyStr__oo<>::Obj(x->repr) + ">"_ps;
} else { } else {
std::stringstream s; std::stringstream s;
s << x; 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