正确地调用字符串相关的函数(第一部分)

发表于:2007-07-01来源:作者:点击数: 标签:
即便在常规的程序 开发 过程中, 程序员 也常常会碰到调用字符串函数方面的困难。注意,这里的字符串函数是指有关字符指针操作相关的函数。例如,我们现有这么一个函数:reverse_string: templatelt; class CharTypegt; void reverse_string( const CharTyp


即便在常规的程序开发过程中,程序员也常常会碰到调用字符串函数方面的困难。注意,这里的字符串函数是指有关字符指针操作相关的函数。例如,我们现有这么一个函数:reverse_string:

template< class CharType>
void reverse_string( const CharType * strDest)
{
int nLength = /* what should we call here? */;
int idxFirst = 0;
int idxLast = nLength - 1;
while ( idxFirst < idxLast)
{
std::swap( strDest[ idxFirst], strDest[ idxLast];
++ idxFirst;
-- idxLast;
)
}
在/* what should we call here? */位置,我们使用strlen来调用const char *字符串,使用wcslen来调用const wchar_t *字符串。



解决方法是建立一个t_strlen函数,如下所示:

template< class CharType>
int t_strlen( const CharType * str)
{
return strlen( str);
}

int t_strlen( const wchar_t * str)
{
return wcslen( str);
}
开始看上面代码时,可能会觉得很繁琐,因为我们必需把相关的标号(int strlen( const char *和int wcslen( const char *))与每一个函数相匹配。

这里提供了一种调用字符串函数的简单方法。这一方法很通用,它适用于几个字符串函数操作,特别是它们共用或返回相同参数的时候。例如:

适用于strlen/wcslen.
适用于 strcpy/wcscpy.
适用于 strdup/wcsdup.
假设有两个字符串函数,你将定义一个宏,如:

#define t_functionname( CharType, parameters)
string_function_<noofparameters>params< CharType, return_type>(
function_for_char, function_for_wchar_t, parameters)

// Examples

// strlen, wcslen pair
// they each have one parameter, and return an ’int’
#define t_strlen( CharType, strVal)
string_function_1param< CharType, int>( strlen, wcslen, strVal)

// strcpy, wcscpy
// they each have two parameters, and return a pointer to a string
#define t_strcpy( CharType, param1, param2)
string_function_2params< CharType, CharType*>( strcpy, wcscpy, param1, param2)

// strncpy, wcsncpy
// they each have three parameters, and return a pointer to a string
#define t_strncpy( CharType, param1, param2, param3)
string_function_3params< CharType, CharType*>( strncpy, wcsncpy, param1, param2, param3)
定义这一宏以后,在程序中调用它就会非常地简单,如: 

template< class CharType>
void reverse_string( const CharType * strDest)
{
int nLength = t_strlen( CharType, strDest);
// . . . same as before . . .
}
下面的代码的功能是正确地调用字符串相关的函数。下一部分,我们将会讨论string_function_xparams 的用途,然后为你提供一系列的宏,你可以在<string.h>头文件中调用这些宏。

/////////////////////////////////////////////////////////
// one parameter

template&lt; class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1&gt;
ReturnType string_function_1param_helper( CharFunction funcChar, WchartFunction funcWchart, Param1 param1, const wchar_t *)
{
return funcWchart( param1);
}

template&lt; class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1&gt;
ReturnType string_function_1param_helper( CharFunction funcChar, WchartFunction funcWchart, Param1 param1, ...)
{
return funcChar( param1);
}


template&lt; class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1&gt;
ReturnType string_function_1param( CharFunction funcChar, WchartFunction funcWchart, Param1 param1)
{
return string_function_1param_helper&lt; CharType, ReturnType, CharFunction, WchartFunction, Param1&gt;(
funcChar, funcWchart, param1, ( const CharType *)NULL);


/////////////////////////////////////////////////////////
// two parameters

template&lt; class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2&gt;
ReturnType string_function_2params_helper(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2, const wchar_t *)
{
return funcWchart( param1, param2);
}

template&lt; class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2&gt;
ReturnType string_function_2params_helper(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2, ...)
{
return funcChar( param1, param2);
}


template&lt; class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2&gt;
ReturnType string_function_2params(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2)
{
return string_function_2params_helper&lt; CharType, ReturnType, CharFunction, WchartFunction, Param1&gt;(
funcChar, funcWchart, param1, param2, ( const CharType *)NULL);
}



/////////////////////////////////////////////////////////
// three parameters

template< class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2, class Param3>
ReturnType string_function_3params_helper(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2,
Param3 param3, const wchar_t *)
{
return funcWchart( param1, param2, param3);
}

template< class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2, class Param3>
ReturnType string_function_3params_helper(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2,
Param3 param3, ...)
{
return funcChar( param1, param2, param3);
}


template< class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2, class Param3>
ReturnType string_function_3params(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2,
Param3 param3)
{
return string_function_3params_helper< CharType, ReturnType, CharFunction, WchartFunction, Param1>(
funcChar, funcWchart, param1, param2, param3, ( const CharType *)NULL);
}

/////////////////////////////////////////////////////////
// four parameters

template< class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2, class Param3, class Param4>
ReturnType string_function_4params_helper(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2,
Param3 param3,
Param4 param4, const wchar_t *)
{
return funcWchart( param1, param2, param3, param4);
}

template< class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2, class Param3, class Param4>
ReturnType string_function_4params_helper(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2,
Param3 param3,
Param4 param4, ...)
{
return funcChar( param1, param2, param3, param4);
}


template< class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2, class Param3, class Param4>
ReturnType string_function_4params(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2,
Param3 param3,
Param4 param4)
{
return string_function_4params_helper< CharType, ReturnType, CharFunction, WchartFunction, Param1>(
funcChar, funcWchart, param1, param2, param3, param4, ( const CharType *)NULL);
}

/////////////////////////////////////////////////////////
// five parameters

template< class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2, class Param3, class Param4, class Param5>
ReturnType string_function_5params_helper(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2,
Param3 param3,
Param4 param4,
Param5 param5, const wchar_t *)
{
return funcWchart( param1, param2, param3, param4, param5);
}

template< class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2, class Param3, class Param4, class Param5>
ReturnType string_function_5params_helper(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2,
Param3 param3,
Param4 param4,
Param5 param5, ...)
{
return funcChar( param1, param2, param3, param4, param5);
}


template< class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2, class Param3, class Param4, class Param5>
ReturnType string_function_5params(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2,
Param3 param3,
Param4 param4,
Param5 param5)
{
return string_function_5params_helper< CharType, ReturnType, CharFunction, WchartFunction, Param1>(
funcChar, funcWchart, param1, param2, param3, param4, param5, ( const CharType *)NULL);
}


原文转自:http://www.ltesting.net