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 » U++ Library : Other (not classified elsewhere) » I request the implementation of callback5
Re: I request the implementation of callback5 [message #49819 is a reply to message #49818] Thu, 10 May 2018 10:33 Go to previous messageGo to previous message
Oblivion is currently offline  Oblivion
Messages: 1202
Registered: August 2007
Senior Contributor
Quote:

I will use anything else if it works..
What do they do?
What is difference among them?
Advantages/disadvantages?


They are basically the same. You can consider the Upp::Function (and its two derivatives Event, and Gate) as the re-implemantation of the old U++ Calllback mechanism, using the C++11 features such as variadic templates.

From the U++ official documentation:
Upp::Function is wrapper to represent callable operation. It is similar to std::function with two differences:

- Calling empty Function is allowed (and NOP). Returns zero.
- Functions can be combined (chained) using operator<<



So,

#include <Core/Core.h>

using namespace Upp;

struct Foo {
	Event<int, int> WhenAddition;
	void DoAddition(int a, int b) { WhenAddition(a, b); }
};

CONSOLE_APP_MAIN
{
	StdLogSetup(LOG_COUT);
	
	// I am explicitly specifying the type here for educational purpose.
	// Normally you can simply use auto, where it is proper.
	Event<int, int> Addition = [=](int a, int b) {
		LOG("Foo::DoAddition: " << a + b);
	};
	
	Gate<int, int> Subtraction = [=](int a, int b) {
		return a - b > 0;
	};
	
	Function<int(int, int)> Multiplication = [=](int a, int b) {
		return a * b;
	};
	
	Foo myfoo;
	myfoo.WhenAddition = pick(Addition); 
	
	int a = 10, b = 5;
	
	myfoo.DoAddition(a, b);
	
	if(Subtraction(a, b))
		LOG("a is greater than b");
	
	LOG(Multiplication(a, b));
}


Best regards,
Oblivion


[Updated on: Thu, 10 May 2018 13:30]

Report message to a moderator

 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: design flaw in scatterctrl?
Next Topic: How can I turn off soring in ArrayCtrl?
Goto Forum:
  


Current Time: Sat Apr 26 09:52:00 CEST 2025

Total time taken to generate the page: 0.01013 seconds