IBinaryDeserializer.h 2.3 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
/***************************************************************************
 * 
 * Copyright (c) 2008 Baidu.com, Inc. All Rights Reserved
 * $Id: IBinaryDeserializer.h,v 1.3 2009/06/15 06:29:05 chenxm Exp $ 
 * 
 **************************************************************************/
 
 
 
/**
 * @file IBinaryDeserializer.h
 * @author duchuanying(duchuanying@baidu.com)
 * @date 2008/11/01 21:37:08
 * @version $Revision: 1.3 $ 
 * @brief mcpack -> IVar
 *  
 **/


#ifndef  __BSL_VAR_IBINARY_DESERIALIZER_H__
#define  __BSL_VAR_IBINARY_DESERIALIZER_H__

#include <bsl/exception.h>
#include <bsl/var/IVar.h>
#include <bsl/var/Null.h>

namespace bsl{
namespace var{

class IBinaryDeserializer{
public:
    virtual ~IBinaryDeserializer(){}

    /**
     * @brief 反序列化buffer为Var对象
	 *
	 * 默认实现,如无特殊需要,子类不必重写该函数。
     *
     * @param [in] buf   : const void*
     * @param [in] max_size   : size_t
     * @return  IVar& 
     * @retval   
     * @see 
     * @author chenxm
     * @date 2010/12/02 16:58:09
    **/
	virtual IVar& deserialize(const void* buf, size_t max_size){
		size_t res = try_deserialize( buf, max_size );
		if ( res > max_size ){
			throw bsl::OutOfBoundException()<<BSL_EARG<<"buffer too small! max_size["<<max_size<<"] expect[>="<<res<<"]";
		}
		return get_result();
	}
	
	/* @brief 尝试反序列化,不保证成功
	 * 
	 * [NOTICE] 提供默认实现兼容老版本bslext,子类应该重写该接口。
	 * 
	 * @param [in] buf   : const void*
     * @param [in] max_size   : size_t
     * @return  IVar& 
     * @retval   
     * @see 
     * @author zhujianwei
     * @date 2011/03/21 16:58:09
	 */
	virtual size_t try_deserialize( const void* /*buf*/, size_t /*max_size*/){
		throw bsl::NotImplementedException()<<BSL_EARG<<"try_deserialize interface has not been implemented.";
	}

	/**
	 * @brief 获取反串行化的结果,该函数应在deserialize()或try_deserialize()成功执行后调用。否则应返回Null对象的引用
	 *
	 * [NOTICE] 提供默认实现兼容老版本bslext,子类应该重写该接口。
	 * 
	 * @return  IVar& 
	 * @retval   
	 * @see 
	 * @author zhujianwei
	 * @date 2011/03/21 17:22:30
	**/
	virtual IVar& get_result() const{
		throw bsl::NotImplementedException()<<BSL_EARG<<"get_result interface has not been implemented.";
	}
};
}}   //namespace bsl::var


#endif  //__BSL_VAR_IBINARY_DESERIALIZER_H__

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