From 325e1c2a5c41a6a0ad2aa65a71fa69d6a6fcb93e Mon Sep 17 00:00:00 2001 From: Mirek Fidler Date: Wed, 1 Jul 2026 14:32:27 +0200 Subject: [PATCH] Core, CtrlLib, Draw: Display is now Pte, DisplayPopup is using Ptr to be resilient wrt to destroyed Displays --- uppsrc/Core/Ptr.cpp | 11 +++-------- uppsrc/Core/Ptr.h | 29 +++++++---------------------- uppsrc/CtrlLib/DisplayPopup.cpp | 24 ++++++++++++------------ uppsrc/CtrlLib/DisplayPopup.h | 20 ++++++++++---------- uppsrc/Draw/Display.h | 2 +- uppsrc/RichText/TxtData.cpp | 2 +- 6 files changed, 34 insertions(+), 54 deletions(-) diff --git a/uppsrc/Core/Ptr.cpp b/uppsrc/Core/Ptr.cpp index b812bec4c..967fe05ed 100644 --- a/uppsrc/Core/Ptr.cpp +++ b/uppsrc/Core/Ptr.cpp @@ -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); diff --git a/uppsrc/Core/Ptr.h b/uppsrc/Core/Ptr.h index 1c348e01e..bbf844171 100644 --- a/uppsrc/Core/Ptr.h +++ b/uppsrc/Core/Ptr.h @@ -3,16 +3,15 @@ template 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 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 Ptr : public PtrBase, Moveable< Ptr > { - T *Get() const { return prec ? static_cast(prec->ptr) : NULL; } + T *Get() const { return prec ? static_cast(const_cast(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 diff --git a/uppsrc/CtrlLib/DisplayPopup.cpp b/uppsrc/CtrlLib/DisplayPopup.cpp index afb483301..5ba1e82c2 100644 --- a/uppsrc/CtrlLib/DisplayPopup.cpp +++ b/uppsrc/CtrlLib/DisplayPopup.cpp @@ -2,18 +2,18 @@ namespace Upp { -Rect DisplayPopup::screen_rect; -Ptr DisplayPopup::ctrl; -Ptr 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 DisplayPopup::ctrl; +Ptr DisplayPopup::owner; +Rect DisplayPopup::item; +Value DisplayPopup::value; +Color DisplayPopup::paper; +Color DisplayPopup::ink; +dword DisplayPopup::style; +Ptr DisplayPopup::display; +int DisplayPopup::margin; +bool DisplayPopup::usedisplaystdsize_s; + DisplayPopup::DisplayPopup() { ONCELOCK { diff --git a/uppsrc/CtrlLib/DisplayPopup.h b/uppsrc/CtrlLib/DisplayPopup.h index 43a18a606..ee65d266f 100644 --- a/uppsrc/CtrlLib/DisplayPopup.h +++ b/uppsrc/CtrlLib/DisplayPopup.h @@ -1,16 +1,16 @@ class DisplayPopup : public Pte { bool usedisplaystdsize = false; - static Rect screen_rect; - static Ptr ctrl; - static Ptr 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; + static Ptr owner; + static Rect item; + static Value value; + static Color paper, ink; + static dword style; + static Ptr display; + static int margin; + static bool usedisplaystdsize_s; static bool StateHook(Ctrl *, int reason); static bool MouseHook(Ctrl *, bool, int, Point, int, dword); diff --git a/uppsrc/Draw/Display.h b/uppsrc/Draw/Display.h index 528552bc0..11b901204 100644 --- a/uppsrc/Draw/Display.h +++ b/uppsrc/Draw/Display.h @@ -2,7 +2,7 @@ #define IMAGEFILE #include -class Display { +class Display : public Pte { public: enum { CURSOR = 0x01, diff --git a/uppsrc/RichText/TxtData.cpp b/uppsrc/RichText/TxtData.cpp index 29b015ee4..9383cf71c 100644 --- a/uppsrc/RichText/TxtData.cpp +++ b/uppsrc/RichText/TxtData.cpp @@ -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; }