09-13-2019, 05:05 PM
Sample anonymous function usage with C++ classic compiler without lambda functions:
//---------------------------------------------------------------------------
From 'How to Handle Delphi Anonymous Methods in C++' RAD Studio help.
This needs to be included once so the function pointers can be wrapped.
//---------------------------------------------------------------------------
enum _DummyType{};
template <typename INTF, // Interface with Invoke
typename F, // Functor type
typename R, // Return type
typename P1 = _DummyType, // Param #1
typename P2 = _DummyType, // Param #2
typename P3 = _DummyType, // Param #3
typename P4 = _DummyType, // Param #4
typename P5 = _DummyType> // Param #5
class TMethodRef : public TCppInterfacedObject<INTF>
{
private:
F callback;
public:
TMethodRef(F _callback) : callback(_callback) {}
INTFOBJECT_IMPL_IUNKNOWN(TInterfacedObject);
R __fastcall Invoke(P1 p1) {
return callback(p1);
}
R __fastcall Invoke(P1 p1, P2 p2) {
return callback(p1, p2);
}
R __fastcall Invoke(P1 p1, P2 p2, P3 p3) {
return callback(p1, p2, p3);
}
R __fastcall Invoke(P1 p1, P2 p2, P3 p3, P4 p4) {
return callback(p1, p2, p3, p4);
}
R __fastcall Invoke(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
return callback(p1, p2, p3, p4, p5);
}
};
//Sample TSessionProc callback
//---------------------------------------------------------------------------
void LookUpProc(System::TObject* obj)
{
TIWApplication *ASession = (TIWApplication *)obj;
UserSession()->tmpStr = ASession->IP+","+ASession->LastAccess.FormatString("yyyy-mm-dd hh:nn
s");
}
// Sample callback usage
//---------------------------------------------------------------------------
void __fastcall TMJForm::btnRefreshAsyncClick(TObject *Sender, TStringList *EventParams)
{
auto_ptr<TStringList> sl(new TStringList), sl2(new TStringList);
gSessions->GetList(sl.get());
for(int i=0;i<sl->Count;i++) {
gSessions->LookUp(sl->Strings[i],new TMethodRef<Iwlfsessionlist::TSessionProc,void (*)(System::TObject*),void,System::TObject* >(LookUpProc));
sl2->DelimitedText = UserSession()->tmpStr;
...
}
}
//---------------------------------------------------------------------------
From 'How to Handle Delphi Anonymous Methods in C++' RAD Studio help.
This needs to be included once so the function pointers can be wrapped.
//---------------------------------------------------------------------------
enum _DummyType{};
template <typename INTF, // Interface with Invoke
typename F, // Functor type
typename R, // Return type
typename P1 = _DummyType, // Param #1
typename P2 = _DummyType, // Param #2
typename P3 = _DummyType, // Param #3
typename P4 = _DummyType, // Param #4
typename P5 = _DummyType> // Param #5
class TMethodRef : public TCppInterfacedObject<INTF>
{
private:
F callback;
public:
TMethodRef(F _callback) : callback(_callback) {}
INTFOBJECT_IMPL_IUNKNOWN(TInterfacedObject);
R __fastcall Invoke(P1 p1) {
return callback(p1);
}
R __fastcall Invoke(P1 p1, P2 p2) {
return callback(p1, p2);
}
R __fastcall Invoke(P1 p1, P2 p2, P3 p3) {
return callback(p1, p2, p3);
}
R __fastcall Invoke(P1 p1, P2 p2, P3 p3, P4 p4) {
return callback(p1, p2, p3, p4);
}
R __fastcall Invoke(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
return callback(p1, p2, p3, p4, p5);
}
};
//Sample TSessionProc callback
//---------------------------------------------------------------------------
void LookUpProc(System::TObject* obj)
{
TIWApplication *ASession = (TIWApplication *)obj;
UserSession()->tmpStr = ASession->IP+","+ASession->LastAccess.FormatString("yyyy-mm-dd hh:nn
s");}
// Sample callback usage
//---------------------------------------------------------------------------
void __fastcall TMJForm::btnRefreshAsyncClick(TObject *Sender, TStringList *EventParams)
{
auto_ptr<TStringList> sl(new TStringList), sl2(new TStringList);
gSessions->GetList(sl.get());
for(int i=0;i<sl->Count;i++) {
gSessions->LookUp(sl->Strings[i],new TMethodRef<Iwlfsessionlist::TSessionProc,void (*)(System::TObject*),void,System::TObject* >(LookUpProc));
sl2->DelimitedText = UserSession()->tmpStr;
...
}
}

