From ebcdd71848f29552564c63d3243b84e1d7e870b9 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Thu, 28 Aug 2014 10:02:01 +0200 Subject: [PATCH] Added onSize() method to DeferredGet --- include/callbacks.h | 1 + include/deferredget.h | 19 +++++++++++++++++++ src/deferredget.cpp | 9 +++++++++ 3 files changed, 29 insertions(+) diff --git a/include/callbacks.h b/include/callbacks.h index c611f89..608f5f0 100644 --- a/include/callbacks.h +++ b/include/callbacks.h @@ -24,6 +24,7 @@ using EmptyCallback = std::function; using MessageCallback = std::function; using QueueCallback = std::function; using DeleteCallback = std::function; +using SizeCallback = std::function; using ConsumeCallback = std::function; using CancelCallback = std::function; diff --git a/include/deferredget.h b/include/deferredget.h index 6642c35..db74621 100644 --- a/include/deferredget.h +++ b/include/deferredget.h @@ -33,6 +33,12 @@ private: * @var EmptyCallback */ EmptyCallback _emptyCallback; + + /** + * Callback with the number of messages still in the queue + * @var SizeCallback + */ + SizeCallback _sizeCallback; /** * Report success when a message is indeed expected @@ -121,6 +127,19 @@ public: // allow chaining return *this; } + + /** + * Register a function to be called when size information is known + * @param callback the callback to execute + */ + DeferredGet &onSize(const SizeCallback &callback) + { + // store callback + _sizeCallback = callback; + + // allow chaining + return *this; + } }; /** diff --git a/src/deferredget.cpp b/src/deferredget.cpp index 0cf24a9..8daeaf6 100644 --- a/src/deferredget.cpp +++ b/src/deferredget.cpp @@ -28,6 +28,12 @@ Deferred *DeferredGet::reportSuccess(uint32_t messageCount) const auto messageCallback = _messageCallback; auto finalizeCallback = _finalizeCallback; auto *channel = _channel; + + // install a monitor because the channel could be destructed + Monitor monitor(channel); + + // report the size (technically, the channel object could be destructed now, but we ignore that case) + if (_sizeCallback) _sizeCallback(messageCount); // we now know the name, so we can install the message callback on the channel _channel->install("", [channel, messageCallback, finalizeCallback](const Message &message, uint64_t deliveryTag, bool redelivered) { @@ -54,6 +60,9 @@ Deferred *DeferredGet::reportSuccess(uint32_t messageCount) const */ Deferred *DeferredGet::reportSuccess() const { + // report the size + if (_sizeCallback) _sizeCallback(0); + // check if a callback was set if (_emptyCallback) _emptyCallback();