Blame external/pybind11/tests/object.h

Packit 534379
#if !defined(__OBJECT_H)
Packit 534379
#define __OBJECT_H
Packit 534379
Packit 534379
#include <atomic>
Packit 534379
#include "constructor_stats.h"
Packit 534379
Packit 534379
/// Reference counted object base class
Packit 534379
class Object {
Packit 534379
public:
Packit 534379
    /// Default constructor
Packit 534379
    Object() { print_default_created(this); }
Packit 534379
Packit 534379
    /// Copy constructor
Packit 534379
    Object(const Object &) : m_refCount(0) { print_copy_created(this); }
Packit 534379
Packit 534379
    /// Return the current reference count
Packit 534379
    int getRefCount() const { return m_refCount; };
Packit 534379
Packit 534379
    /// Increase the object's reference count by one
Packit 534379
    void incRef() const { ++m_refCount; }
Packit 534379
Packit 534379
    /** \brief Decrease the reference count of
Packit 534379
     * the object and possibly deallocate it.
Packit 534379
     *
Packit 534379
     * The object will automatically be deallocated once
Packit 534379
     * the reference count reaches zero.
Packit 534379
     */
Packit 534379
    void decRef(bool dealloc = true) const {
Packit 534379
        --m_refCount;
Packit 534379
        if (m_refCount == 0 && dealloc)
Packit 534379
            delete this;
Packit 534379
        else if (m_refCount < 0)
Packit 534379
            throw std::runtime_error("Internal error: reference count < 0!");
Packit 534379
    }
Packit 534379
Packit 534379
    virtual std::string toString() const = 0;
Packit 534379
protected:
Packit 534379
    /** \brief Virtual protected deconstructor.
Packit 534379
     * (Will only be called by \ref ref)
Packit 534379
     */
Packit 534379
    virtual ~Object() { print_destroyed(this); }
Packit 534379
private:
Packit 534379
    mutable std::atomic<int> m_refCount { 0 };
Packit 534379
};
Packit 534379
Packit 534379
// Tag class used to track constructions of ref objects.  When we track constructors, below, we
Packit 534379
// track and print out the actual class (e.g. ref<MyObject>), and *also* add a fake tracker for
Packit 534379
// ref_tag.  This lets us check that the total number of ref<Anything> constructors/destructors is
Packit 534379
// correct without having to check each individual ref<Whatever> type individually.
Packit 534379
class ref_tag {};
Packit 534379
Packit 534379
/**
Packit 534379
 * \brief Reference counting helper
Packit 534379
 *
Packit 534379
 * The \a ref refeference template is a simple wrapper to store a
Packit 534379
 * pointer to an object. It takes care of increasing and decreasing
Packit 534379
 * the reference count of the object. When the last reference goes
Packit 534379
 * out of scope, the associated object will be deallocated.
Packit 534379
 *
Packit 534379
 * \ingroup libcore
Packit 534379
 */
