Core, CtrlLib, Draw: Display is now Pte, DisplayPopup is using Ptr<const Display> to be resilient wrt to destroyed Displays

This commit is contained in:
Mirek Fidler 2026-07-01 14:32:27 +02:00
parent e81224b008
commit 325e1c2a5c
6 changed files with 34 additions and 54 deletions

View file

@ -4,7 +4,7 @@ namespace Upp {
static StaticMutex sPteLock;
PteBase::Prec *PteBase::PtrAdd()
PteBase::Prec *PteBase::PtrAdd() const
{
Mutex::Lock __(sPteLock);
if(prec)
@ -27,11 +27,6 @@ void PteBase::PtrRelease(Prec *prec)
}
}
PteBase::PteBase()
{
prec = NULL;
}
PteBase::~PteBase()
{
Mutex::Lock __(sPteLock);
@ -44,12 +39,12 @@ void PtrBase::Release()
PteBase::PtrRelease(prec);
}
void PtrBase::Set(PteBase *p)
void PtrBase::Set(const PteBase *p)
{
prec = p ? p->PtrAdd() : NULL;
}
void PtrBase::Assign(PteBase *p)
void PtrBase::Assign(const PteBase *p)
{
Release();
Set(p);

View file

@ -3,16 +3,15 @@ template <class T> class Ptr;
class PteBase {
protected:
struct Prec {
PteBase *ptr;
Atomic n;
const PteBase *ptr;
Atomic n;
};
volatile Prec *prec;
mutable Prec *prec = nullptr; // mutable as we need Ptr<const Foo> as well
Prec *PtrAdd();
Prec *PtrAdd() const;
static void PtrRelease(Prec *prec);
PteBase();
~PteBase();
friend class PtrBase;
@ -21,9 +20,9 @@ protected:
class PtrBase {
protected:
PteBase::Prec *prec;
void Set(PteBase *p);
void Set(const PteBase *p);
void Release();
void Assign(PteBase *p);
void Assign(const PteBase *p);
public:
~PtrBase();
@ -36,7 +35,7 @@ class Pte : public PteBase {
template <class T>
class Ptr : public PtrBase, Moveable< Ptr<T> > {
T *Get() const { return prec ? static_cast<T *>(prec->ptr) : NULL; }
T *Get() const { return prec ? static_cast<T *>(const_cast<PteBase *>(prec->ptr)) : NULL; }
public:
T *operator->() const { return Get(); }
@ -51,20 +50,6 @@ public:
Ptr(const Ptr& ptr) { Set(ptr.Get()); }
String ToString() const;
friend bool operator==(const Ptr& a, const T *b) { return a.Get() == b; }
friend bool operator==(const T *a, const Ptr& b) { return a == b.Get(); }
friend bool operator==(const Ptr& a, const Ptr& b) { return a.prec == b.prec; }
friend bool operator==(const Ptr& a, T *b) { return a.Get() == b; }
friend bool operator==(T *a, const Ptr& b) { return a == b.Get(); }
friend bool operator!=(const Ptr& a, const T *b) { return a.Get() != b; }
friend bool operator!=(const T *a, const Ptr& b) { return a != b.Get(); }
friend bool operator!=(const Ptr& a, const Ptr& b) { return a.prec != b.prec; }
friend bool operator!=(const Ptr& a, T *b) { return a.Get() != b; }
friend bool operator!=(T *a, const Ptr& b) { return a != b.Get(); }
};
template <class T>

View file

@ -2,18 +2,18 @@
namespace Upp {
Rect DisplayPopup::screen_rect;
Ptr<Ctrl> DisplayPopup::ctrl;
Ptr<DisplayPopup> DisplayPopup::owner;
Rect DisplayPopup::item;
Value DisplayPopup::value;
Color DisplayPopup::paper;
Color DisplayPopup::ink;
dword DisplayPopup::style;
const Display *DisplayPopup::display;
int DisplayPopup::margin;
bool DisplayPopup::usedisplaystdsize_s;
Rect DisplayPopup::screen_rect;
Ptr<Ctrl> DisplayPopup::ctrl;
Ptr<DisplayPopup> DisplayPopup::owner;
Rect DisplayPopup::item;
Value DisplayPopup::value;
Color DisplayPopup::paper;
Color DisplayPopup::ink;
dword DisplayPopup::style;
Ptr<const Display> DisplayPopup::display;
int DisplayPopup::margin;
bool DisplayPopup::usedisplaystdsize_s;
DisplayPopup::DisplayPopup()
{
ONCELOCK {

View file

@ -1,16 +1,16 @@
class DisplayPopup : public Pte<DisplayPopup> {
bool usedisplaystdsize = false;
static Rect screen_rect;
static Ptr<Ctrl> ctrl;
static Ptr<DisplayPopup> owner;
static Rect item;
static Value value;
static Color paper, ink;
static dword style;
static const Display *display;
static int margin;
static bool usedisplaystdsize_s;
static Rect screen_rect;
static Ptr<Ctrl> ctrl;
static Ptr<DisplayPopup> owner;
static Rect item;
static Value value;
static Color paper, ink;
static dword style;
static Ptr<const Display> display;
static int margin;
static bool usedisplaystdsize_s;
static bool StateHook(Ctrl *, int reason);
static bool MouseHook(Ctrl *, bool, int, Point, int, dword);

View file

@ -2,7 +2,7 @@
#define IMAGEFILE <Draw/DrawImg.iml>
#include <Draw/iml_header.h>
class Display {
class Display : public Pte<Display> {
public:
enum {
CURSOR = 0x01,

View file

@ -484,7 +484,7 @@ String RichCellPos::ToString() const
{
String s;
s << "pos: " << pos << ", textlen: " << textlen << ", size: " << tabsize << ", tabpos: " << tabpos
<< ", tablen: " << tablen << ", cellpos: " << cellpos << ", celllen: " << celllen << ", level: " << level;
<< ", tablen: " << tablen << ", cellpos: " << cellpos << ", level: " << level;
return s;
}