check_cast_bsl_string.h 6.7 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
/***************************************************************************
 * 
 * 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<bsl::string, char>(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<bsl::string, signed char>(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<bsl::string, unsigned char>(unsigned char value)
        {
            return bsl::string().appendf("%hhu", value);
        }

    template<>
        inline bsl::string check_cast<bsl::string, short>(short value)
        {
            return bsl::string().appendf("%hd", value);
        }

    template<>
        inline bsl::string check_cast<bsl::string, unsigned short>(unsigned short value)
        {
            return bsl::string().appendf("%hu", value);
        }

    template<>
        inline bsl::string check_cast<bsl::string, int>(int value)
        {
            return bsl::string().appendf("%d", value);
        }

    template<>
        inline bsl::string check_cast<bsl::string, unsigned int>(unsigned int value)
        {
            return bsl::string().appendf("%u", value);
        }

    template<>
        inline bsl::string check_cast<bsl::string, long>(long value)
        {
            return bsl::string().appendf("%ld", value);
        }

    template<>
        inline bsl::string check_cast<bsl::string, unsigned long>(unsigned long value)
        {
            return bsl::string().appendf("%lu", value);
        }

    template<>
        inline bsl::string check_cast<bsl::string, long long>(long long value)
        {
            return bsl::string().appendf("%lld", value);
        }

    template<>
        inline bsl::string check_cast<bsl::string, unsigned long long>(unsigned long long value)
        {
            return bsl::string().appendf("%llu", value);
        }

    template<>
        inline bsl::string check_cast<bsl::string, float>(float value)
        {
            return bsl::string().appendf("%g", value);
        }

    template<>
        inline bsl::string check_cast<bsl::string, double>(double value)
        {
            return bsl::string().appendf("%lg", value);
        }

    template<>
        inline bsl::string check_cast<bsl::string, long double>(long double value)
        {
            return bsl::string().appendf("%Lg", value);
        }

    template<>
        inline bsl::string check_cast<bsl::string, char *>(char * value)
        {
            return value;
        }

    template<>
        inline bsl::string check_cast<bsl::string, const char *>(const char * value)
        {
            return value;
        }

    //from bsl::string
    /* 可能会有效率问题
    template<>
        inline char check_cast<char,bsl::string>( bsl::string s ){
            return s[0];
        }

    template<>
        inline long check_cast<long, bsl::string> ( bsl::string s ){
            return check_cast<long>(s.c_str());
        }

    //signed char is used as int8
    template<>
        inline signed char check_cast<signed char,bsl::string>( bsl::string s ){
            return check_cast<signed char>( s.c_str() );
        }

    //unsigned char is used as uint8
    template<>
        inline unsigned char check_cast<unsigned char,bsl::string>( bsl::string s ){
            return check_cast<unsigned char>( s.c_str() );
        }

    template<>
        inline short check_cast<short, bsl::string> ( bsl::string s ){
            return check_cast<short>( s.c_str() );
        }

    template<>
        inline int check_cast<int, bsl::string> ( bsl::string s ){
            return check_cast<int>( s.c_str() );
        }

    template<>
        inline long long check_cast<long long, bsl::string>( bsl::string s ){
            return check_cast<long long>( s.c_str() );

    template<>
        inline unsigned long check_cast<unsigned long, bsl::string>( bsl::string s ){
            return check_cast<unsigned long>( s.c_str() );
        }

    template<>
        inline unsigned short check_cast<unsigned short, bsl::string>( bsl::string s ){
            return check_cast<unsigned short>( s.c_str() );
        }

    template<>
        inline unsigned int check_cast<unsigned int, bsl::string>( bsl::string s ){
            return check_cast<unsigned int>( s.c_str() );
        }

    template<>
        inline unsigned long long check_cast<unsigned long long, bsl::string>( bsl::string s ){
            return check_cast<unsigned long long>( s.c_str() );
        }

    template<>
        inline float check_cast<float, bsl::string>( bsl::string s ){
            return check_cast<float>( s.c_str() );
        }

    template<>
        inline double check_cast<double, bsl::string>( bsl::string s ){
            return check_cast<double>( s.c_str() );
        }

    template<>
        inline long double check_cast<long double, bsl::string>( bsl::string s ){
            return check_cast<long double>( s.c_str() );
        }

    template<>
        inline const char * check_cast<const char *, bsl::string>( bsl::string s ){
            return s.c_str();
        }
    */
}


#endif  //__CHECK_CAST_BSL_STRING_H_

/* vim: set ts=4 sw=4 sts=4 tw=100 */