Compare commits

...

3 Commits

Author SHA1 Message Date
Avril 4698b48344
Improving error interface: Begin working on `Box<T>` to allow any `T` to tell if it is owned by a `Box<T>`.
2 years ago
Avril 2d30b3ce85
Adding polymorphic boxing helpers.
2 years ago
Avril 044006aef0
Began rework of Error/TracedError interface.
2 years ago

@ -10,12 +10,15 @@
#include "exopt.h" #include "exopt.h"
namespace exopt::types { namespace boxed { namespace exopt::types { namespace boxed {
template<typename T> template<typename T>
struct [[gnu::visibility("internal")]] boxable_value_type { using type = std::conditional_t struct [[gnu::visibility("internal")]] boxable_value_type { using type = std::conditional_t
<std::is_reference_v<T> <std::is_reference_v<T>
,std::reference_wrapper<std::remove_cvref_t<T> > ,std::reference_wrapper<std::remove_cvref_t<T> >
,T>; }; ,T>; };
namespace {
template<typename T> template<typename T>
constexpr std::unique_ptr<T> uniq_clone(std::unique_ptr<T> const& c) noexcept(std::is_nothrow_copy_constructible_v<T>) requires(std::is_copy_constructible_v<T>) constexpr std::unique_ptr<T> uniq_clone(std::unique_ptr<T> const& c) noexcept(std::is_nothrow_copy_constructible_v<T>) requires(std::is_copy_constructible_v<T>)
{ if(__builtin_expect(bool(c), true)) return std::make_unique<T>(*c); return nullptr; } { if(__builtin_expect(bool(c), true)) return std::make_unique<T>(*c); return nullptr; }
@ -35,10 +38,96 @@ namespace exopt::types { namespace boxed {
return std::make_unique<T>(*c); } return std::make_unique<T>(*c); }
template<typename Error = ptr::NullException>
constexpr decltype(auto) force_ok(auto&& mv) requires(std::is_convertible_v<decltype(mv), bool> && std::is_default_constructible_v<Error>)
{
struct err {
[[gnu::noinline, gnu::cold, noreturn]]
static constexpr void _throw() { throw Error{}; }
};
if( __builtin_expect(!bool(mv), false)) err::_throw();
return std::forward<decltype(mv)>(mv);
}
} // _impl (anon)
template<typename From, typename To>
concept binary_convertible = std::is_convertible_v<From, To> and std::is_convertible_v<To, From>;
template<typename From, typename To>
concept polymorphic_castable = std::derived_from<std::decay_t<To>, std::decay_t<From>>
or std::derived_from<std::decay_t<From>, std::decay_t<To>>
or std::is_same_v<std::decay_t<From>, std::decay_t<To>>
or requires(std::decay_t<From> && from) {
{ static_cast<std::decay_t<To> &&>(from) } -> std::convertible_to<To>;
};
template<typename To, typename From> requires(polymorphic_castable<From, To>)
constexpr std::unique_ptr<To> static_uniq_cast(std::unique_ptr<From>&& from) noexcept
{
return std::unique_ptr<To> {
static_cast<To*>( std::move(from).release() )
};
}
template<typename To, typename From> requires(polymorphic_castable<From, To>)
constexpr std::unique_ptr<To> dynamic_uniq_cast(std::unique_ptr<From>&& from) noexcept
{
return std::unique_ptr<To> {
dynamic_cast<To*>( std::move(from).release() )
};
}
template<typename To, typename From, bool Check = false> requires(polymorphic_castable<From, To>)
constexpr std::unique_ptr<To> uniq_cast(std::unique_ptr<From>&& from) noexcept
{
if constexpr(Check) return dynamic_uniq_cast(std::move(from));
else return static_uniq_cast(std::move(from));
}
#if 0
//TODO: Cannot be implemented with this method
constexpr bool is_boxed_value(auto const& value) noexcept {
return bool(dynamic_cast<ObjectBase const*>(std::addressof(value)));
}
template<typename T> template<typename T>
struct Box { constexpr bool is_boxed_type() noexcept {
using type = std::remove_reference_t<T>;
constexpr type const* VALUE = nullptr;
return bool(dynamic_cast<ObjectBase const*>(VALUE));
}
// We need either: Tagged pointers (XOR, bit-mangled, manual heap, or GOT (external allocation) kind), \
or (preferred): layout of Box<T> to be `{ aligned<T>(T, box_type_tag) } unique*` \
box_type_tag should be: One pointer width, starting at offset *T+1 (aligned to T within internal tuple struct), provides a way to access RTTI for `*T`, a pointer to static (non-allocated) type information or dynamic_cast used lambda would be ideal.
#endif
template<typename T>
struct Box final {
typedef boxable_value_type<T>::type type; typedef boxable_value_type<T>::type type;
template<std::derived_from<T> U, bool RuntimeCheck=false>
constexpr static Box<T> new_dynamic(U&& v) noexcept(!RuntimeCheck) {
if constexpr(!RuntimeCheck) {
auto boxed = std::make_unique<U>(std::move(v));
return Box<T>{std::unique_ptr<T>{static_cast<T*>(boxed.release())}};
} else
return Box<T>{std::make_unique<T>(dynamic_cast<T&&>(std::move(v)))};
}
template<std::derived_from<T> U, bool Check = false>
constexpr Box<U> upcast() && noexcept(!Check)
{ return Box<U> { force_ok(uniq_cast<U, T, Check>(std::move(m_ptr))) }; }
template<typename U, bool Check = true> requires(std::derived_from<T, U>)
constexpr Box<U> downcast() && noexcept(!Check)
{ return Box<U> { force_ok(uniq_cast<U, T, Check>(std::move(m_ptr))) }; }
template<polymorphic_castable<T> U, bool Check = !std::derived_from<U, T> > // Default dynamic check set to false only if we can statically determine that T is derived from U and the conversion is therfore infallible
constexpr Box<U> polycast() && noexcept(!Check)
{ return Box<U> { force_ok(uniq_cast<U, T, Check>(std::move(m_ptr))) }; }
constexpr Box() noexcept(std::is_nothrow_default_constructible_v<T>) requires(std::is_default_constructible_v<T>) constexpr Box() noexcept(std::is_nothrow_default_constructible_v<T>) requires(std::is_default_constructible_v<T>)
: m_ptr{std::make_unique<T>()} {} : m_ptr{std::make_unique<T>()} {}
@ -90,9 +179,35 @@ namespace exopt::types { namespace boxed {
_EO_ASSUME(m_ptr.get()); _EO_ASSUME(m_ptr.get());
} }
#if 0
// TODO: Identifying if a value `this` pointer is boxed
struct alignas(type) inner_layout {
constexpr inner_layout() noexcept = default;
constexpr inner_layout(const inner_layout&) noexcept = default;
constexpr ~inner_layout() noexcept = default;
type value;
box_type_tag identifier{box_tag_of<T>()};
};
#endif
std::unique_ptr<T> m_ptr; // Unique<T> m_ptr; std::unique_ptr<T> m_ptr; // Unique<T> m_ptr;
}; };
#define _EO_ADD_RV std::add_rvalue_reference_v
template<typename Base, typename T = Base>
constexpr inline bool is_boxable_v = binary_convertible<_EO_ADD_RV<T>, _EO_ADD_RV<Base> > and requires(T&& value) {
typename Box<Base>;
typename Box<Base>::type;
{ std::make_unique<T>(std::move(value)) } -> std::same_as<std::unique_ptr<T>>;
};
template<typename T> concept is_boxable = is_boxable_v<T>;
template<typename T>
constexpr inline bool is_nothrow_boxable_v = is_boxable_v<T> && std::is_nothrow_constructible_v<Box<T>, T>;
#undef _EO_ADD_RV
template<typename T, typename U> requires(requires(T&& o) { static_cast<U&&>(o); }) template<typename T, typename U> requires(requires(T&& o) { static_cast<U&&>(o); })
constexpr Box<U> static_box_cast(Box<T>&& b) noexcept { return Box<U>::from_raw_ptr(static_cast<U*>(b.release())); } constexpr Box<U> static_box_cast(Box<T>&& b) noexcept { return Box<U>::from_raw_ptr(static_cast<U*>(b.release())); }
@ -106,6 +221,44 @@ namespace exopt::types { namespace boxed {
throw; throw;
} }
} //TODO: Overload for `const&` that does the type check *before* the copy allocation. } //TODO: Overload for `const&` that does the type check *before* the copy allocation.
#if 0
template<typename T, typename R /*XXX: Maybe remove this, and put the requires after the definition, using `auto&& value` instead of `R&& value`*/> requires(std::derived_from<R, T>)
constexpr Box<T> box(R&& value, util::comptime<bool> auto Check = _EO_CONSTANT_VALUE(false)) noexcept(std::is_nothrow_invocable_v< Box<T>::new_dynamic<R, util::comptime_eval_v<bool, Check>>) //XXX: This seems illegal...
{ return Box<T>::template new_dynamic<R, Check>(std::move(value)); }
template<typename T>
constexpr Box<T> box(T&& value) noexcept(std::is_nothrow_constructible_v<Box<T>, T>)
{ return { std::move(value) }; }
#endif
template<typename T>
constexpr Box<T> box(T&& value) noexcept(std::is_nothrow_constructible_v<Box<T>, decltype(value)>)
{
/*if constexpr(requires(decltype(value) v) { static_cast<T&&>(std::move(v)); }) {
return Box<T>::template new_dynamic<T>(std::move(value));
} else */return Box<T> { std::move(value) };
}
template<typename T>
constexpr Box<T> box(std::unique_ptr<T>&& value) noexcept { return Box<T> { std::move(value) }; }
template<typename Base, typename T> /*requires(polymorphic_castable<Base, T>)
constexpr Box<Base> polybox(T&& value) noexcept
{
return Box<T>{ std::move(value) }.polycast<Base>()
}*/ requires(std::derived_from<T, Base>)
constexpr Box<Base> polybox(T&& value) noexcept {
return Box<T>{ std::move(value) }.polycast<Base, false>(); // Convertion from derived to base infallible, no need for check.
}
template<typename Base, typename T> /*requires(polymorphic_castable<Base, T>)
constexpr Box<Base> polybox(T&& value) noexcept
{
return Box<T>{ std::move(value) }.polycast<Base>()
}*/ requires(std::derived_from<T, Base>)
constexpr Box<Base> polybox(std::unique_ptr<T>&& value) noexcept {
return Box<T>{ std::move(value) }.polycast<Base, false>(); // Convertion from derived to base infallible, no need for check.
}
} }
using boxed::Box; using boxed::Box;
} }

