/*************************************************************************** * * Copyright (c) 2008 Baidu.com, Inc. All Rights Reserved * $Id: Double.h,v 1.4 2010/04/28 12:45:33 scmpf Exp $ * **************************************************************************/ /** * @file Double.h * @author chenxm(chenxiaoming@baidu.com) * @date 2008/09/24 01:32:42 * @version $Revision: 1.4 $ * @brief * **/ #ifndef __BSL_VAR_DOUBLE_H__ #define __BSL_VAR_DOUBLE_H__ #include "bsl/var/IVar.h" #include "bsl/var/Ref.h" #include "bsl/check_cast.h" namespace bsl{ namespace var{ class Double: public IVar{ public: typedef IVar::string_type string_type; typedef IVar::field_type field_type; public: //special methods Double( double __value = 0 ) :_value(__value){} Double( const Double& other ) :IVar(other), _value( other._value ) {} Double& operator = ( const Double& other ){ _value = other._value; return *this; } //methods for all virtual Double& operator = ( IVar& var ) { try{ _value = var.to_double(); //throw }catch(bsl::Exception& e){ e<<"{bsl::var::Int32::operator =("<(_value); } /** * @brief 转化为8位无符号整数类型(所有is_number()返回真的类型都支持) * * @return unsigned char * @retval * @see * @author chenxm * @date 2010/03/17 18:09:14 **/ virtual unsigned char to_uint8() const { return check_cast(_value); } /** * @brief 转化为16位有符号整数类型(所有is_number()返回真的类型都支持) * * @return signed short * @retval * @see * @author chenxm * @date 2010/03/17 18:09:14 **/ virtual signed short to_int16() const { return check_cast(_value); } /** * @brief 转化为16位无符号整数类型(所有is_number()返回真的类型都支持) * * @return unsigned short * @retval * @see * @author chenxm * @date 2010/03/17 18:09:14 **/ virtual unsigned short to_uint16() const { return check_cast(_value); } /** * @brief 转化为32位有符号整数类型(所有is_number()返回真的类型都支持) * * @return signed int * @retval * @see * @author chenxm * @date 2010/03/17 18:09:14 **/ virtual signed int to_int32() const { return check_cast(_value); } /** * @brief 转化为32位无符号整数类型(所有is_number()返回真的类型都支持) * * @return unsigned int * @retval * @see * @author chenxm * @date 2010/03/17 18:09:14 **/ virtual unsigned int to_uint32() const { return check_cast(_value); } /** * @brief 转化为64位有符号整数类型(所有is_number()返回真的类型都支持) * * @return signed long long * @retval * @see * @author chenxm * @date 2010/03/17 18:09:14 **/ virtual signed long long to_int64() const { return check_cast(_value); } /** * @brief 转化为64位无符号整数类型(所有is_number()返回真的类型都支持) * * @return unsigned long long * @retval * @see * @author chenxm * @date 2010/03/17 18:09:14 **/ virtual unsigned long long to_uint64() const { return check_cast(_value); } /** * @brief 转化为单精度浮点数类型(所有is_number()返回真的类型都支持) * * @return float * @retval * @see * @author chenxm * @date 2010/03/17 18:09:14 **/ virtual float to_float() const { return float(_value); } /** * @brief 转化为双精度浮点数类型(所有is_number()返回真的类型都支持) * * @return double * @retval * @see * @author chenxm * @date 2010/03/17 18:09:14 **/ virtual double to_double() const { return _value; } virtual Double& operator = ( bool b ){ _value = b; return *this; } virtual Double& operator = ( int i ){ _value = check_cast(i); return *this; } virtual Double& operator = ( long long ll ){ _value = check_cast(ll); return *this; } /** * @brief 使用float类型赋值 * * @param [in] val : float * @return Double& * @retval * @see * @author chenxm * @date 2010/03/17 19:23:16 **/ virtual Double& operator = ( float f ){ _value = f; return *this; } /** * @brief 使用double类型赋值 * * @param [in] val : double * @return Double& * @retval * @see * @author chenxm * @date 2010/03/17 19:23:16 **/ virtual Double& operator = ( double d ){ _value = d; return *this; } /** * @brief 使用const char *类型赋值 * * @param [in] val : const char * * @return Double& * @retval * @see * @author chenxm * @date 2010/03/17 19:23:16 **/ virtual Double& operator = ( const char * cstr ){ _value = check_cast(cstr); return *this; } /** * @brief 使用字符串类型赋值 * * @param [in] val : const string_type& * @return Double& * @retval * @see * @author chenxm * @date 2010/03/17 19:23:16 **/ virtual Double& operator = ( const string_type& str ){ _value = check_cast(str.c_str()); return *this; } //use default version for bool、raw using IVar::operator =; //testers virtual bool is_number() const { return true; } virtual bool is_double() const { return true; } private: double _value; }; }} //namespace bsl::var #endif //__BSL_VAR_DOUBLE_H__ /* vim: set ts=4 sw=4 sts=4 tw=100 */