/*************************************************************************** * * Copyright (c) 2008 Baidu.com, Inc. All Rights Reserved * $Id: IVar.h,v 1.4 2010/04/28 12:45:33 scmpf Exp $ * **************************************************************************/ /** * @file IVar.h * @author chenxm(chenxiaoming@baidu.com) * @date 2008/09/24 01:29:15 * @version $Revision: 1.4 $ * @brief * **/ #ifndef __BSL_VAR_IVAR_H__ #define __BSL_VAR_IVAR_H__ #include "bsl/containers/string/bsl_string.h" #include "bsl/exception/bsl_exception.h" #include "bsl/ResourcePool.h" #include "bsl/var/ArrayIterator.h" #include "bsl/var/DictIterator.h" namespace bsl{ namespace var{ /** * @brief 为IVar支持无类型二进制数据而定义的结构体 * * 相当于头指针与长度的简单集合,析构时不会回收data所指内存。 */ struct raw_t{ /** * @brief 二进制数据头指针 * * */ const void *data; /** * @brief 二进制数据长度  * * */ size_t length; /** * @brief 默认构造函数 * * @see * @author chenxm * @date 2010/03/17 16:46:28 **/ raw_t() :data(NULL), length(0) {} /** * @brief 常用的构造函数 * * @param [in] data_ : const void* * @param [in] len : size_t * @see * @author chenxm * @date 2010/03/17 16:46:41 **/ raw_t( const void *data_, size_t len) :data(data_), length(len) {} }; //forward declaration class IVar{ public: /** * @brief 字符串类型 * * */ typedef bsl::string string_type; /** * @brief 字段名类型 * * */ typedef bsl::string field_type; /** * @brief 无类型二进制数据类型 * * */ typedef raw_t raw_type; /** * @brief 数组迭代器类型 * * */ typedef ArrayIterator array_iterator; /** * @brief 只读数组迭代器类型 * * */ typedef ArrayConstIterator array_const_iterator; /** * @brief 字典迭代器类型 * * */ typedef DictIterator dict_iterator; /** * @brief 只读字典迭代器类型 * * */ typedef DictConstIterator dict_const_iterator; /** * @brief 掩码类型 * * */ typedef unsigned int mask_type; // constant definition #ifdef PHP_COMLOG static const mask_type _IS_BOOL = 1 << 0; /**< 是否布尔类型 */ static const mask_type IS_NUMBER = 1 << 1; /**< 是否数值类型 */ static const mask_type _IS_STRING = 1 << 2; /**< 是否字符串类型 */ static const mask_type IS_RAW = 1 << 3; /**< 是否二进制类型 */ static const mask_type _IS_ARRAY = 1 << 4; /**< 是否数组类型 */ static const mask_type IS_DICT = 1 << 5; /**< 是否字典类型 */ static const mask_type IS_CALLABLE = 1 << 6; /**< 是否可调用类型 */ static const mask_type IS_OTHER = 1 << 7; /**< 是否其它类型 */ static const mask_type IS_REF = 1 << 8; /**< 是否引用类型 */ static const mask_type IS_MUTABLE = 1 << 9; /**< 是否可变类型 */ static const mask_type IS_FLOATING = 1 << 10; /**< 是否浮点类型 */ static const mask_type IS_SIGNED = 1 << 11; /**< 是否有符号类型 */ static const mask_type IS_ONE_BYTE = 1 << 12; /**< 是否单字节类型 */ static const mask_type IS_TWO_BYTE = 1 << 13; /**< 是否双字节类型 */ static const mask_type IS_FOUR_BYTE = 1 << 14; /**< 是否四字节类型 */ static const mask_type IS_EIGHT_BYTE = 1 << 15; /**< 是否八字节类型 */ static const mask_type IS_BIG_INT = 1 << 16; /**< 是否大整数类型 */ static const mask_type NONE_MASK = 0; /**< 空掩码 */ static const mask_type ALL_MASK = ~0; /**< 满掩码 */ #else static const mask_type IS_BOOL = 1 << 0; /**< 是否布尔类型 */ static const mask_type IS_NUMBER = 1 << 1; /**< 是否数值类型 */ static const mask_type IS_STRING = 1 << 2; /**< 是否字符串类型 */ static const mask_type IS_RAW = 1 << 3; /**< 是否二进制类型 */ static const mask_type IS_ARRAY = 1 << 4; /**< 是否数组类型 */ static const mask_type IS_DICT = 1 << 5; /**< 是否字典类型 */ static const mask_type IS_CALLABLE = 1 << 6; /**< 是否可调用类型 */ static const mask_type IS_OTHER = 1 << 7; /**< 是否其它类型 */ static const mask_type IS_REF = 1 << 8; /**< 是否引用类型 */ static const mask_type IS_MUTABLE = 1 << 9; /**< 是否可变类型 */ static const mask_type IS_FLOATING = 1 << 10; /**< 是否浮点类型 */ static const mask_type IS_SIGNED = 1 << 11; /**< 是否有符号类型 */ static const mask_type IS_ONE_BYTE = 1 << 12; /**< 是否单字节类型 */ static const mask_type IS_TWO_BYTE = 1 << 13; /**< 是否双字节类型 */ static const mask_type IS_FOUR_BYTE = 1 << 14; /**< 是否四字节类型 */ static const mask_type IS_EIGHT_BYTE = 1 << 15; /**< 是否八字节类型 */ static const mask_type IS_BIG_INT = 1 << 16; /**< 是否大整数类型 */ static const mask_type NONE_MASK = 0; /**< 空掩码 */ static const mask_type ALL_MASK = ~0; /**< 满掩码 */ #endif //methods for all /** * @brief 析构函数 * * @see * @author chenxm * @date 2009/02/04 18:49:09 **/ virtual ~IVar(){ } /** * @brief 赋值运算符 * * 所有IVar实现类都必须支持该方法。 * * @return IVar& operator * @retval * @see * @author chenxm * @date 2009/02/04 18:49:21 **/ virtual IVar& operator = ( IVar& ) = 0; /** * @brief 打印重要的内部状态,及其子IVar对象的状态 * * 所有IVar实现类都必须支持该方法。 * 该方法仅用于调试与跟踪,其内容应该容易被肉眼识别。其格式可能经常变化,不应对其内容进行监控。 * * 可选的verbose_level参数表示递归深度。0表示不递归子IVar对象,实现类应保证该函数算法复杂度为O(1);1表示递归所有直接子IVar对象,实现类应保证该函数算法复杂度为O(size()),余类推。 * * @return string_type * @retval * @see * @author chenxm * @date 2009/02/04 18:51:26 **/ virtual string_type dump(size_t verbose_level=0) const = 0; /** * @brief 清空函数 * * 所有IVar实现类都必须支持该方法。 * * @return void * @retval * @see * @author chenxm * @date 2009/02/04 18:51:06 **/ virtual void clear() = 0; /** * @brief 转化为字符串。 * * 所有IVar实现类都必须支持该方法。 * 自BSL 1.0.5后,该函数只用于转化为字符串,调试/跟踪应使用dump() * * @return string_type * @retval * @see * @author chenxm * @date 2009/02/04 18:51:26 **/ virtual string_type to_string() const = 0; /** * @brief 返回实现类型的字符串表示 * * 所有IVar实现类都必须支持该方法。 * * @return string_type * @retval * @see * @author chenxm * @date 2009/02/04 18:54:18 **/ virtual string_type get_type() const = 0; /** * @brief 返回实现类型的类型掩码 * * 所有IVar实现类都必须支持该方法。 * 目前返回值类型目前是unsigned short,以后可能会改变,但会保持与unsigned short兼容 * * @return mask_type * @retval * @see * @author chenxm * @date 2010/03/11 18:54:18 **/ virtual mask_type get_mask() const = 0; /** * @brief 复制一个var结点 * * @return IVar& * @retval * @see * @author chenxm * @date 2010/01/29 16:20:41 **/ virtual IVar& clone(bsl::ResourcePool& /*rp*/) const = 0; /** * @brief 复制一个var结点, 由is_deep_copy参数控制是否深复制 * * @return IVar& * @retval * @see * @author zhujianwei * @date 2011/03/22 **/ virtual IVar& clone(bsl::ResourcePool& /*rp*/, bool /*is_deep_copy*/) const{ throw bsl::NotImplementedException()< T * @retval * @see * @author chenxm * @date 2010/03/17 17:12:36 **/ template T to() const; /** * @brief 用无类型二进制数据类型赋值 * * 所有is_raw()返回true的实现类都必须实现该方法 * 若实现类不支持该操作,抛出bsl::InvalidOperationException异常 * * @return IVar& operator * @retval * @see * @author chenxm * @date 2009/05/03 14:32:19 **/ virtual IVar& operator = ( const raw_type& value_ ){ throw bsl::InvalidOperationException()<= size(),返回bsl::var::Null::null; * 一般用于试探性获取 * * @return const IVar& * @retval * @see * @author chenxm * @date 2009/02/04 19:12:42 **/ virtual const IVar& get( size_t idx ) const { throw bsl::InvalidOperationException()<= size(),则数组会自动增长到size() + 1 * * @return void * @retval * @see * @author chenxm * @date 2009/02/04 19:07:04 **/ virtual void set( size_t idx, IVar& value_ ){ throw bsl::InvalidOperationException()<= size(),抛出bsl::OutOfBoundException异常 * 一般用于"确定性获取"(获取不到直接抛异常) * 试验性支持:若index < 0,等价于size() + index * * @param [in] index : int * @return const IVar& [] * @retval * @see * @author chenxm * @date 2009/02/04 19:16:21 **/ virtual const IVar& operator []( int idx ) const { throw bsl::InvalidOperationException()<= size(),数组会自动增长到index + 1 * 一般用于设置下标绑定或者确定性获取(获取不到会导致数组自动增长) * 试验性支持:若index < 0,等价于size() + index * * @param [in] index : int * @return IVar& [] * @retval * @see * @author chenxm * @date 2009/02/04 19:23:03 **/ virtual IVar& operator []( int idx ){ throw bsl::InvalidOperationException()<的特化实现(该方法不能被改写) * * @return template<> inline bool * @retval * @see * @author chenxm * @date 2010/03/17 17:16:00 **/ template<> inline bool IVar::to() const{ return this->to_bool(); } /** * @brief IVar::to的特化实现(该方法不能被改写) * * @return template<> inline signed char IVar::to inline signed char IVar::to() const{ return this->to_int8(); } /** * @brief IVar::to的特化实现(该方法不能被改写) * * @return template<> inline unsigned char IVar::to inline unsigned char IVar::to() const{ return this->to_uint8(); } /** * @brief IVar::to的特化实现(该方法不能被改写) * * @return template<> inline signed short IVar::to inline signed short IVar::to() const{ return this->to_int16(); } /** * @brief IVar::to的特化实现(该方法不能被改写) * * @return template<> inline unsigned short IVar::to inline unsigned short IVar::to() const{ return this->to_uint16(); } /** * @brief IVar::to的特化实现(该方法不能被改写) * * @return template<> inline signed int IVar::to inline signed int IVar::to() const{ return this->to_int32(); } /** * @brief IVar::to的特化实现(该方法不能被改写) * * @return template<> inline unsigned int IVar::to inline unsigned int IVar::to() const{ return this->to_uint32(); } /** * @brief IVar::to的特化实现(该方法不能被改写) * * @return template<> inline signed long long IVar::to inline signed long long IVar::to() const{ return this->to_int64(); } /** * @brief IVar::to的特化实现(该方法不能被改写) * * @return template<> inline unsigned long long IVar::to inline unsigned long long IVar::to() const{ return this->to_uint64(); } /** * @brief IVar::to的特化实现(该方法不能被改写) * * @return template<> inline float * @retval * @see * @author chenxm * @date 2010/03/17 17:17:15 **/ template<> inline float IVar::to() const{ return this->to_float(); } /** * @brief IVar::to的特化实现(该方法不能被改写) * * @return template<> inline double * @retval * @see * @author chenxm * @date 2010/03/17 17:17:17 **/ template<> inline double IVar::to() const{ return this->to_double(); } /** * @brief IVar::to的特化实现(该方法不能被改写) * * @return template<> inline bsl::string * @retval * @see * @author chenxm * @date 2010/03/17 17:17:19 **/ template<> inline bsl::string IVar::to() const{ return this->to_string(); } /** * @brief IVar::to的特化实现(该方法不能被改写) * * @return template<> inline raw_t * @retval * @see * @author chenxm * @date 2010/03/17 17:17:21 **/ template<> inline raw_t IVar::to() const{ return this->to_raw(); } /** * @brief IVar::to的特化实现(该方法不能被改写) * * @return template<> inline const char* IVar::to inline const char * IVar::to() const{ return this->c_str(); } #if __WORDSIZE == 64 /** * @brief IVar::to的特化实现(该方法不能被改写) * * @return signed long * @retval * @see * @author chenxm * @date 2010/03/17 17:17:23 **/ template<> inline signed long IVar::to() const{ return this->to_int64(); } /** * @brief IVar::to的特化实现(该方法不能被改写) * * @return unsigned long * @retval * @see * @author chenxm * @date 2010/03/17 17:17:23 **/ template<> inline unsigned long IVar::to() const{ return this->to_uint64(); } #else /** * @brief IVar::to的特化实现(该方法不能被改写) * * @return signed long * @retval * @see * @author chenxm * @date 2010/03/17 17:17:23 **/ template<> inline signed long IVar::to() const{ return this->to_int32(); } /** * @brief IVar::to的特化实现(该方法不能被改写) * * @return unsigned long * @retval * @see * @author chenxm * @date 2010/03/17 17:17:23 **/ template<> inline unsigned long IVar::to() const{ return this->to_uint32(); } #endif }} // namespace bsl::var #endif //__BSL_VAR_IVAR_H__ /* vim: set ts=4 sw=4 sts=4 tw=100 */