2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* Watchable.h
|
|
|
|
|
*
|
|
|
|
|
* Every class that overrides from the Watchable class can be monitored for
|
|
|
|
|
* destruction by a Monitor object
|
|
|
|
|
*
|
|
|
|
|
* @copyright 2014 Copernica BV
|
|
|
|
|
*/
|
|
|
|
|
|
2015-11-01 17:48:13 +08:00
|
|
|
/**
|
|
|
|
|
* Include guard
|
|
|
|
|
*/
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2016-06-23 20:42:50 +08:00
|
|
|
/**
|
|
|
|
|
* Dependencies
|
|
|
|
|
*/
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* Set up namespace
|
|
|
|
|
*/
|
|
|
|
|
namespace AMQP {
|
|
|
|
|
|
2016-06-23 20:42:50 +08:00
|
|
|
/**
|
|
|
|
|
* Forward declarations
|
|
|
|
|
*/
|
|
|
|
|
class Monitor;
|
|
|
|
|
|
2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* Class definition
|
|
|
|
|
*/
|
|
|
|
|
class Watchable
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
/**
|
|
|
|
|
* The monitors
|
2015-11-01 16:43:17 +08:00
|
|
|
* @var std::vector
|
2014-01-04 19:45:04 +08:00
|
|
|
*/
|
2015-11-01 16:43:17 +08:00
|
|
|
std::vector<Monitor*> _monitors;
|
2014-01-04 19:45:04 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add a monitor
|
|
|
|
|
* @param monitor
|
|
|
|
|
*/
|
|
|
|
|
void add(Monitor *monitor)
|
|
|
|
|
{
|
2015-11-01 16:43:17 +08:00
|
|
|
// add to the vector
|
|
|
|
|
_monitors.push_back(monitor);
|
2014-01-04 19:45:04 +08:00
|
|
|
}
|
2016-06-23 20:42:50 +08:00
|
|
|
|
2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* Remove a monitor
|
|
|
|
|
* @param monitor
|
|
|
|
|
*/
|
|
|
|
|
void remove(Monitor *monitor)
|
|
|
|
|
{
|
2015-11-01 16:43:17 +08:00
|
|
|
// put the monitor at the end of the vector
|
|
|
|
|
auto iter = std::remove(_monitors.begin(), _monitors.end(), monitor);
|
2016-06-23 20:42:50 +08:00
|
|
|
|
2015-11-01 16:43:17 +08:00
|
|
|
// make the vector smaller
|
|
|
|
|
_monitors.erase(iter, _monitors.end());
|
2014-01-04 19:45:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Destructor
|
|
|
|
|
*/
|
|
|
|
|
virtual ~Watchable();
|
2016-06-23 20:42:50 +08:00
|
|
|
|
2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* Only a monitor has full access
|
|
|
|
|
*/
|
|
|
|
|
friend class Monitor;
|
2016-06-23 20:42:50 +08:00
|
|
|
};
|
2014-01-04 19:45:04 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* End of namespace
|
|
|
|
|
*/
|
|
|
|
|
}
|