@ -11,13 +11,24 @@
#include "util.hh" #include "util.hh"
#define _EO_DEFEXCEPT_BASIC_(NAME, BASE, ...) class NAME : public ::exopt:: BASE { \
constexpr static inline auto MESSSAGE = (__VA_ARGS__); \
public: \
_EO_CLASS_DEFINE(NAME, constexpr, noexcept, virtual, =default); \
constexpr virtual std::string_view message() const noexcept override { return { MESSAGE }; } \
}
#define _EO_DEFEXCEPT_BASIC(NAME, MSG) _EO_DEFEXCEPT_BASIC_(NAME, Error, MSG)
#define _EO_DEFEXCEPT_TRACE_BASIC(NAME, MSG) _EO_DEFEXCEPT_BASIC_(NAME, TracedError, MSG)
namespace exopt { namespace exopt {
// Report generated from an error. (virtual base, not abstract.) // Report generated from an error. (virtual base, not abstract.)
class Report { class Report {
public: public:
virtual ~Report(); virtual ~Report();
}; };
// This is propagated returned (base, abstract)
// This is base thrown (base, abstract)
class Error { class Error {
public: public:
constexpr Error(Error const&) = default; constexpr Error(Error const&) = default;
@ -25,6 +36,9 @@ namespace exopt {
constexpr Error& operator=(Error &&) = default; constexpr Error& operator=(Error &&) = default;
constexpr Error& operator=(Error const&) = default; constexpr Error& operator=(Error const&) = default;
virtual types::Box<TracedError> into_traced(std::stacktrace trace&&) &&noexcept;// = std::stacktrace::current()) && noexcept;
inline typed::Box<TracedError> into_traced(std::stacktrace trace = std::stacktrace::current()) && noexcept
{ return std::move(*this).into_traced(std::move(trace)); }
constexpr virtual std::source_location const& location() const noexcept { return m_location; } constexpr virtual std::source_location const& location() const noexcept { return m_location; }
@ -32,8 +46,9 @@ namespace exopt {
constexpr virtual std::string_view message() const noexcept =0; constexpr virtual std::string_view message() const noexcept =0;
constexpr virtual types::Option<Error&> inner() noexcept { return { std::nullopt }; } //constexpr virtual types::Option<Error&> inner() noexcept { return { std::nullopt }; }
constexpr virtual types::MaybeOwned<Report> into_report() noexcept { /* TODO */ } //TODO: Maybe just use `std::optional<std::reference_wrapper<Error>>`? Or Box<Report&>? constexpr virtual types::Option<Error const&> inner() const noexcept { return { std::nullopt }; }
constexpr virtual types::MaybeOwned<Report> into_report() & noexcept { /* TODO */ } //TODO: Maybe just use `std::optional<std::reference_wrapper<Error>>`? Or Box<Report&>?
constexpr virtual types::MaybeOwned<Report> into_report() && noexcept { /* TODO: auto{*this}.into_report() */ } constexpr virtual types::MaybeOwned<Report> into_report() && noexcept { /* TODO: auto{*this}.into_report() */ }
constexpr virtual ~Error() = default; constexpr virtual ~Error() = default;
@ -47,7 +62,7 @@ namespace exopt {
}; };
// This is propagated returned, along with a stack trace. (base, abstract) // This is propagated returned, along with a stack trace. (base, abstract)
class TracedError : Error { class TracedError : public Error {
public: public:
TracedError(TracedError const &) = default; TracedError(TracedError const &) = default;
TracedError(TracedError &&) = default; TracedError(TracedError &&) = default;
@ -62,13 +77,27 @@ namespace exopt {
std::source_location loc = std::source_location::current() std::source_location loc = std::source_location::current()
, std::stacktrace trace = std::stacktrace::current()) noexcept , std::stacktrace trace = std::stacktrace::current()) noexcept
: Error(std::move(loc)), m_trace(std::move(trace)) {} : Error(std::move(loc)), m_trace(std::move(trace)) {}
/*inline TracedError(
std::stacktrace&& trace,
, std::source_location loc = std::source_location::current()) noexcept
: Error(std::move(loc)), m_trace(std::move(trace)) {}*/
inline TracedError(
std::stacktrace&& trace,
, std::source_location&& loc) noexcept
: Error(std::move(loc)), m_trace(std::move(trace)) {}
private: private:
inline types::Box<TracedError> into_traced(std::stacktrace trace&&) &&noexcept override final// = std::stacktrace::current()) && noexcept;
{ return typed::Box<TracedError> { std::move(*this) }; } //TODO: use `boxed::is_boxed_value()` to check if *this is boxed... Uhh, actually... XXX: No that doesn't work, since `Box<T>` doesn't make `T` visible to `boxed::ObjectBase`.... Eh...
inline typed::Box<TracedError> into_traced(std::stacktrace trace = std::stacktrace::current()) && noexcept override final
{ return typed::Box<TracedError> { std::move(*this) }; }
std::stacktrace m_trace; //TODO: Maybe box this? idk how big it is... std::stacktrace m_trace; //TODO: Maybe box this? idk how big it is...
}; };
#if 0
// This is thrown. (virtual base, abstract) // This is thrown. (virtual base, abstract)
class Panic : /*virtual?? I think it should be XXX*/ Error { class Panic : /*virtual?? I think it should be XXX*/ Error {
//TODO: How to make a `constexpr` deduced panic that has stack-trace at runtime only? Is it needed? Hnm... //TODO: How to make a `constexpr` deduced panic that has stack-trace at runtime only? Is it needed? Hnm...
}; };
//TODO: Fatal(string) : public virtual Panic //TODO: Fatal(string) : public virtual Panic
#endif
} }

@ -27,11 +27,43 @@ extern "C" {
# define _EO_ASSUME(X) ({ if(! (X)) __builtin_unreachable(); }) # define _EO_ASSUME(X) ({ if(! (X)) __builtin_unreachable(); })
#endif #endif
#define _EO_NOTHING
#if _EO(cpp) #if _EO(cpp)
# ifndef _EO_ASSUME # ifndef _EO_ASSUME
# define _EO_ASSUME(X) [[gnu::assume(X)]] # define _EO_ASSUME(X) [[gnu::assume(X)]]
# endif # endif
# define _exopt__restrict __restrict__ # define _exopt__restrict __restrict__
#define _EO_CTORS_BASIC_W(N, constexpr, noexcept, ...) \
constexpr N(N const&) noexcept __VA_ARGS__; \
constexpr N(N &&) noexcept __VA_ARGS__; \
constexpr N & operator=(N const&) noexcept __VA_ARGS__; \
constexpr N & operator=(N &&) noexcept __VA_ARGS__
#define _EO_CTORS_BASIC(...) _EO_CTORS_BASIC_W(__VA_ARGS__, _EO_NOTHING, _EO_NOTHING, _EO_NOTHING)
#define _EO_CTORS_BASIC_DEFAULT(...) _EO_CTORS_BASIC(__VA_ARGS__, _EO_NOTHING, _EO_NOTHING, = default)
#define _EO_CTORS_DEFAULT_BASIC_W(N, constexpr, noexcept, ...) \
constexpr N(N const&) noexcept = default; \
constexpr N(N &&) noexcept = default; \
constexpr N & operator=(N const&) noexcept = default; \
constexpr N & operator=(N &&) noexcept = default
#define _EO_CTORS_DEFAULT_BASIC(N) _EO_CTORS_DEFAULT_BASIC_W(N, constexpr, _EO_NOTHING, _EO_NOTHING)
#define _EO_CTORS_DEFAULT_BASIC_RT(N) _EO_CTORS_DEFAULT_BASIC_W(N, _EO_NOTHING, _EO_NOTHING, _EO_NOTHING)
#define _EO_CTORS_DEFAULT_BASIC_NE_W(N, C) _EO_CTORS_DEFAULT_BASIC_W(N, C, noexcept, _EO_NOTHING)
#define _EO_CTORS_DEFAULT_BASIC_NE(N) _EO_CTORS_DEFAULT_BASIC_W(N, constexpr, noexcept, _EO_NOTHING)
#define _EO_CTORS_DEFAULT_BASIC_NE_RT(N) _EO_CTORS_DEFAULT_BASIC_W(N, _EO_NOTHING, noexcept, _EO_NOTHING)
#define _EO_CLASS_DEFAULT_(N, ...) N
#define _EO_CLASS_DEFAULT_2(_, N, ...) N
#define _EO_CLASS_DEFAULT_3(_, __, N, ...) N
#define _EO_CLASS_DEFAULT_4(_, __, ___, N, ...) N
#define _EO_CLASS_DEFAULT_3REST(_, __, ___, ...) __VA_ARGS__
#define _EO_CLASS_DEFAULT_4REST(_, __, ___, ____, ...) __VA_ARGS__
// _EO_CLASS_DEFINE(NAME, [constexpr], [noexcept], [virtual], [=default])
#define _EO_CLASS_DEFINE(...) _EO_CTORS_BASIC(__VA_ARGS__); \
_EO_CLASS_DEFAULT_4(__VA_ARGS__, _EO_NOTHING, _EO_NOTHING, _EO_NOTHING, _EO_NOTHING, _EO_NOTHING) _EO_CLASS_DEFAULT_2(__VA_ARGS__, _EO_NOTHING, _EO_NOTHING) ~ _EO_CLASS_DEFAULT_(__VA_ARGS__, _EO_NOTHING)() _EO_CLASS_DEFAULT_3(__VA_ARGS__, _EO_NOTHING, _EO_NOTHING, _EO_NOTHING) _EO_CLASS_DEFAULT_4REST(__VA_ARGS__, _EO_NOTHING, _EO_NOTHING, _EO_NOTHING, _EO_NOTHING, _EO_NOTHING)
#else #else
# ifndef _EO_ASSUME # ifndef _EO_ASSUME
# define _EO_ASSUME(X) __attribute__((assume(X))) # define _EO_ASSUME(X) __attribute__((assume(X)))

@ -220,6 +220,11 @@ namespace exopt::types { namespace optional [[gnu::visibility("internal")]] {
template<typename T> template<typename T>
using mut_ref_t = typename mut_ref_or_void<T>::type; using mut_ref_t = typename mut_ref_or_void<T>::type;
template<typename T>
using remove_ref_keep_const_t = std::conditional_t,
<std::is_const_v<std::remove_reference_v<T> >
,const std::remove_cvref_t<T>
, std::remove_cvref_t<T> >;
template<typename T> requires(requires(T v) { { v == nullptr } -> std::convertible_to<bool>; }) template<typename T> requires(requires(T v) { { v == nullptr } -> std::convertible_to<bool>; })
[[gnu::always_inline]] [[gnu::always_inline]]
@ -268,7 +273,27 @@ namespace exopt::types { namespace optional [[gnu::visibility("internal")]] {
template<AsOption T> template<AsOption T>
struct Option final { class Option final {
using value_type_correct = _details::remove_ref_keep_const_t<T>;// std::remove_cvref_t<T>;
constexpr static inline bool is_const_v = std::is_const_v<value_type_correct>;
template<typename U, typename V = const std::remove_const_t<U> >
using correct_const_t = std::conditional_t<is_const_v, V, U>;
public:
[[gnu::const]]
constexpr bool is_mutable() const noexcept { return !is_const_v; }
using value_type = std::remove_const_t<value_type_correct>;
using pointer_type = correct_const_t<value_type*, value_type const*>;
using const_pointer_type = value_type const*;
using reference_type = correct_const_t<value_type*, value_type const&>;
using const_reference_type = value_type const&;
using move_reference_type = std::add_rvalue_reference_t<value_type_correct>;
using const_move_reference_type = value_type const&&;
struct UnwrapError : public OptionUnwrapError struct UnwrapError : public OptionUnwrapError
{ {
UnwrapError() noexcept =default; UnwrapError() noexcept =default;
@ -287,7 +312,9 @@ namespace exopt::types { namespace optional [[gnu::visibility("internal")]] {
} }
constexpr Option(std::optional<T>&& opt) noexcept requires(!std::is_void_v<T>) constexpr Option(std::optional<T>&& opt) noexcept requires(!std::is_void_v<T>)
: inner_( bool(opt) ? std::move(*opt) : nullptr ) {} : Option(nullptr) {
if(opt) inner_ = decltype(inner_)(std::move(*opt));
}
constexpr explicit Option(std::nullptr_t) noexcept : inner_(nullptr) {} constexpr explicit Option(std::nullptr_t) noexcept : inner_(nullptr) {}
constexpr Option(none_t) noexcept : Option(nullptr) {} constexpr Option(none_t) noexcept : Option(nullptr) {}
@ -300,9 +327,28 @@ namespace exopt::types { namespace optional [[gnu::visibility("internal")]] {
: inner_(std::move(move.inner_)) : inner_(std::move(move.inner_))
{} {}
constexpr explicit(std::convertible_to<std::add_rvalue_reference_t<T>, Option<T>>) Option(_details::rv_ref_t<T> value) noexcept(std::is_nothrow_invocable_v<convert_to_held_rv, std::add_rvalue_reference_t<T>>) requires(!std::is_void_v<T>) constexpr explicit(std::convertible_to<std::add_rvalue_reference_t<T>, Option<T>>)
Option(_details::rv_ref_t<T> value)
noexcept(std::is_nothrow_invocable_v<convert_to_held_rv, std::add_rvalue_reference_t<T>>)
requires(!std::is_void_v<T>)
: inner_(convert_to_held_rv(std::forward<decltype(value)>(value))) : inner_(convert_to_held_rv(std::forward<decltype(value)>(value)))
{} {}
constexpr explicit Option(pointer_type const&& ptr) noexcept(std::is_nothrow_move_constructible_v<decltype(*ptr)>) requires(!std::is_void_v<T>)
Option(nullptr) {
if(ptr) inner_ = decltype(inner_)(convert_to_held_rv(std::move(*ptr)));
}
constexpr explicit Option(const_pointer_type ptr) noexcept(std::is_nothrow_copy_constructible_v<decltype(*ptr)>) requires(!std::is_void_v<T>)
: Option(nullptr) {
if(ptr) inner_ = decltype(inner_)(convert_to_held_rv(auto(*ptr)));
}
template<typename P> requires(!std::is_void_v<T> && (std::is_same_v<std::remove_cvref_t<P>, pointer_type>
or std::is_same_v<std::remove_cvref_t<P>, const_pointer_type>))
constexpr Option<T> from_pointer(P ptr) noexcept
{ if constexpr(std::is_const_v<decltype(*ptr)> or !std::is_rvalue_reference_v<P>)
return Option<T> { static_cast<const_pointer_type>(ptr) };
else return Option<T> { std::move(static_cast<pointer_type>(ptr)) }; }
//: inner_(ptr ? convert_to_held_rv(*ptr) : nullptr) {}
//TODO: copy/move assign where appropriate //TODO: copy/move assign where appropriate
constexpr Option& operator=(const Option& copy) noexcept(std::is_void_v<T> || std::is_nothrow_copy_constructible_v<decltype(copy.inner_)::held_type>) constexpr Option& operator=(const Option& copy) noexcept(std::is_void_v<T> || std::is_nothrow_copy_constructible_v<decltype(copy.inner_)::held_type>)

@ -12,6 +12,7 @@
#define _EO_CONSTANT_VALUE(X) ([]() { \ #define _EO_CONSTANT_VALUE(X) ([]() { \
struct { \ struct { \
typedef decltype(X) comptime_constant_t; \ typedef decltype(X) comptime_constant_t; \
constexpr static inline auto VALUE = (X); \
consteval operator comptime_constant_t() const noexcept { return (X); } \ consteval operator comptime_constant_t() const noexcept { return (X); } \
} inner; \ } inner; \
return inner; \ return inner; \
@ -28,7 +29,7 @@ namespace exopt { namespace util [[gnu::visibility("internal")]] {
constexpr auto comptime_value(auto value) noexcept { constexpr auto comptime_value(auto value) noexcept {
using U = decltype(value); using U = decltype(value);
struct inner { struct inner final {
typedef U comptime_constant_t; typedef U comptime_constant_t;
consteval operator comptime_constant_t() const noexcept { return std::move(value); } consteval operator comptime_constant_t() const noexcept { return std::move(value); }
U value; U value;
@ -39,6 +40,30 @@ namespace exopt { namespace util [[gnu::visibility("internal")]] {
return inner{ std::move(value) }; return inner{ std::move(value) };
} }
template<typename T, auto value> requires(comptime<decltype(value), T>)
consteval T comptime_eval() noexcept {
if constexpr(requires {
{ decltype(value)::VALUE } -> std::convertible_to<decltype(value)::comptime_constant_t>;
}) {
return decltype(value)::VALUE;
} else return value;
}
template<typename T, auto value> requires(comptime<decltype(value), T>)
constexpr inline T comptime_eval_v = comptime_eval<T, value>();
template<typename T>
consteval T comptime_eval(comptime<T> auto value) noexcept {
return comptime_eval<T, value>();
}
static_assert(comptime_eval(comptime_value(5.0)) == 5.0, "Bad comptime_eval() consteval");
static_assert(comptime_eval(_EO_CONSTANT_VALUE(3.f)) == 3.f, "Bad comptime_eval() macro");
static_assert(std::is_same_v<decltype(comptime_eval(comptime_value(4.f))), float>, "Bad comptime_eval() return type consteval");
static_assert(std::is_same_v<decltype(comptime_eval(_EO_CONSTANT_VALUE(4.0))), double>, "Bad comptime_eval() return type macro");
static_assert(comptime_value(std::string_view{"hello"}.size()) == 5, "Bad comptime_value()"); static_assert(comptime_value(std::string_view{"hello"}.size()) == 5, "Bad comptime_value()");
static_assert(_EO_CONSTANT_VALUE(std::string_view{"hello world"}) == std::string_view{"hello world"}, "Bad CONSTANT_VALUE()"); static_assert(_EO_CONSTANT_VALUE(std::string_view{"hello world"}) == std::string_view{"hello world"}, "Bad CONSTANT_VALUE()");

@ -0,0 +1,30 @@
#include <stdexcept>
#include <error.hh>
using namespace exopt::types;
namespace exopt {
Box<TracedError> Error::into_traced(std::stacktrace&& trace) &&noexcept {
struct DynamicTracedError final : TracedError {
_EO_CTORS_BASIC_DEFAULT(DynamicTracedError, _EO_NOTHING, noexcept);
virtual ~DynamicTracedError() noexcept = default;
DynamicTracedError(Error&& self, std::stacktrace&& trace) noexcept
: TracedError(std::move(trace), std::move(self.m_location))
, m_error(std::move(self)) {}
std::string_view message() const noexcept override { return m_error->message(); }
Option<Error const&> inner() const noexcept override { return Option<Error const&>(*m_error); }//{ return Option<Error const&>::from_pointer(std::as_const(m_error.get())); }
MaybeOwned<Report> into_report() && noexcept override { return std::move(*m_error).into_report(); }
MaybeOwned<Report> into_report() & noexcept override { return m_error->into_report(); }
private:
Box<Error> m_error;
};
#define ERR DynamicTracedError{std::move(*this), std::move(trace)}
return Box<TracedError>::template new_dynamic<DynamicTracedError, false>(ERR);
#undef ERR
}
}
Loading…
Cancel
Save