C++中使用class語法實(shí)現(xiàn)回調(diào)
(當(dāng)然,椒楣,舊式的C函數(shù)指針回調(diào)也是支持的)
比如给郊,有人提供一個(gè)類庫 AfCopyFile,能夠提供文件拷貝的功能撒顿,而且能通知用戶當(dāng)前的進(jìn)度丑罪。。凤壁。
int DoCopy(const char* source,
const char* dst,
AfCopyFileListener* listener);
用戶只需要自己實(shí)現(xiàn)一個(gè)AfCopyFileListener對(duì)象吩屹,傳給這個(gè)函數(shù)就行。拧抖。煤搜。
class MainJob : public AfCopyFileListener{
int OnCopyProgress(long long total,
long long transfered){
}
}
把Listener對(duì)象傳過去
AfCopyFile af;
af.DoCopy(source, dst, this);
回調(diào)機(jī)制的缺點(diǎn):
無論是C語言的回調(diào)函數(shù),還是C++里的Listener唧席,都有一個(gè)共同的缺點(diǎn):
它使代碼邏輯變得難以閱讀擦盾。嘲驾。
我們應(yīng)盡量避免使用回調(diào)機(jī)制,最好采用單向的函數(shù)調(diào)用迹卢。
示例代碼:
AfCopyFile.h
#ifndef _AF_COPY_FILE_H
#define _AF_COPY_FILE_H
class AfCopyFile
{
public:
// 作為內(nèi)部類
class Listener
{
public:
virtual int OnCopyProgress(long long total, long long transfered) = 0;
};
public:
int DoCopy(const char* source, const char* dst, Listener* listener);
};
#endif
AfCopyFile.cpp
#include <stdio.h>
#include <Windows.h>
#include "AfCopyFile.h"
// 將LARGE_INTTEGER類型轉(zhuǎn)成unsigned long long
inline unsigned long long translate(LARGE_INTEGER num)
{
unsigned long long result = num.HighPart;
result <<= 32;
result += num.LowPart;
return result;
}
// 回調(diào)函數(shù)
// 注:要求將此函數(shù)用關(guān)鍵字CALLBACK修飾(這是Windows API的要求)
static DWORD CALLBACK CopyProgress(
LARGE_INTEGER TotalFileSize,
LARGE_INTEGER TotalBytesTransferred,
LARGE_INTEGER StreamSize,
LARGE_INTEGER StreamBytesTransferred,
DWORD dwStreamNumber,
DWORD dwCallbackReason,
HANDLE hSourceFile,
HANDLE hDestinationFile,
LPVOID lpData) // <- 這個(gè)就是上下文件對(duì)象
{
// 計(jì)算百分比
unsigned long long total = translate(TotalFileSize);
unsigned long long copied = translate(TotalBytesTransferred);
// 打印進(jìn)度
AfCopyFile::Listener* listener = (AfCopyFile::Listener*) lpData;
listener->OnCopyProgress(total, copied);
return PROGRESS_CONTINUE;
}
int AfCopyFile::DoCopy(const char* source, const char* dst, Listener* listener)
{
BOOL ret = CopyFileEx(source, dst,
&CopyProgress, // 待回調(diào)的函數(shù)
listener, // 上下文對(duì)象
NULL, 0);
return ret ? 0 : -1;
}
main.cpp
#include <stdio.h>
#include <string.h>
#include "AfCopyFile.h"
class MainJob : public AfCopyFile::Listener
{
public:
int DoJob()
{
strcpy(user, "shaofa");
strcpy(source, "c:\\test\\2.rmvb" );
strcpy(dst, "c:\\test\\2_copy.rmvb");
AfCopyFile af;
af.DoCopy(source, dst, this); // 將this傳過去
return 0;
}
int OnCopyProgress(long long total, long long transfered)
{
// 打印進(jìn)度
int percent = (int) ( (transfered * 100 / total) );
printf("[用戶: %s], %s -> %s : 進(jìn)度 %d %%\n",
user, source, dst, percent);
return 0;
}
private:
char source[256];
char dst[256];
char user[64];
};
int main()
{
MainJob job;
job.DoJob();
return 0;
}
??我的目標(biāo)是:someday辽故,即便你花錢看我的文章,也會(huì)覺得心滿意足