.reference

git-svn-id: svn://ultimatepp.org/upp/trunk@10280 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2016-10-09 15:55:30 +00:00
parent 2bb38b8f97
commit 473925253a
6 changed files with 46 additions and 60 deletions

View file

@ -9,6 +9,7 @@ struct Foo {
void ActionWithParam(int y) { Cout() << "ActionWithParam: " << x + y << '\n'; } void ActionWithParam(int y) { Cout() << "ActionWithParam: " << x + y << '\n'; }
Event<> WhenDo; Event<> WhenDo;
void Do() { WhenDo(); } void Do() { WhenDo(); }
Foo(int x = 0) : x(x) {} Foo(int x = 0) : x(x) {}
@ -22,11 +23,9 @@ void Fn()
struct Bar { struct Bar {
Foo foo; Foo foo;
void Action() { Cout() << "foo's Do called\n"; } Bar() {
foo.WhenDo = [=] { Cout() << "foo's Do called\n"; };
typedef Bar CLASSNAME; }
Bar() { foo.WhenDo = THISBACK(Action); }
}; };
struct Safe : Pte<Safe> { struct Safe : Pte<Safe> {
@ -61,10 +60,10 @@ CONSOLE_APP_MAIN
Cout() << "---------\n"; Cout() << "---------\n";
{ {
Safe f; Safe f;
ev4 = pteback(&f, &Safe::Action); Ptr<Safe> p = &f;
Cout() << "callback valid: " << (bool)ev4 << '\n'; ev4 = [=] { if(p) p->Action(); };
ev4();
}
Cout() << "callback valid: " << (bool)ev4 << '\n'; Cout() << "callback valid: " << (bool)ev4 << '\n';
ev4(); ev4();
} // Safe f gets destroyed here, which makes capture 'p' NULL
ev4(); // captured 'p' is now NULL, so no Action is called
} }

View file

@ -5,9 +5,6 @@ using namespace Upp;
struct App : TopWindow { struct App : TopWindow {
Thread work; Thread work;
void Work();
void AskQuit(bool *quit);
ArrayCtrl list; ArrayCtrl list;
typedef App CLASSNAME; typedef App CLASSNAME;
@ -16,22 +13,24 @@ struct App : TopWindow {
~App(); ~App();
}; };
void App::AskQuit(bool *quit) App::App()
{
*quit = PromptYesNo("Quit?");
}
void App::Work()
{ {
list.AddColumn("Test");
Add(list.SizePos());
work.Run([=] {
for(;;) { for(;;) {
Sleep(1); Sleep(1);
GuiLock __; GuiLock __; // After locking GuiLock __, access is allowed to GUI object except opening/closing windows
if(IsShutdownThreads()) if(IsShutdownThreads())
break; break;
if(list.GetCount() > 100) { if(list.GetCount() > 100) {
bool quit; bool quit;
Call(PTEBACK1(AskQuit, &quit)); // This is the generic way for any GUI (dlg) Ptr<Ctrl> p = this;
// quit = PromptYesNo("Quit?"); // But Prompt has this ability already implemented 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) { if(quit) {
Break(); Break();
return; return;
@ -40,13 +39,7 @@ void App::Work()
} }
list.Add((int64)Random()); list.Add((int64)Random());
} }
} });
App::App()
{
list.AddColumn("Test");
Add(list.SizePos());
work.Run(THISBACK(Work));
} }
App::~App() App::~App()

View file

@ -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 uses
CtrlLib; CtrlLib;

View file

@ -5,9 +5,12 @@ using namespace Upp;
#define LAYOUTFILE <GuiMT/Divisors.lay> #define LAYOUTFILE <GuiMT/Divisors.lay>
#include <CtrlCore/lay.h> #include <CtrlCore/lay.h>
struct Divisors : public WithDivisorsLayout<TopWindow> { // This example shows older way how to do multithreded GUI using PostCallback to communicate
typedef Divisors CLASSNAME; // 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<TopWindow> {
volatile Atomic terminated; volatile Atomic terminated;
volatile Atomic threads; volatile Atomic threads;
@ -42,10 +45,10 @@ void WorkerThread(DivisorsInfo f)
r2 = " " + AsString(j) + r2; r2 = " " + AsString(j) + r2;
divisors++; 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); AtomicDec(f.gui->threads);
} }
@ -79,7 +82,7 @@ Divisors::Divisors()
{ {
CtrlLayout(*this, "Window title"); CtrlLayout(*this, "Window title");
Sizeable().Zoomable(); Sizeable().Zoomable();
push <<= THISBACK(TestNumber); push << [=] { TestNumber(); };
editor.SetFilter(CharFilterDigit); editor.SetFilter(CharFilterDigit);
table.AddColumn("Number"); table.AddColumn("Number");
table.AddColumn("Divisors"); table.AddColumn("Divisors");

View file

@ -18,9 +18,7 @@ struct WebCrawler : public WithCrawlerLayout<TopWindow> {
void ExtractUrls(const String& html, int srci); void ExtractUrls(const String& html, int srci);
void ShowPath(); void ShowPath();
void OpenURL(ArrayCtrl *a); void OpenURL(ArrayCtrl& a);
typedef WebCrawler CLASSNAME;
public: public:
void Run(); void Run();
@ -125,9 +123,9 @@ void WebCrawler::ShowPath()
path.Add(p[i]); path.Add(p[i]);
} }
void WebCrawler::OpenURL(ArrayCtrl *a) void WebCrawler::OpenURL(ArrayCtrl& a)
{ {
String u = a->GetKey(); String u = a.GetKey();
WriteClipboardText(u); WriteClipboardText(u);
LaunchWebBrowser(u); LaunchWebBrowser(u);
} }
@ -139,10 +137,10 @@ WebCrawler::WebCrawler()
work.AddColumn("Status"); work.AddColumn("Status");
finished.AddColumn("Finished"); finished.AddColumn("Finished");
finished.AddColumn("Response"); finished.AddColumn("Response");
finished.WhenCursor = THISBACK(ShowPath); finished.WhenCursor = [=] { ShowPath(); };
finished.WhenLeftDouble = THISBACK1(OpenURL, &finished); finished.WhenLeftDouble = [=] { OpenURL(finished); };
path.AddColumn("Path"); path.AddColumn("Path");
path.WhenLeftDouble = THISBACK1(OpenURL, &path); path.WhenLeftDouble = [=] { OpenURL(path); };
total = 0; total = 0;
Zoomable().Sizeable(); Zoomable().Sizeable();
} }

View file

@ -11,8 +11,6 @@ using namespace Upp;
#include <Draw/iml_source.h> #include <Draw/iml_source.h>
struct MyApp : TopWindow { struct MyApp : TopWindow {
typedef MyApp CLASSNAME;
DropList method; DropList method;
void Paint(Draw& w) { void Paint(Draw& w) {
@ -40,11 +38,6 @@ struct MyApp : TopWindow {
w.DrawText(GetSize().cx - 200, 40, String().Cat() << "Elapsed " << tm << "s"); w.DrawText(GetSize().cx - 200, 40, String().Cat() << "Elapsed " << tm << "s");
} }
void Sync()
{
Refresh();
}
MyApp() { MyApp() {
SetRect(0, 0, 600, 800); SetRect(0, 0, 600, 800);
Sizeable(); Sizeable();