File: deferreddelete.h

package info (click to toggle)
amqp-cpp 4.3.27-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,384 kB
  • sloc: cpp: 10,021; ansic: 191; makefile: 95
file content (107 lines) | stat: -rw-r--r-- 2,860 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
 *  DeferredDelete.h
 *
 *  Deferred callback for instructions that delete or purge queues, and that
 *  want to report the number of deleted messages.
 *
 *  @copyright 2014 Copernica BV
 */

/**
 *  Include guard
 */
#pragma once

/**
 *  Set up namespace
 */
namespace AMQP {

/**
 *  We extend from the default deferred and add extra functionality
 */
class DeferredDelete : public Deferred
{
private:
    /**
     *  Callback to execute when the instruction is completed
     *  @var    DeleteCallback
     */
    DeleteCallback _deleteCallback;

    /**
     *  Report success for queue delete and queue purge messages
     *  @param  messagecount    Number of messages that were deleted
     *  @return Deferred        Next deferred result
     */
    virtual const std::shared_ptr<Deferred> &reportSuccess(uint32_t messagecount) const override
    {
        // skip if no special callback was installed
        if (!_deleteCallback) return Deferred::reportSuccess();
        
        // call the callback
        _deleteCallback(messagecount);
        
        // return next object
        return _next;
    }


    /**
     *  The channel implementation may call our
     *  private members and construct us
     */
    friend class ChannelImpl;
    friend class ConsumedMessage;
    
public:
    /**
     *  Protected constructor that can only be called
     *  from within the channel implementation
     * 
     *  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.
     *
     *  @param  boolean     are we already failed?
     */
    DeferredDelete(bool failed = false) : Deferred(failed) {}

public:
    /**
     *  Register a function to be called when the queue is deleted or purged
     *
     *  Only one callback can be registered. Successive calls
     *  to this function will clear callbacks registered before.
     *
     *  @param  callback    the callback to execute
     */
    inline DeferredDelete &onSuccess(const DeleteCallback& callback) { return onSuccess(DeleteCallback(callback)); }
    DeferredDelete &onSuccess(DeleteCallback&& callback)
    {
        // store callback
        _deleteCallback = std::move(callback);
        
        // allow chaining
        return *this;
    }

    /**
     *  Register the function that is called when the queue is deleted or purged
     *  @param  callback
     */
    inline DeferredDelete &onSuccess(const SuccessCallback& callback) { return onSuccess(SuccessCallback(callback)); }
    DeferredDelete &onSuccess(SuccessCallback&& callback)
    {
        // call base
        Deferred::onSuccess(std::move(callback));
        
        // allow chaining
        return *this;
    }
};

/**
 *  End namespace
 */
}