Commit df975797 authored by Dominik Charousset's avatar Dominik Charousset

Add convenience functions for discarding resources

parent 7ea378a5
...@@ -329,7 +329,7 @@ struct resource_ctrl : ref_counted { ...@@ -329,7 +329,7 @@ struct resource_ctrl : ref_counted {
~resource_ctrl() { ~resource_ctrl() {
if (buf) { if (buf) {
if constexpr (IsProducer) { if constexpr (IsProducer) {
auto err = make_error(sec::invalid_upstream, auto err = make_error(sec::disposed,
"producer_resource destroyed without opening it"); "producer_resource destroyed without opening it");
buf->abort(err); buf->abort(err);
} else { } else {
...@@ -397,11 +397,19 @@ public: ...@@ -397,11 +397,19 @@ public:
} }
} }
/// Convenience function for calling
/// `ctx->make_observable().from_resource(*this)`.
template <class Coordinator> template <class Coordinator>
auto observe_on(Coordinator* ctx) { auto observe_on(Coordinator* ctx) {
return ctx->make_observable().from_resource(*this); return ctx->make_observable().from_resource(*this);
} }
/// Calls `try_open` and on success immediately calls `close` on the buffer.
void close() {
if (auto buf = try_open())
buf->close();
}
explicit operator bool() const noexcept { explicit operator bool() const noexcept {
return ctrl_ != nullptr; return ctrl_ != nullptr;
} }
...@@ -454,6 +462,12 @@ public: ...@@ -454,6 +462,12 @@ public:
} }
} }
/// Calls `try_open` and on success immediately calls `cancel` on the buffer.
void cancel() {
if (auto buf = try_open())
buf->cancel();
}
explicit operator bool() const noexcept { explicit operator bool() const noexcept {
return ctrl_ != nullptr; return ctrl_ != nullptr;
} }
......
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