Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » ScatterDraw & ScatterCtrl » Use function pointer
Use function pointer [message #48221] Tue, 06 June 2017 12:52 Go to next message
deep is currently offline  deep
Messages: 227
Registered: July 2011
Location: Bangalore
Experienced Member
Hi,

Actually this is not Scatter package issue.

I want to use Function pointer (Class pointer) to access internal variables from ScatterCtrlDemo.

From file tab14_userequations.cpp
ScatterDemo *ConstructUserEquation()
{
	static TabUserEquation tab_ue;
	return &tab_ue;
}

INITBLOCK {
	RegisterExample("User equation", ConstructUserEquation, __FILE__);
}


From file main.cpp
struct Example {
	ScatterDemo* (*ctrl)();
	String name;
	int index;
};

Array<Example>& Examples()
{
	static Array<Example> x;
	return x;
}

void RegisterExample(const char *name, ScatterDemo* (*ctrl)(), String fileName)
{
	Example& x = Examples().Add();
	x.name = name;
	x.ctrl = ctrl;
	x.index = ScanInt(GetFileName(fileName).Mid(3, 2));
}


Now with RegisterExample static TabUserEquation tab_ue gets added to Examples Array as Type ScatterDemo.
I want to use tab_ue in other part of code.

How to declare it so that it will be part of Examples Array and available independently.


Warm Regards

Deepak

[Updated on: Tue, 06 June 2017 12:53]

Report message to a moderator

Re: Use function pointer [message #48228 is a reply to message #48221] Wed, 07 June 2017 09:16 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3246
Registered: August 2008
Senior Veteran
Dear Deepak

Actually you can handle static TabUserEquation tab_ue from any part of the code through Examples().

With Examples()[i].name you get the name of the demo to be sure is that index "i" is the one that you want, and with Examples()[i].ctrl() you have access to tab_ue.


Best regards
Iñaki
Re: Use function pointer [message #48229 is a reply to message #48228] Wed, 07 June 2017 10:23 Go to previous messageGo to next message
deep is currently offline  deep
Messages: 227
Registered: July 2011
Location: Bangalore
Experienced Member
Dear Koldo,

Thanks for your answer.

Considering same example there is EditString "equation" on the layout.

I want to know how to access it.

Examples()[i].ctrl() gives me access to elements of struct "Example" only.


Warm Regards

Deepak

[Updated on: Wed, 07 June 2017 10:23]

Report message to a moderator

Re: Use function pointer [message #48269 is a reply to message #48229] Tue, 13 June 2017 12:50 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3246
Registered: August 2008
Senior Veteran
Hello Deep

Probably I would prefer to save a reference to the tab and using it directly.

However you can use this everywhere:
for (int i = 0; i < Examples().GetCount(); ++i) {
	if (Examples()[i].name == "User equation") {
		TabUserEquation &tab = dynamic_cast<TabUserEquation&>(*Examples()[i].ctrl());
		EditString &equation = tab.equation;
		equation.SetText("HELLO WORLD!");
	}
}


Best regards
Iñaki
Re: Use function pointer [SOLVED] [message #48270 is a reply to message #48269] Tue, 13 June 2017 13:22 Go to previous message
deep is currently offline  deep
Messages: 227
Registered: July 2011
Location: Bangalore
Experienced Member
Hello Koldo,

Thank you.

I checked it in my code. It is working as expected.


Warm Regards

Deepak

[Updated on: Wed, 14 June 2017 08:38]

Report message to a moderator

Previous Topic: bug in scatterdraw.h and its fix
Next Topic: Dynamic example - 100% CPU core load
Goto Forum:
  


Current Time: Sun Jan 17 10:23:23 CET 2021

Total time taken to generate the page: 0.01485 seconds