C與C++ API的比較
在c語言中,API體現(xiàn)為c函數(shù),如操作系統(tǒng)提供的一系列API,在c++中,API體現(xiàn)為自由函數(shù),這里的自由函數(shù)是指除普通成員函數(shù)、靜態(tài)成員函數(shù)、友元函數(shù)外的能在某命名空間作用域或全局空間內(nèi)直接訪問的函數(shù),而這更多地體現(xiàn)為函數(shù)模板,如stl提供的一系列算法swap、count和sort等。相對于c API,c++ API具有類型安全和封閉開放的優(yōu)點,類型安全是因為c++本身就是一種比c更強的靜態(tài)類型語言,而封閉開放是指函數(shù)的設(shè)計實現(xiàn)一部分是固定的,而另一部分可以是靈活擴展的,這表現(xiàn)為函數(shù)模板的重載和全局特化。本文主要講述如何運用函數(shù)模板來設(shè)計應(yīng)用程序API,并以windows平臺為例說明。
Windows雙版本API
在windows中,很多API通常都有ANSI和UNICODE兩種字符集形式,其命名對應(yīng)為xxxA和xxxW。如果應(yīng)用層需要針對這些API來封裝,為完備起見,就需要考慮ANSI和UNICODE兩種版本。一般有兩種方法:第一種是先實現(xiàn)一個A(或W)版本,而W(或A)版本的實現(xiàn)則是在其內(nèi)部將UNICODE(或ANSI)型數(shù)化轉(zhuǎn)化為ANSI(或UNICODE)類型,再調(diào)用A(或W)版本,這種方法因需要作字符集的轉(zhuǎn)換來實現(xiàn),因而效率較低;第二種是兩個版本平行實現(xiàn),即A版本調(diào)用系統(tǒng)A版本API實現(xiàn),W版本調(diào)用系統(tǒng)W版本API實現(xiàn),這種方法的缺點是結(jié)果產(chǎn)生除了A或W API調(diào)用不同外很多的重復(fù)代碼。在A和W版本都實現(xiàn)后,進一步,可根據(jù)編譯器的宏定義_UNICODE或UNICODE來定義一個自己的API宏。那么除以上兩種方法外,還有沒有更好的方法呢?而這種方法必然要能夠兼顧效率和避免代碼的重復(fù)冗余。在使用這個方法前,有下列幾個問題:
1)如何根據(jù)泛型參數(shù)來選擇定義正確的結(jié)構(gòu)體,因為有些系統(tǒng)API的參數(shù)中不僅字符串類型有A和W兩種類型,而且凡是其內(nèi)部包含字符串類型的結(jié)構(gòu)體因而也帶有A和W兩種類型。
2)如何根據(jù)泛型參數(shù)來選擇調(diào)用正確版本的系統(tǒng)API。
針對第1個問題,泛型參數(shù)通常只有A或W兩種,因此可以使用選擇特征類模板來實現(xiàn),如boost中的if_c,softstl中的select_first_type類模板,也可以自己實現(xiàn)這樣的類模板。對第2個問題,與第1個問題不同的是,它是選擇函數(shù)而不是類型,本質(zhì)上就是選擇變量,因此需要實現(xiàn)一種基于類型或非類型參數(shù)選擇變量的模板,并且使用方式如下
select_variable<flag>(xxxA,xxxW)(arg1,arg2,...,argN)
flag是一個布爾非類型模板實參,當值為true時表示選擇返回xxxA,反之選擇返回xxxW,然后接下來跟著一系列參數(shù),表示調(diào)用對應(yīng)的A或W版本API,因此select_variable應(yīng)該實現(xiàn)為函數(shù)模板比較方便,若實現(xiàn)為類模板(關(guān)于實現(xiàn)可以參考boost中的function),則需要顯式指定函數(shù)返回值和參數(shù)類型,這樣一來,當函數(shù)參數(shù)過多,就是一個噩夢了,因為模板實參演繹不能用于類模板及其構(gòu)造函數(shù),只能應(yīng)用于其成員函數(shù)模板。
綜上分析解決,下面給出select_variable的實現(xiàn)與應(yīng)用。
select_variable實現(xiàn)
使用類模板特化與函數(shù)模板重載技術(shù)
1
#define TEMPLATE_BOOL_TRAIT_DEF1(trait,T,c)\
2
template<typename T>\
3
struct trait\
4

{\
5
static const bool value=c;\
6
};\
7
8
#define TEMPLATE_BOOL_TRAIT_SPEC1(trait,sp,c)\
9
template<>\
10
struct trait<sp>\
11

{\
12
static const bool value=c;\
13
};\
14
15
template<bool flag,typename T1,typename T2>
16
struct select_type;
17
18
template<typename T1,typename T2>
19
struct select_type<true,T1,T2>
20

{
21
typedef T1 type;
22
};
23
24
template<typename T1,typename T2>
25
struct select_type<false,T1,T2>
26

