C++ Builder 参考手册 ➙ System::Sysutils ➙ TProc
用于定义兼容 Delphi 匿名函数的接口类或 lambda 表达式
- 简介
- 成员
• 方法 - 例子
一. System::Sysutils::TProc... 简介
- 继承关系:
IUnknown
⠀╙ System::IInterface
⠀⠀⠀┣ System::Sysutils::TProc
⠀⠀⠀┣ System::Sysutils::TProc__1
⠀⠀⠀┣ System::Sysutils::TProc__2
⠀⠀⠀┣ System::Sysutils::TProc__3
⠀⠀⠀┣ System::Sysutils::TProc__4
⠀⠀⠀┣ System::Sysutils::TFunc__1
⠀⠀⠀┣ System::Sysutils::TFunc__2
⠀⠀⠀┣ System::Sysutils::TFunc__3
⠀⠀⠀┣ System::Sysutils::TFunc__4
⠀⠀⠀┣ System::Sysutils::TFunc__5
⠀⠀⠀┗ System::Sysutils::TPredicate__1
- 头文件:
#include <System.SysUtils.hpp> - 命名空间:
System::Sysutils - TProc, TProc__1, TProc__2, TProc__3, TProc__4,
TFunc__1, TFunc__2, TFunc__3, TFunc__4, TFunc__5,
TPredicate__1 用于定义兼容 Delphi 匿名函数的接口类或 lambda 表达式:
_di_TProc, _di_TProc__1, _di_TProc__2, _di_TProc__3, _di_TProc__4,
_di_TFunc__1, _di_TFunc__2, _di_TFunc__3, _di_TFunc__4, _di_TFunc__5,
_di_TPredicate__1
二. System::Sysutils::TProc... 成员
__interface TProc : public System::IInterface
{
virtual void __fastcall Invoke() = 0 ;
};
template<typename T> __interface TProc__1 : public System::IInterface
{
virtual void __fastcall Invoke(T Arg1) = 0 ;
};
template<typename T1, typename T2> __interface TProc__2 : public System::IInterface
{
virtual void __fastcall Invoke(T1 Arg1, T2 Arg2) = 0 ;
};
template<typename T1, typename T2, typename T3> __interface TProc__3 : public System::IInterface
{
virtual void __fastcall Invoke(T1 Arg1, T2 Arg2, T3 Arg3) = 0 ;
};
template<typename T1, typename T2, typename T3, typename T4> __interface TProc__4 : public System::IInterface
{
virtual void __fastcall Invoke(T1 Arg1, T2 Arg2, T3 Arg3, T4 Arg4) = 0 ;
};
template<typename TResult> __interface TFunc__1 : public System::IInterface
{
virtual TResult __fastcall Invoke() = 0 ;
};
template<typename T, typename TResult> __interface TFunc__2 : public System::IInterface
{
virtual TResult __fastcall Invoke(T Arg1) = 0 ;
};
template<typename T1, typename T2, typename TResult> __interface TFunc__3 : public System::IInterface
{
virtual TResult __fastcall Invoke(T1 Arg1, T2 Arg2) = 0 ;
};
template<typename T1, typename T2, typename T3, typename TResult> __interface TFunc__4 : public System::IInterface
{
virtual TResult __fastcall Invoke(T1 Arg1, T2 Arg2, T3 Arg3) = 0 ;
};
template<typename T1, typename T2, typename T3, typename T4, typename TResult> __interface TFunc__5 : public System::IInterface
{
virtual TResult __fastcall Invoke(T1 Arg1, T2 Arg2, T3 Arg3, T4 Arg4) = 0 ;
};
template<typename T> __interface TPredicate__1 : public System::IInterface
{
virtual bool __fastcall Invoke(T Arg1) = 0 ;
};
1. System::Sysutils::TProc... 方法
| 方法 | 说明 |
|---|---|
| public: | |
| Invoke | 匿名函数的实现,纯虚函数。子类必须重载这个函数来实现匿名函数的功能 |
| IInterface:: | 从 System::IInterface 继承过来的 |
| public: | |
| Supports | 判断是否支持某个接口,如果支持,返回这个接口指针 |
| IUnknown:: | 从 IUnknown 继承过来的 |
| public: | |
| QueryInterface | 判断是否支持某个接口,如果支持,返回这个接口指针 |
| AddRef | 增加引用计数 |
| Release | 减少引用计数,如果引用计数等于 0 接口被销毁,占用的资源被释放 |
例1:有个浮点数数组,分别用从小到大和从大到小的顺序输出这个数组里面的数据,使用 lambda 表达式
void TForm1::MyFunc(double *pArray, int iCount, _di_TFunc__3<double, double, int> pLambda)
{
for(int iMin=0; iMin<iCount-1; iMin++)
{
for(int iFind=iMin+1; iFind<iCount; iFind++)
{
if(pLambda->Invoke(pArray[iMin], pArray[iFind]) > 0)
{
double tmp = pArray[iMin];
pArray[iMin] = pArray[iFind];
pArray[iFind] = tmp;
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
double a[8] = { 1.23, 0.69, 3.14, 2.72, 0.02, 9.65, 7.34, 3.33 };
MyFunc(a, 8, [&](double a, double b)->int { return a > b; });
Memo1->Lines->Add(L"从小到大:");
for(int i=0; i<8; i++)
Memo1->Lines->Add(a[i]);
MyFunc(a, 8, [&](double a, double b)->int { return a < b; });
Memo1->Lines->Add(L"从大到小:");
for(int i=0; i<8; i++)
Memo1->Lines->Add(a[i]);
}
运行结果:
运行结果
例2:依然是前面例1的 MyFunc 函数,不用 lambda 表达式,采用继承 System::TCppInterfacedObject<TFunc__3<double, double , int>> 的方法调用这个函数
void TForm1::MyFunc(double *pArray, int iCount, _di_TFunc__3<double, double, int> pLambda)
{
for(int iMin=0; iMin<iCount-1; iMin++)
{
for(int iFind=iMin+1; iFind<iCount; iFind++)
{
if(pLambda->Invoke(pArray[iMin], pArray[iFind]) > 0)
{
double tmp = pArray[iMin];
pArray[iMin] = pArray[iFind];
pArray[iFind] = tmp;
}
}
}
}
class TMyProcAsc : public TCppInterfacedObject<TFunc__3<double, double , int>>
{
public:
int __fastcall Invoke(double a, double b) // 这里是调用 lambda 执行的内容
{
return a > b;
}
};
class TMyProcDesc : public TCppInterfacedObject<TFunc__3<double, double , int>>
{
public:
int __fastcall Invoke(double a, double b) // 这里是调用 lambda 执行的内容
{
return a < b;
}
};
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
double a[8] = { 1.23, 0.69, 3.14, 2.72, 0.02, 9.65, 7.34, 3.33 };
MyFunc(a, 8, new TMyProcAsc); // 用 TMyProcAsc 对象代替 lambda,自动销毁
Memo1->Lines->Add(L"从小到大:");
for(int i=0; i<8; i++)
Memo1->Lines->Add(a[i]);
MyFunc(a, 8, new TMyProcDesc); // 用 TMyProcDesc 对象代替 lambda,自动销毁
Memo1->Lines->Add(L"从大到小:");
for(int i=0; i<8; i++)
Memo1->Lines->Add(a[i]);
}
相关:
- System::Sysutils::_di_TFunc__1
- System::Sysutils::_di_TFunc__2
- System::Sysutils::_di_TFunc__3
- System::Sysutils::_di_TFunc__4
- System::Sysutils::_di_TFunc__5
- System::Sysutils::_di_TPredicate__1
- System::Sysutils::_di_TProc
- System::Sysutils::_di_TProc__1
- System::Sysutils::_di_TProc__2
- System::Sysutils::_di_TProc__3
- System::Sysutils::_di_TProc__4
- System::Sysutils
- System::Classes::TThread::CreateAnonymousThread
- System::Classes::TThread
- System::Classes
- System::TCppInterfacedObject
- System::DelphiInterface
- System
C++ Builder 参考手册 ➙ System::Sysutils ➙ TProc











网友评论