From 473925253ae4636735a7ae6c1207b47332d9683f Mon Sep 17 00:00:00 2001 From: cxl Date: Sun, 9 Oct 2016 15:55:30 +0000 Subject: [PATCH] .reference git-svn-id: svn://ultimatepp.org/upp/trunk@10280 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- reference/Event/main.cpp | 17 ++++---- reference/GuiLock/main.cpp | 53 ++++++++++------------- reference/GuiMT/GuiMT.upp | 2 +- reference/GuiMT/main.cpp | 13 +++--- reference/GuiWebCrawler/GuiWebCrawler.cpp | 14 +++--- reference/RescaleFilter/main.cpp | 7 --- 6 files changed, 46 insertions(+), 60 deletions(-) diff --git a/reference/Event/main.cpp b/reference/Event/main.cpp index 9b92f546f..7b98191d1 100644 --- a/reference/Event/main.cpp +++ b/reference/Event/main.cpp @@ -9,6 +9,7 @@ struct Foo { void ActionWithParam(int y) { Cout() << "ActionWithParam: " << x + y << '\n'; } Event<> WhenDo; + void Do() { WhenDo(); } Foo(int x = 0) : x(x) {} @@ -22,11 +23,9 @@ void Fn() struct Bar { Foo foo; - void Action() { Cout() << "foo's Do called\n"; } - - typedef Bar CLASSNAME; - - Bar() { foo.WhenDo = THISBACK(Action); } + Bar() { + foo.WhenDo = [=] { Cout() << "foo's Do called\n"; }; + } }; struct Safe : Pte { @@ -61,10 +60,10 @@ CONSOLE_APP_MAIN Cout() << "---------\n"; { Safe f; - ev4 = pteback(&f, &Safe::Action); + Ptr p = &f; + ev4 = [=] { if(p) p->Action(); }; Cout() << "callback valid: " << (bool)ev4 << '\n'; ev4(); - } - Cout() << "callback valid: " << (bool)ev4 << '\n'; - ev4(); + } // Safe f gets destroyed here, which makes capture 'p' NULL + ev4(); // captured 'p' is now NULL, so no Action is called } diff --git a/reference/GuiLock/main.cpp b/reference/GuiLock/main.cpp index 0cfc5e5c4..7fa51b772 100644 --- a/reference/GuiLock/main.cpp +++ b/reference/GuiLock/main.cpp @@ -4,9 +4,6 @@ using namespace Upp; struct App : TopWindow { Thread work; - - void Work(); - void AskQuit(bool *quit); ArrayCtrl list; @@ -16,37 +13,33 @@ struct App : TopWindow { ~App(); }; -void App::AskQuit(bool *quit) -{ - *quit = PromptYesNo("Quit?"); -} - -void App::Work() -{ - for(;;) { - Sleep(1); - GuiLock __; - if(IsShutdownThreads()) - break; - if(list.GetCount() > 100) { - bool quit; - Call(PTEBACK1(AskQuit, &quit)); // This is the generic way for any GUI (dlg) - // quit = PromptYesNo("Quit?"); // But Prompt has this ability already implemented - if(quit) { - Break(); - return; - } - list.Clear(); - } - list.Add((int64)Random()); - } -} - App::App() { list.AddColumn("Test"); Add(list.SizePos()); - work.Run(THISBACK(Work)); + work.Run([=] { + for(;;) { + Sleep(1); + GuiLock __; // After locking GuiLock __, access is allowed to GUI object except opening/closing windows + if(IsShutdownThreads()) + break; + if(list.GetCount() > 100) { + bool quit; + Ptr p = this; + Call([=, &quit] { + if(p) // check whether App still exists (e.g. can be closed by close button) + quit = PromptYesNo("Quit?"); + }); + // quit = PromptYesNo("Quit?"); // (but Prompt has this ability already implemented, so direct call would work too) + if(quit) { + Break(); + return; + } + list.Clear(); + } + list.Add((int64)Random()); + } + }); } App::~App() diff --git a/reference/GuiMT/GuiMT.upp b/reference/GuiMT/GuiMT.upp index cfad8977d..b3873cde5 100644 --- a/reference/GuiMT/GuiMT.upp +++ b/reference/GuiMT/GuiMT.upp @@ -1,4 +1,4 @@ -description "Using event queue for communication between worker threads and GUI"; +description "Using event queue for communication between worker threads and GUI\377"; uses CtrlLib; diff --git a/reference/GuiMT/main.cpp b/reference/GuiMT/main.cpp index 861c69f4e..551434d5a 100644 --- a/reference/GuiMT/main.cpp +++ b/reference/GuiMT/main.cpp @@ -5,9 +5,12 @@ using namespace Upp; #define LAYOUTFILE #include -struct Divisors : public WithDivisorsLayout { - typedef Divisors CLASSNAME; +// This example shows older way how to do multithreded GUI using PostCallback to communicate +// between worker thread and main GUI thread. +// While PostCallback is still useful in certain circumstances, new applications are +// perhaps better off using GuiLock and Call, as demostrated in GuiLock reference example. +struct Divisors : public WithDivisorsLayout { volatile Atomic terminated; volatile Atomic threads; @@ -42,10 +45,10 @@ void WorkerThread(DivisorsInfo f) r2 = " " + AsString(j) + r2; divisors++; } - PostCallback(callback2(f.gui, &Divisors::ShowResult, f.line, "working..." + r1 + r2)); + PostCallback([=] { f.gui->ShowResult(f.line, "working..." + r1 + r2); }); } } - PostCallback(callback2(f.gui, &Divisors::ShowResult, f.line, AsString(divisors) + ": " + r1 + r2)); + PostCallback([=] { f.gui->ShowResult(f.line, AsString(divisors) + ": " + r1 + r2); }); AtomicDec(f.gui->threads); } @@ -79,7 +82,7 @@ Divisors::Divisors() { CtrlLayout(*this, "Window title"); Sizeable().Zoomable(); - push <<= THISBACK(TestNumber); + push << [=] { TestNumber(); }; editor.SetFilter(CharFilterDigit); table.AddColumn("Number"); table.AddColumn("Divisors"); diff --git a/reference/GuiWebCrawler/GuiWebCrawler.cpp b/reference/GuiWebCrawler/GuiWebCrawler.cpp index 264043d2c..d7170bef9 100644 --- a/reference/GuiWebCrawler/GuiWebCrawler.cpp +++ b/reference/GuiWebCrawler/GuiWebCrawler.cpp @@ -18,9 +18,7 @@ struct WebCrawler : public WithCrawlerLayout { void ExtractUrls(const String& html, int srci); void ShowPath(); - void OpenURL(ArrayCtrl *a); - - typedef WebCrawler CLASSNAME; + void OpenURL(ArrayCtrl& a); public: void Run(); @@ -125,9 +123,9 @@ void WebCrawler::ShowPath() path.Add(p[i]); } -void WebCrawler::OpenURL(ArrayCtrl *a) +void WebCrawler::OpenURL(ArrayCtrl& a) { - String u = a->GetKey(); + String u = a.GetKey(); WriteClipboardText(u); LaunchWebBrowser(u); } @@ -139,10 +137,10 @@ WebCrawler::WebCrawler() work.AddColumn("Status"); finished.AddColumn("Finished"); finished.AddColumn("Response"); - finished.WhenCursor = THISBACK(ShowPath); - finished.WhenLeftDouble = THISBACK1(OpenURL, &finished); + finished.WhenCursor = [=] { ShowPath(); }; + finished.WhenLeftDouble = [=] { OpenURL(finished); }; path.AddColumn("Path"); - path.WhenLeftDouble = THISBACK1(OpenURL, &path); + path.WhenLeftDouble = [=] { OpenURL(path); }; total = 0; Zoomable().Sizeable(); } diff --git a/reference/RescaleFilter/main.cpp b/reference/RescaleFilter/main.cpp index d49b9266b..9dcce9c5f 100644 --- a/reference/RescaleFilter/main.cpp +++ b/reference/RescaleFilter/main.cpp @@ -11,8 +11,6 @@ using namespace Upp; #include struct MyApp : TopWindow { - typedef MyApp CLASSNAME; - DropList method; void Paint(Draw& w) { @@ -39,11 +37,6 @@ struct MyApp : TopWindow { w.DrawText(GetSize().cx - 200, 40, String().Cat() << "Elapsed " << tm << "s"); } - - void Sync() - { - Refresh(); - } MyApp() { SetRect(0, 0, 600, 800);