{
27
typedef T2 type;
28
};
29
30
template<bool flag,typename T1,typename T2>
31
inline typename select_type<flag,T1,T2>::type select_variable(T1 t1,T2 t2)
32

{
33
return select_variable_impl(typename select_type<flag,int,long>::type(),t1,t2);
34
}
35
36
template<typename T1,typename T2>
37
inline T1 select_variable_impl(int,T1 t1,T2 t2)
38

{
39
return t1;
40
}
41
42
template<typename T1,typename T2>
43
inline T2 select_variable_impl(long,T1 t1,T2 t2)
44

{
45
return t2;
46
}
47
48
TEMPLATE_BOOL_TRAIT_DEF1(is_ansi_char,T,false)
49
TEMPLATE_BOOL_TRAIT_SPEC1(is_ansi_char,char,true)
50
TEMPLATE_BOOL_TRAIT_SPEC1(is_ansi_char,char const,true)
51
TEMPLATE_BOOL_TRAIT_SPEC1(is_ansi_char,char volatile,true)
52
TEMPLATE_BOOL_TRAIT_SPEC1(is_ansi_char,char const volatile,true)
53
54
#undef TEMPLATE_BOOL_TRAIT_DEF1
55
#undef TEMPLATE_BOOL_TRAIT_SPEC1
select_variable應(yīng)用
有了select_variable,封裝設(shè)計API就方便多了,在函數(shù)模板中,類型的參數(shù)化體現(xiàn)在其參數(shù)、返回值和內(nèi)部實現(xiàn)三方面,下面就從這三方面來說明其應(yīng)用:
參數(shù)類型化
IsDirectoryOrFile根據(jù)路徑來判斷是否為目錄或文件,對于調(diào)用方來說,可以靈活指定A或W版本的字符串路徑。
1
template<typename charT>
2
inline int IsDirectoryOrFile(const charT* path)
3

{
4
DWORD dwFlag = select_variable<is_ansi_char<charT>::value>(GetFileAttributesA,GetFileAttributesW)(path);
5
if (INVALID_FILE_ATTRIBUTES == dwFlag)
6
return 0;
7
return (dwFlag & FILE_ATTRIBUTE_DIRECTORY) ? 1 : 2;
8
}
返回值類型化
GetExePath獲取當前應(yīng)用程序的路徑,對于調(diào)用方來說,可以靈活指定想要返回A或W版本字符串表示的路徑。
1
template<typename T>
2
inline std::basic_string<T> GetExePath()
3

{
4
T szExePath[MAX_PATH];
5
select_variable<is_ansi_char<T>::value>(GetModuleFileNameA,GetModuleFileNameW)(NULL,szExePath);
6
return szExePath;
7
}
內(nèi)部實現(xiàn)類型化
GetDirSize計算某一目錄或文件的大小,因內(nèi)部用到了FirstFirstFile和FirstNextFile,而這兩個API不僅路徑,而且WIN32_FIND_DATA結(jié)構(gòu)體都有A和W版本,因此需要選擇定義正確的結(jié)構(gòu)體變量和調(diào)用正確的API函數(shù)。
1
template<typename charT>
2
inline ULONGLONG GetDirSize(const charT* path, const volatile BOOL& bExitCalc)
3

{
4
int ret = IsDirectoryOrFile(path);
5
if (0==ret) return 0L;
6
7
std::basic_string<charT> strPath = path;
8
if (1==ret)
9
{
10
if (strPath.length() - 1 != strPath.rfind((charT)'\\'))
11
strPath += (charT)'\\';
12
strPath += select_variable<is_ansi_char<charT>::value>("*.*",L"*.*");
13
}
14
ULONGLONG ullSumSize = 0;
15
typename select_type<is_ansi_char<charT>::value,WIN32_FIND_DATAA,WIN32_FIND_DATAW>::type findData;
16
HANDLE hFindFile = select_variable<is_ansi_char<charT>::value>(FindFirstFileA,FindFirstFileW)(strPath.c_str(), &findData);
17
18
for(BOOL bResult = (hFindFile != INVALID_HANDLE_VALUE); bResult; bResult = select_variable<is_ansi_char<charT>::value>(FindNextFileA,FindNextFileW)(hFindFile, &findData))
19
{
20
if(findData.cFileName[0] == (charT)'.')
21
continue;
22
if(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
23
{
24
strPath = strPath.substr(0,strPath.rfind((charT)'\\')+1)+findData.cFileName;
25
ullSumSize += GetDirSize(strPath.c_str(), bExitCalc);
26
}
27
else
28
ullSumSize += (((ULONGLONG)findData.nFileSizeHigh) << 32) + findData.nFileSizeLow;
29
}
30
::FindClose(hFindFile);
31
return ullSumSize;
32
}
posted on 2011-12-24 19:08
春秋十二月 閱讀(2960)
評論(2) 編輯 收藏 引用 所屬分類:
C/C++