Packit 534379
template <typename T> class ref {
Packit 534379
public:
Packit 534379
    /// Create a nullptr reference
Packit 534379
    ref() : m_ptr(nullptr) { print_default_created(this); track_default_created((ref_tag*) this); }
Packit 534379
Packit 534379
    /// Construct a reference from a pointer
Packit 534379
    ref(T *ptr) : m_ptr(ptr) {
Packit 534379
        if (m_ptr) ((Object *) m_ptr)->incRef();
Packit 534379
Packit 534379
        print_created(this, "from pointer", m_ptr); track_created((ref_tag*) this, "from pointer");
Packit 534379
Packit 534379
    }
Packit 534379
Packit 534379
    /// Copy constructor
Packit 534379
    ref(const ref &r) : m_ptr(r.m_ptr) {
Packit 534379
        if (m_ptr)
Packit 534379
            ((Object *) m_ptr)->incRef();
Packit 534379
Packit 534379
        print_copy_created(this, "with pointer", m_ptr); track_copy_created((ref_tag*) this);
Packit 534379
    }
Packit 534379
Packit 534379
    /// Move constructor
Packit 534379
    ref(ref &&r) : m_ptr(r.m_ptr) {
Packit 534379
        r.m_ptr = nullptr;
Packit 534379
Packit 534379
        print_move_created(this, "with pointer", m_ptr); track_move_created((ref_tag*) this);
Packit 534379
    }
Packit 534379
Packit 534379
    /// Destroy this reference
Packit 534379
    ~ref() {
Packit 534379
        if (m_ptr)
Packit 534379
            ((Object *) m_ptr)->decRef();
Packit 534379
Packit 534379
        print_destroyed(this); track_destroyed((ref_tag*) this);
Packit 534379
    }
Packit 534379
Packit 534379
    /// Move another reference into the current one
Packit 534379
    ref& operator=(ref&& r) {
Packit 534379
        print_move_assigned(this, "pointer", r.m_ptr); track_move_assigned((ref_tag*) this);
Packit 534379
Packit 534379
        if (*this == r)
Packit 534379
            return *this;
Packit 534379
        if (m_ptr)
Packit 534379
            ((Object *) m_ptr)->decRef();
Packit 534379
        m_ptr = r.m_ptr;
Packit 534379
        r.m_ptr = nullptr;
Packit 534379
        return *this;
Packit 534379
    }
Packit 534379
Packit 534379
    /// Overwrite this reference with another reference
Packit 534379
    ref& operator=(const ref& r) {
Packit 534379
        print_copy_assigned(this, "pointer", r.m_ptr); track_copy_assigned((ref_tag*) this);
Packit 534379
Packit 534379
        if (m_ptr == r.m_ptr)
Packit 534379
            return *this;
Packit 534379
        if (m_ptr)
Packit 534379
            ((Object *) m_ptr)->decRef();
Packit 534379
        m_ptr = r.m_ptr;
Packit 534379
        if (m_ptr)
Packit 534379
            ((Object *) m_ptr)->incRef();
Packit 534379
        return *this;
Packit 534379
    }
Packit 534379
Packit 534379
    /// Overwrite this reference with a pointer to another object
Packit 534379
    ref& operator=(T *ptr) {
Packit 534379
        print_values(this, "assigned pointer"); track_values((ref_tag*) this, "assigned pointer");
Packit 534379
Packit 534379
        if (m_ptr == ptr)
Packit 534379
            return *this;
Packit 534379
        if (m_ptr)
Packit 534379
            ((Object *) m_ptr)->decRef();
Packit 534379
        m_ptr = ptr;
Packit 534379
        if (m_ptr)
Packit 534379
            ((Object *) m_ptr)->incRef();
Packit 534379
        return *this;
Packit 534379
    }
Packit 534379
Packit 534379
    /// Compare this reference with another reference
Packit 534379
    bool operator==(const ref &r) const { return m_ptr == r.m_ptr; }
Packit 534379
Packit 534379
    /// Compare this reference with another reference
Packit 534379
    bool operator!=(const ref &r) const { return m_ptr != r.m_ptr; }
Packit 534379
Packit 534379
    /// Compare this reference with a pointer
Packit 534379
    bool operator==(const T* ptr) const { return m_ptr == ptr; }
Packit 534379
Packit 534379
    /// Compare this reference with a pointer
Packit 534379
    bool operator!=(const T* ptr) const { return m_ptr != ptr; }
Packit 534379
Packit 534379
    /// Access the object referenced by this reference
Packit 534379
    T* operator->() { return m_ptr; }
Packit 534379
Packit 534379
    /// Access the object referenced by this reference
Packit 534379
    const T* operator->() const { return m_ptr; }
Packit 534379
Packit 534379
    /// Return a C++ reference to the referenced object
Packit 534379
    T& operator*() { return *m_ptr; }
Packit 534379
Packit 534379
    /// Return a const C++ reference to the referenced object
Packit 534379
    const T& operator*() const { return *m_ptr; }
Packit 534379
Packit 534379
    /// Return a pointer to the referenced object
Packit 534379
    operator T* () { return m_ptr; }
Packit 534379
Packit 534379
    /// Return a const pointer to the referenced object
Packit 534379
    T* get_ptr() { return m_ptr; }
Packit 534379
Packit 534379
    /// Return a pointer to the referenced object
Packit 534379
    const T* get_ptr() const { return m_ptr; }
Packit 534379
private:
Packit 534379
    T *m_ptr;
Packit 534379
};
Packit 534379
Packit 534379
#endif /* __OBJECT_H */