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.
|
|
|
|
|
|
|
|
void duplicate() const;
|
|
|
|
/// Increments the object's reference count.
|
|
|
|
|
|
|
|
void release() const throw();
|
|
|
|
/// Decrements the object's reference count
|
|
|
|
/// and deletes the object if the count
|
|
|
|
/// reaches zero.
|
|
|
|
|
|
|
|
int referenceCount() const;
|
|
|
|
/// 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 &);
|
2023-02-08 11:04:11 +00:00
|
|
|
|
2023-02-13 09:22:33 +00:00
|
|
|
mutable AtomicCounter _counter;
|
2023-02-08 11:04:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// inlines
|
|
|
|
//
|
|
|
|
inline int RefCountedObject::referenceCount() const
|
|
|
|
{
|
2023-02-13 09:22:33 +00:00
|
|
|
return _counter.value();
|
2023-02-08 11:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void RefCountedObject::duplicate() const
|
|
|
|
{
|
2023-02-13 09:22:33 +00:00
|
|
|
++_counter;
|
2023-02-08 11:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void RefCountedObject::release() const throw()
|
|
|
|
{
|
2023-02-13 09:22:33 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (--_counter == 0)
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
poco_unexpected();
|
|
|
|
}
|
2023-02-08 11:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Poco
|
|
|
|
|
|
|
|
|
|
|
|
#endif // Foundation_RefCountedObject_INCLUDED
|