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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
/**
* Monitor.h
*
* A monitor object monitors if the connection is still valid. When the
* connection is parsing incoming data, it calls the user handler for each
* incoming frame. However, it is unknown what this handler is going to do,
* it could for example decide to destruct the connection object. In that
* case the connection object should stop further handling the data. This
* monitor class is used to check if the connection has been destructed.
*
* @copyright 2014 - 2018 Copernica BV
*/
/**
* Include guard
*/
#pragma once
/**
* Dependencies
*/
#include "watchable.h"
/**
* Set up namespace
*/
namespace AMQP {
/**
* Class definition
*/
class Monitor
{
private:
/**
* The object being watched
* @var Watchable
*/
Watchable *_watchable;
/**
* Invalidate the object
*/
void invalidate()
{
_watchable = nullptr;
}
public:
/**
* Constructor
* @param watchable
*/
Monitor(Watchable *watchable) : _watchable(watchable)
{
// register with the watchable
_watchable->add(this);
}
/**
* Copy constructor
* @param monitor
*/
Monitor(const Monitor &monitor) : _watchable(monitor._watchable)
{
// register with the watchable
if (_watchable) _watchable->add(this);
}
/**
* Assignment operator
* @param monitor
*/
Monitor& operator= (const Monitor &monitor)
{
// remove from watchable
if (_watchable) _watchable->remove(this);
// replace watchable
_watchable = monitor._watchable;
// register with the watchable
if (_watchable) _watchable->add(this);
return *this;
}
/**
* Destructor
*/
virtual ~Monitor()
{
// remove from watchable
if (_watchable) _watchable->remove(this);
}
/**
* Cast to boolean: is object in valid state?
* @return bool
*/
operator bool () const
{
return _watchable != nullptr;
}
/**
* Negate operator: is the object in an invalid state?
* @return bool
*/
bool operator! () const
{
return _watchable == nullptr;
}
/**
* Check if the object is valid
* @return bool
*/
bool valid() const
{
return _watchable != nullptr;
}
/**
* The watchable can access private data
*/
friend class Watchable;
};
/**
* End of namespace
*/
}
|