subsurface/qt-quick/chartitem_ptr.h
Berthold Stoeger 2eebae13dd stats: break out common QtQuick part of the code
Move most of the QtQuick code to its own directory, so that it
can be reused in the future for the chart.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-09-11 10:35:04 +02:00

51 lines
1,021 B
C++

// SPDX-License-Identifier: GPL-2.0
// A stupid pointer class that initializes to null and can be copy
// assigned. This is for historical reasons: unique_ptrs to ChartItems
// were replaced by plain pointers. Instead of nulling the plain pointers
// in the constructors, use this. Ultimately, we might think about making
// this thing smarter, once removal of individual ChartItems is implemented.
#ifndef CHARITEM_PTR_H
#define CHARITEM_PTR_H
template <typename T>
class ChartItemPtr {
friend class ChartView; // Only the chart view can create these pointers
T *ptr;
ChartItemPtr(T *ptr) : ptr(ptr)
{
}
public:
ChartItemPtr() : ptr(nullptr)
{
}
ChartItemPtr(const ChartItemPtr &p) : ptr(p.ptr)
{
}
void reset()
{
ptr = nullptr;
}
ChartItemPtr &operator=(const ChartItemPtr &p)
{
ptr = p.ptr;
return *this;
}
operator bool() const
{
return !!ptr;
}
bool operator!() const
{
return !ptr;
}
T &operator*() const
{
return *ptr;
}
T *operator->() const
{
return ptr;
}
};
#endif