2014-04-15 16:43:33 +08:00
|
|
|
/**
|
|
|
|
|
* DeferredCancel.h
|
|
|
|
|
*
|
|
|
|
|
* Deferred callback for instructions that cancel a running consumer. This
|
|
|
|
|
* deferred object allows one to register a callback that also gets the
|
|
|
|
|
* consumer tag as one of its parameters.
|
|
|
|
|
*
|
|
|
|
|
* @copyright 2014 Copernica BV
|
|
|
|
|
*/
|
|
|
|
|
|
2016-04-06 22:49:39 +08:00
|
|
|
/**
|
|
|
|
|
* Include guard
|
|
|
|
|
*/
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2014-04-15 16:43:33 +08:00
|
|
|
/**
|
|
|
|
|
* Set up namespace
|
|
|
|
|
*/
|
|
|
|
|
namespace AMQP {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* We extend from the default deferred and add extra functionality
|
|
|
|
|
*/
|
|
|
|
|
class DeferredCancel : public Deferred
|
|
|
|
|
{
|
|
|
|
|
private:
|
2014-04-15 18:36:11 +08:00
|
|
|
/**
|
|
|
|
|
* Pointer to the channel
|
|
|
|
|
* @var ChannelImpl
|
|
|
|
|
*/
|
|
|
|
|
ChannelImpl *_channel;
|
|
|
|
|
|
2014-04-15 16:43:33 +08:00
|
|
|
/**
|
|
|
|
|
* Callback to execute when the instruction is completed
|
|
|
|
|
* @var CancelCallback
|
|
|
|
|
*/
|
|
|
|
|
CancelCallback _cancelCallback;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Report success for frames that report cancel operations
|
|
|
|
|
* @param name Consumer tag that is cancelled
|
|
|
|
|
* @return Deferred
|
|
|
|
|
*/
|
2016-06-23 20:42:50 +08:00
|
|
|
virtual const std::shared_ptr<Deferred> &reportSuccess(const std::string &name) override;
|
2014-04-15 16:43:33 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The channel implementation may call our
|
|
|
|
|
* private members and construct us
|
|
|
|
|
*/
|
|
|
|
|
friend class ChannelImpl;
|
|
|
|
|
friend class ConsumedMessage;
|
|
|
|
|
|
2014-09-02 16:32:55 +08:00
|
|
|
public:
|
2014-04-15 16:43:33 +08:00
|
|
|
/**
|
|
|
|
|
* Protected constructor that can only be called
|
|
|
|
|
* from within the channel implementation
|
|
|
|
|
*
|
2014-09-02 16:32:55 +08:00
|
|
|
* Note: this constructor _should_ be protected, but because make_shared
|
|
|
|
|
* will then not work, we have decided to make it public after all,
|
|
|
|
|
* because the work-around would result in not-so-easy-to-read code.
|
|
|
|
|
*
|
2014-04-15 18:36:11 +08:00
|
|
|
* @param channel Pointer to the channel
|
|
|
|
|
* @param failed Are we already failed?
|
2014-04-15 16:43:33 +08:00
|
|
|
*/
|
2014-04-15 18:36:11 +08:00
|
|
|
DeferredCancel(ChannelImpl *channel, bool failed = false) :
|
|
|
|
|
Deferred(failed), _channel(channel) {}
|
2014-04-15 16:43:33 +08:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Register a function to be called when the cancel operation succeeded
|
|
|
|
|
*
|
|
|
|
|
* Only one callback can be registered. Successive calls
|
|
|
|
|
* to this function will clear callbacks registered before.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback to execute
|
|
|
|
|
*/
|
2022-03-19 06:53:44 +08:00
|
|
|
inline DeferredCancel &onSuccess(const CancelCallback& callback) { return onSuccess(CancelCallback(callback)); }
|
|
|
|
|
DeferredCancel &onSuccess(CancelCallback&& callback)
|
2014-04-15 16:43:33 +08:00
|
|
|
{
|
|
|
|
|
// store callback
|
2022-03-15 21:10:45 +08:00
|
|
|
_cancelCallback = std::move(callback);
|
2014-04-15 16:43:33 +08:00
|
|
|
|
|
|
|
|
// allow chaining
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-04-16 17:43:27 +08:00
|
|
|
* Register the function that is called when the cancel operation succeeded
|
|
|
|
|
* @param callback
|
2014-04-15 16:43:33 +08:00
|
|
|
*/
|
2022-03-19 06:53:44 +08:00
|
|
|
inline DeferredCancel &onSuccess(const SuccessCallback& callback) { return onSuccess(SuccessCallback(callback)); }
|
|
|
|
|
DeferredCancel &onSuccess(SuccessCallback&& callback)
|
2014-04-16 17:43:27 +08:00
|
|
|
{
|
|
|
|
|
// call base
|
2022-03-15 21:10:45 +08:00
|
|
|
Deferred::onSuccess(std::move(callback));
|
2014-04-16 17:43:27 +08:00
|
|
|
|
|
|
|
|
// allow chaining
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2014-04-15 16:43:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* End namespace
|
|
|
|
|
*/
|
|
|
|
|
}
|