removed channel parameter from the callbacks, because this can easily be captured

This commit is contained in:
Emiel Bruijntjes 2014-04-15 08:52:49 +02:00
parent d47d6bda5d
commit 7b20f46519
4 changed files with 25 additions and 30 deletions

View File

@ -27,17 +27,17 @@ private:
/** /**
* Callback to execute on success * Callback to execute on success
*/ */
std::function<void(Channel *channel, Arguments ...parameters)> _successCallback; std::function<void(Arguments ...parameters)> _successCallback;
/** /**
* Callback to execute on failure * Callback to execute on failure
*/ */
std::function<void(Channel *channel, const std::string& error)> _errorCallback; std::function<void(const std::string& error)> _errorCallback;
/** /**
* Callback to execute either way * Callback to execute either way
*/ */
std::function<void(Channel *channel, const std::string& error)> _finalizeCallback; std::function<void(const std::string& error)> _finalizeCallback;
/** /**
* Indicate success * Indicate success
@ -47,8 +47,8 @@ private:
void success(Arguments ...parameters) const void success(Arguments ...parameters) const
{ {
// execute callbacks if registered // execute callbacks if registered
if (_successCallback) _successCallback(_channel, parameters...); if (_successCallback) _successCallback(parameters...);
if (_finalizeCallback) _finalizeCallback(_channel, ""); if (_finalizeCallback) _finalizeCallback("");
} }
/** /**
@ -59,8 +59,8 @@ private:
void error(const std::string& error) const void error(const std::string& error) const
{ {
// execute callbacks if registered // execute callbacks if registered
if (_errorCallback) _errorCallback(_channel, error); if (_errorCallback) _errorCallback(error);
if (_finalizeCallback) _finalizeCallback(_channel, error); if (_finalizeCallback) _finalizeCallback(error);
} }
/** /**
@ -69,14 +69,11 @@ private:
*/ */
friend class ChannelImpl; friend class ChannelImpl;
friend class Callbacks; friend class Callbacks;
protected: protected:
/**
* The channel we operate under
*/
Channel *_channel;
/** /**
* Do we already know we failed? * Do we already know we failed?
* @var bool
*/ */
bool _failed; bool _failed;
@ -84,13 +81,10 @@ protected:
* Protected constructor that can only be called * Protected constructor that can only be called
* from within the channel implementation * from within the channel implementation
* *
* @param channel the channel we operate under * @param failed are we already failed?
* @param boolea are we already failed?
*/ */
Deferred(Channel *channel, bool failed = false) : Deferred(bool failed = false) : _failed(failed) {}
_channel(channel),
_failed(failed)
{}
public: public:
/** /**
* Deleted copy constructor * Deleted copy constructor
@ -125,7 +119,7 @@ public:
* *
* @param callback the callback to execute * @param callback the callback to execute
*/ */
Deferred& onSuccess(const std::function<void(Channel *channel, Arguments ...parameters)>& callback) Deferred& onSuccess(const std::function<void(Arguments ...parameters)>& callback)
{ {
// store callback // store callback
_successCallback = callback; _successCallback = callback;
@ -142,7 +136,7 @@ public:
* *
* @param callback the callback to execute * @param callback the callback to execute
*/ */
Deferred& onError(const std::function<void(Channel *channel, const std::string& error)>& callback) Deferred& onError(const std::function<void(const std::string& error)>& callback)
{ {
// store callback // store callback
_errorCallback = callback; _errorCallback = callback;
@ -164,7 +158,7 @@ public:
* *
* @param callback the callback to execute * @param callback the callback to execute
*/ */
Deferred& onFinalize(const std::function<void(Channel *channel, const std::string& error)>& callback) Deferred& onFinalize(const std::function<void(const std::string& error)>& callback)
{ {
// store callback // store callback
_finalizeCallback = callback; _finalizeCallback = callback;

View File

@ -20,7 +20,7 @@ private:
/** /**
* Callback to execute when a message arrives * Callback to execute when a message arrives
*/ */
std::function<void(Channel *channel, const Message &message, uint64_t deliveryTag, const std::string &consumerTag, bool redelivered)> _messageCallback; std::function<void(const Message &message, uint64_t deliveryTag, const std::string &consumerTag, bool redelivered)> _messageCallback;
/** /**
* Process a message * Process a message
@ -33,7 +33,7 @@ private:
void message(const Message &message, uint64_t deliveryTag, const std::string &consumerTag, bool redelivered) const void message(const Message &message, uint64_t deliveryTag, const std::string &consumerTag, bool redelivered) const
{ {
// do we have a valid callback // do we have a valid callback
if (_messageCallback) _messageCallback(_channel, message, deliveryTag, consumerTag, redelivered); if (_messageCallback) _messageCallback(message, deliveryTag, consumerTag, redelivered);
} }
/** /**
@ -42,6 +42,7 @@ private:
*/ */
friend class ChannelImpl; friend class ChannelImpl;
friend class ConsumedMessage; friend class ConsumedMessage;
protected: protected:
/** /**
* Protected constructor that can only be called * Protected constructor that can only be called
@ -50,9 +51,8 @@ protected:
* @param channel the channel we operate under * @param channel the channel we operate under
* @param boolea are we already failed? * @param boolea are we already failed?
*/ */
DeferredConsumer(Channel *channel, bool failed = false) : DeferredConsumer(bool failed = false) : Deferred(failed) {}
Deferred(channel, failed)
{}
public: public:
/** /**
* Register a function to be called when a message arrives * Register a function to be called when a message arrives
@ -62,7 +62,7 @@ public:
* *
* @param callback the callback to execute * @param callback the callback to execute
*/ */
DeferredConsumer& onReceived(const std::function<void(Channel *channel, const Message &message, uint64_t deliveryTag, const std::string &consumerTag, bool redelivered)>& callback) DeferredConsumer& onReceived(const std::function<void(const Message &message, uint64_t deliveryTag, const std::string &consumerTag, bool redelivered)>& callback)
{ {
// store callback // store callback
_messageCallback = callback; _messageCallback = callback;

View File

@ -1,6 +1,6 @@
CPP = g++ CPP = g++
RM = rm -f RM = rm -f
CPPFLAGS = -Wall -c -I. -O2 -flto -std=c++11 -g CPPFLAGS = -Wall -c -I. -g -flto -std=c++11 -g
LD = g++ LD = g++
LD_FLAGS = -Wall -shared -O2 LD_FLAGS = -Wall -shared -O2
SHARED_LIB = libamqpcpp.so SHARED_LIB = libamqpcpp.so

View File

@ -439,7 +439,7 @@ Deferred<>& ChannelImpl::setQos(uint16_t prefetchCount)
DeferredConsumer& ChannelImpl::consume(const std::string &queue, const std::string &tag, int flags, const Table &arguments) DeferredConsumer& ChannelImpl::consume(const std::string &queue, const std::string &tag, int flags, const Table &arguments)
{ {
// create the deferred consumer // create the deferred consumer
_consumer = std::unique_ptr<DeferredConsumer>(new DeferredConsumer(_parent, false)); _consumer = std::unique_ptr<DeferredConsumer>(new DeferredConsumer(false));
// can we send the basic consume frame? // can we send the basic consume frame?
if (!send(BasicConsumeFrame(_id, queue, tag, flags & nolocal, flags & noack, flags & exclusive, flags & nowait, arguments))) if (!send(BasicConsumeFrame(_id, queue, tag, flags & nolocal, flags & noack, flags & exclusive, flags & nowait, arguments)))
@ -542,7 +542,7 @@ Deferred<Arguments...>& ChannelImpl::send(const Frame &frame, const char *messag
// create a new deferred handler and get a pointer to it // create a new deferred handler and get a pointer to it
// note: cannot use auto here or the lambda below chokes // note: cannot use auto here or the lambda below chokes
// when compiling under gcc 4.8 // when compiling under gcc 4.8
Deferred<Arguments...> *handler = &_callbacks.push_back(Deferred<Arguments...>(_parent)); Deferred<Arguments...> *handler = &_callbacks.push_back(Deferred<Arguments...>());
// send the frame over the channel // send the frame over the channel
if (!send(frame)) if (!send(frame))
@ -554,6 +554,7 @@ Deferred<Arguments...>& ChannelImpl::send(const Frame &frame, const char *messag
// after a timeout, so it gets called only // after a timeout, so it gets called only
// after a possible handler was installed. // after a possible handler was installed.
_connection->_handler->setTimeout(_connection->_parent, 0, [handler, message]() { _connection->_handler->setTimeout(_connection->_parent, 0, [handler, message]() {
// emit an error on the handler // emit an error on the handler
handler->error(message); handler->error(message);
}); });