/*************************************************************************** * * Copyright (c) 2009 Baidu.com, Inc. All Rights Reserved * $Id: check_cast_bsl_string.h,v 1.2 2009/10/14 08:30:49 chenxm Exp $ * **************************************************************************/ /** * @file check_cast_bsl_string.h * @author chenxm(chenxiaoming@baidu.com) * @date 2009/09/24 14:18:05 * @version $Revision: 1.2 $ * @brief * * 特别感谢: * 本文件部分代码由QAD李伟刚同学贡献 * **/ #ifndef __CHECK_CAST_BSL_STRING_H_ #define __CHECK_CAST_BSL_STRING_H_ //internal use #ifndef __CHECK_CAST_H_ #error "this file cannot be included directly, please include \"check_cast.h\" instead" #endif #include "bsl/containers/string/bsl_string.h" namespace bsl{ //to bsl::string /** * @brief 将字符转换成bsl::string,转换结果为一个只含有一个字符的bsl::string * * @param [in/out] value : char * @return bsl::string * @retval * @see * @note * @author lwg * @date 2009/08/11 17:14:10 **/ template<> inline bsl::string check_cast(char value) { return bsl::string().appendf("%c", value); } /** * @brief 将8位整数(int8_t)转换成bsl::string,使用十进制 * * @param [in/out] value : unsigned char * @return bsl::string * @retval * @see * @note * @author chenxm * @date 2009/08/11 17:16:20 **/ template<> inline bsl::string check_cast(signed char value) { return bsl::string().appendf("%hhd", value); } /** * @brief 将8位整数(uint8_t)转换成bsl::string,使用十进制 * * @param [in/out] value : unsigned char * @return bsl::string * @retval * @see * @note * @author chenxm * @date 2009/08/11 17:16:20 **/ template<> inline bsl::string check_cast(unsigned char value) { return bsl::string().appendf("%hhu", value); } template<> inline bsl::string check_cast(short value) { return bsl::string().appendf("%hd", value); } template<> inline bsl::string check_cast(unsigned short value) { return bsl::string().appendf("%hu", value); } template<> inline bsl::string check_cast(int value) { return bsl::string().appendf("%d", value); } template<> inline bsl::string check_cast(unsigned int value) { return bsl::string().appendf("%u", value); } template<> inline bsl::string check_cast(long value) { return bsl::string().appendf("%ld", value); } template<> inline bsl::string check_cast(unsigned long value) { return bsl::string().appendf("%lu", value); } template<> inline bsl::string check_cast(long long value) { return bsl::string().appendf("%lld", value); } template<> inline bsl::string check_cast(unsigned long long value) { return bsl::string().appendf("%llu", value); } template<> inline bsl::string check_cast(float value) { return bsl::string().appendf("%g", value); } template<> inline bsl::string check_cast(double value) { return bsl::string().appendf("%lg", value); } template<> inline bsl::string check_cast(long double value) { return bsl::string().appendf("%Lg", value); } template<> inline bsl::string check_cast(char * value) { return value; } template<> inline bsl::string check_cast(const char * value) { return value; } //from bsl::string /* 可能会有效率问题 template<> inline char check_cast( bsl::string s ){ return s[0]; } template<> inline long check_cast ( bsl::string s ){ return check_cast(s.c_str()); } //signed char is used as int8 template<> inline signed char check_cast( bsl::string s ){ return check_cast( s.c_str() ); } //unsigned char is used as uint8 template<> inline unsigned char check_cast( bsl::string s ){ return check_cast( s.c_str() ); } template<> inline short check_cast ( bsl::string s ){ return check_cast( s.c_str() ); } template<> inline int check_cast ( bsl::string s ){ return check_cast( s.c_str() ); } template<> inline long long check_cast( bsl::string s ){ return check_cast( s.c_str() ); template<> inline unsigned long check_cast( bsl::string s ){ return check_cast( s.c_str() ); } template<> inline unsigned short check_cast( bsl::string s ){ return check_cast( s.c_str() ); } template<> inline unsigned int check_cast( bsl::string s ){ return check_cast( s.c_str() ); } template<> inline unsigned long long check_cast( bsl::string s ){ return check_cast( s.c_str() ); } template<> inline float check_cast( bsl::string s ){ return check_cast( s.c_str() ); } template<> inline double check_cast( bsl::string s ){ return check_cast( s.c_str() ); } template<> inline long double check_cast( bsl::string s ){ return check_cast( s.c_str() ); } template<> inline const char * check_cast( bsl::string s ){ return s.c_str(); } */ } #endif //__CHECK_CAST_BSL_STRING_H_ /* vim: set ts=4 sw=4 sts=4 tw=100 */