2023-02-08 11:04:11 +00:00
|
|
|
//
|
|
|
|
// RefCountedObject.h
|
|
|
|
//
|
|
|
|
// Library: Foundation
|
|
|
|
// Package: Core
|
|
|
|
// Module: RefCountedObject
|
|
|
|
//
|
|
|
|
// Definition of the RefCountedObject class.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2004-2009, Applied Informatics Software Engineering GmbH.
|
|
|
|
// and Contributors.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef Foundation_RefCountedObject_INCLUDED
|
|
|
|
#define Foundation_RefCountedObject_INCLUDED
|
|
|
|
|
|
|
|
|
|
|
|
#include "Poco/AtomicCounter.h"
|
2023-02-13 09:22:33 +00:00
|
|
|
#include "Poco/Foundation.h"
|
2023-02-08 11:04:11 +00:00
|
|
|
|
|
|
|
|
2023-02-13 09:22:33 +00:00
|
|
|
namespace Poco
|
|
|
|
{
|
2023-02-08 11:04:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Foundation_API RefCountedObject
|
2023-02-13 09:22:33 +00:00
|
|
|
/// A base class for objects that employ
|
|
|
|
/// reference counting based garbage collection.
|
|
|
|
///
|
|
|
|
/// Reference-counted objects inhibit construction
|
|
|
|
/// by copying and assignment.
|
2023-02-08 11:04:11 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-02-13 09:22:33 +00:00
|
|
|
RefCountedObject();
|
|
|
|
/// Creates the RefCountedObject.
|
|
|
|
/// The initial reference count is one.
|
|
|
|
|
2024-01-23 17:00:11 +00:00
|
|
|
size_t duplicate() const;
|
|
|
|
/// Increments the object's reference count, returns reference count before call.
|
2023-02-13 09:22:33 +00:00
|
|
|
|
2024-01-23 17:00:11 +00:00
|
|
|
size_t release() const throw();
|
2023-02-13 09:22:33 +00:00
|
|
|
/// Decrements the object's reference count
|
|
|
|
/// and deletes the object if the count
|
2024-01-23 17:00:11 +00:00
|
|
|
/// reaches zero, returns reference count before call.
|
2023-02-13 09:22:33 +00:00
|
|
|
|
2024-01-23 17:00:11 +00:00
|
|
|
size_t referenceCount() const;
|
2023-02-13 09:22:33 +00:00
|
|
|
/// Returns the reference count.
|
2023-02-08 11:04:11 +00:00
|
|
|
|
|
|
|
protected:
|
2023-02-13 09:22:33 +00:00
|
|
|
virtual ~RefCountedObject();
|
|
|
|
/// Destroys the RefCountedObject.
|
2023-02-08 11:04:11 +00:00
|
|
|
|
|
|
|
private:
|
2023-02-13 09:22:33 +00:00
|
|
|
RefCountedObject(const RefCountedObject &);
|
|
|
|
RefCountedObject & operator=(const RefCountedObject &);
|
2024-02-02 11:10:05 +00:00
|
|
|
|
|
|
|
mutable std::atomic<size_t> _counter;
|
2023-02-08 11:04:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// inlines
|
|
|
|
//
|
2024-01-23 17:00:11 +00:00
|
|
|
inline size_t RefCountedObject::referenceCount() const
|
2023-02-08 11:04:11 +00:00
|
|
|
{
|
2024-01-23 17:00:11 +00:00
|
|
|
return _counter.load(std::memory_order_acquire);
|
2023-02-08 11:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-23 17:00:11 +00:00
|
|
|
inline size_t RefCountedObject::duplicate() const
|
2023-02-08 11:04:11 +00:00
|
|
|
{
|
2024-01-23 17:00:11 +00:00
|
|
|
return _counter.fetch_add(1, std::memory_order_acq_rel);
|
2023-02-08 11:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-23 17:00:11 +00:00
|
|
|
inline size_t RefCountedObject::release() const throw()
|
2023-02-08 11:04:11 +00:00
|
|
|
{
|
2024-01-23 17:00:11 +00:00
|
|
|
size_t reference_count_before = _counter.fetch_sub(1, std::memory_order_acq_rel);
|
|
|
|
|
2023-02-13 09:22:33 +00:00
|
|
|
try
|
|
|
|
{
|
2024-01-23 17:00:11 +00:00
|
|
|
if (reference_count_before == 1)
|
2023-02-13 09:22:33 +00:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
poco_unexpected();
|
|
|
|
}
|
2024-01-23 17:00:11 +00:00
|
|
|
|
|
|
|
return reference_count_before;
|
2023-02-08 11:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Poco
|
|
|
|
|
|
|
|
|
|
|
|
#endif // Foundation_RefCountedObject_INCLUDED
|