2016-11-15 01:51:22 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <memory>
|
|
|
|
|
2021-11-20 15:22:22 +00:00
|
|
|
#include "QObjectPtr.h"
|
|
|
|
|
2016-11-15 01:51:22 +00:00
|
|
|
class Usable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for things that can be used by multiple other things and we want to track the use count.
|
|
|
|
*
|
|
|
|
* @see UseLock
|
|
|
|
*/
|
|
|
|
class Usable
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
friend class UseLock;
|
2016-11-15 01:51:22 +00:00
|
|
|
public:
|
2021-11-20 15:22:22 +00:00
|
|
|
std::size_t useCount() const
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
|
|
|
return m_useCount;
|
|
|
|
}
|
2021-11-20 15:22:22 +00:00
|
|
|
bool isInUse() const
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
|
|
|
return m_useCount > 0;
|
|
|
|
}
|
2016-11-15 01:51:22 +00:00
|
|
|
protected:
|
2018-07-15 13:51:05 +01:00
|
|
|
virtual void decrementUses()
|
|
|
|
{
|
|
|
|
m_useCount--;
|
|
|
|
}
|
|
|
|
virtual void incrementUses()
|
|
|
|
{
|
|
|
|
m_useCount++;
|
|
|
|
}
|
2016-11-15 01:51:22 +00:00
|
|
|
private:
|
2018-07-15 13:51:05 +01:00
|
|
|
std::size_t m_useCount = 0;
|
2016-11-15 01:51:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lock class to use for keeping track of uses of other things derived from Usable
|
|
|
|
*
|
|
|
|
* @see Usable
|
|
|
|
*/
|
|
|
|
class UseLock
|
|
|
|
{
|
|
|
|
public:
|
2021-11-20 15:22:22 +00:00
|
|
|
UseLock(shared_qobject_ptr<Usable> usable)
|
2018-07-15 13:51:05 +01:00
|
|
|
: m_usable(usable)
|
|
|
|
{
|
|
|
|
// this doesn't use shared pointer use count, because that wouldn't be correct. this count is separate.
|
|
|
|
m_usable->incrementUses();
|
|
|
|
}
|
|
|
|
~UseLock()
|
|
|
|
{
|
|
|
|
m_usable->decrementUses();
|
|
|
|
}
|
2016-11-15 01:51:22 +00:00
|
|
|
private:
|
2021-11-20 15:22:22 +00:00
|
|
|
shared_qobject_ptr<Usable> m_usable;
|
2016-11-15 01:51:22 +00:00
|
|
|
};
|