How to set lambda to event properties. [C++Builder][JAPAN]

Posted by on in Blogs

C++Builder, lambda can be set to void* variable.

////
void* test = [](TObject* Sender){}; //Build succeeded.

void* test = [&](TObject* Sender){};  //Error

Capture was not available.

TMethod has two void* variables.

////
struct DECLSPEC_DRECORD TMethod
{
public:
	void *Code;
	void *Data;
///......
}

I wrote lambda in the "code" variable.

////
	TMethod l_method;
	l_method.Data = nullptr;
	l_method.Code = [](TObject* Sender){ShowMessage(L"set lambda to event properties. ");};

TButton(Button1) to one created, you add it to the OnClick event.

////
	Button1->OnClick = reinterpret_cast<TNotifyEvent&>(l_method);

Succeeded by calling a ShowMessage() with a Button1 OnClick event.

 

[An example that can be used in vector.]

////
	std::vector<TMethod> l_event;
	l_event.push_back([this]()->TMethod
	{
		TMethod l_;
		l_.Data = nullptr;
		l_.Code = [](TObject* Sender)
		{
			ShowMessage(Form1->Caption);
		};
		return l_;
	}() );
	Button1->OnClick = reinterpret_cast<TNotifyEvent&>(*l_event.begin());

[further]

////
namespace fmx_method
{
	struct method
	{
		TMethod f_method;
		method()
		{
			f_method.Data = nullptr;
		}
		method(const TMethod& value)
		{
			method();
			f_method = value;
		}
		static TMethod& method_get(void* value)
		{
			fmx_method::method l_method;
			l_method.f_method.Code = value;
			return l_method.f_method;
		}
		~method()
		{

		}
	};
}

	Button1->OnClick = reinterpret_cast<TNotifyEvent&>(fmx_method::method::method_get(
		static_cast<void*>([](TObject* Sender){ShowMessage(L"set lambda to event properties. ");})
	));

 

I am happy if I can set lambda directly in the property.

Among them, I hope strongly that it is implemented.

 

 



About
Gold User, No rank,
Delphi / C++Builder blogger

Comments

Check out more tips and tricks in this development video: