数据库操作集

/********************************************************************
 author  :   Clark/陈泽丹
 created :   2014-4-1
 purpose :   缓冲器
*********************************************************************/
#pragma once


//数据库类型标记
struct MEMENTO_TRANS_TYPE
{
	//只有BYTE大小
	enum 
	{
	};
};


//缓冲器
class DisposableBuf
{
public:
	size_t GetUsedSize()
	{
		return m_nonius_pos;
	}
	BYTE* CreateNoniusMemento(){ return mp_buf; }
	void Clear()
	{ 
		memset(mp_source, 0, m_size);
		m_nonius_pos = 0; 
		mp_buf = mp_source;
	}

protected:
	DisposableBuf( BYTE* _p_buf, size_t _buf_size ):mp_buf(_p_buf), mp_source(_p_buf), m_nonius_pos(0), m_size(_buf_size){}
	virtual ~DisposableBuf(){ mp_buf = NULL; }
	BOOL CanGoto( size_t _piece )
	{
		if( (m_nonius_pos + _piece) <= m_size )
			return TRUE;
		return FALSE;
	}
	void Goto( size_t _piece )
	{
		mp_buf += _piece;
		m_nonius_pos += _piece;
	}

protected:
	BYTE			*mp_buf;

private:
	BYTE			*mp_source;
	size_t			m_nonius_pos;
	size_t			m_size;
};


//--入栈缓冲器
class PushBufHelper;
struct IWriteMemento
{
	virtual BOOL WriteMemento( PushBufHelper& _push_buf ) = NULL;
};
class PushBufHelper: public DisposableBuf
{
public:
	PushBufHelper( BYTE* _p_buf, size_t _buf_size ):DisposableBuf(_p_buf, _buf_size){}
	BOOL Push( BYTE* _p_send, size_t _piece )
	{
		if( NULL != _p_send && CanGoto(_piece) )
		{
			memcpy(mp_buf, _p_send, _piece);
			Goto(_piece);
			return TRUE;
		}
		return FALSE;
	}
	BOOL Push( IWriteMemento* _p_write )
	{
		if( NULL != _p_write )
		{
			return _p_write->WriteMemento( *this );
		}
		return FALSE;
	}
};


//--出栈缓冲器
class PopBufHelper;
struct IReadMemento
{
	virtual BOOL ReadMemento( PopBufHelper& _pop_buf ) = NULL;
};
class PopBufHelper: public DisposableBuf
{
public:
	PopBufHelper( BYTE* _p_buf, size_t _buf_size ):DisposableBuf(_p_buf, _buf_size){}
	BOOL Pop( BYTE* _p_recv, size_t _piece )
	{
		if( NULL != _p_recv && CanGoto(_piece) )
		{
			memcpy(_p_recv, mp_buf, _piece);
			Goto(_piece);
			return TRUE;
		}
		return FALSE;
	}
	BOOL Pop( IReadMemento* _p_read )
	{
		if( NULL != _p_read )
		{
			return _p_read->ReadMemento(*this);
		}
		return FALSE; 
	}
};


/********************************************************************
 author  :   Clark/陈泽丹
 created :   2014-4-1
 purpose :   数据存贮流程框架
*********************************************************************/
#pragma once
#include "BufHelper.h"
#include "MapMementoTrans.h"




//数据存贮流程框架
template< class _KEY_TYPE, class _VAL_TYPE >
class MapMemento: public IWriteMemento, public IReadMemento
{
public:
	typedef hash_map< _KEY_TYPE, _VAL_TYPE >				MAP_TYPE;
	MapMemento( MAP_TYPE& _map_data ):mp_map_data(&_map_data){}

protected:
	// 全部保存
	BOOL WriteMemento( PushBufHelper& _buf )
	{
		//已用过的长度
		const size_t USED_LEN = _buf.GetUsedSize();

		//写入类型标志,标志既用于识别类型也用于说明该内存是初始化过的(因为合服的问题,数据初始化值可能有异常)
		BYTE type_sign = MapMementoWriteTrans<_KEY_TYPE, _VAL_TYPE>::QueryInterfaceType();
		if( !_buf.Push( &type_sign, sizeof(type_sign) ) )
			return FALSE;

		//记录长度空间的游标位置
		BYTE *p_nonius = _buf.CreateNoniusMemento();
		WORD write_size = 0;
		if( !_buf.Push( (BYTE*)&write_size, sizeof(write_size) ) )
			return FALSE;
		
		//写值
		auto FailedInItemWrite = [&_buf]( pair< _KEY_TYPE const, _VAL_TYPE >& _item ) -> bool
		{
			if( MapMementoWriteTrans<_KEY_TYPE, _VAL_TYPE>::IsDefaultVal(_item.second) )
			{
				return false;
			}
			MapMementoWriteTrans<_KEY_TYPE, _VAL_TYPE> write_memento(_item);
			if( write_memento.WriteMemento( _buf ) )
			{
				return false;
			}
			else
			{
				char Error[1024];
				_snprintf_s( Error, sizeof(Error) - 1, "保存数据超过容纳空间" );
				TRACELN(Error);
				return true;
			}
		};
		MAP_TYPE::iterator it = find_if( mp_map_data->begin(), mp_map_data->end(), FailedInItemWrite );

		//填写长度
		write_size = _buf.GetUsedSize() - ( sizeof(type_sign) + sizeof(write_size) + USED_LEN );
		memcpy(p_nonius, (BYTE*)&write_size, sizeof(write_size));
		if( mp_map_data->end() != it )
			return FALSE;
		return TRUE;
	}

	// 全部加载
	BOOL ReadMemento( PopBufHelper& _buf )
	{
		//已用过的长度
		const size_t USED_LEN = _buf.GetUsedSize();

		//清空
		mp_map_data->clear();

		//判断标志
		BYTE type_sign = 0;
		if( !_buf.Pop( &type_sign, sizeof(type_sign) ) )
			return FALSE;
		if( MapMementoReadTrans<_KEY_TYPE, _VAL_TYPE>::QueryInterfaceType() != type_sign )
			return FALSE;

		//读取长度
		WORD context_len = 0;
		if( !_buf.Pop( (BYTE*)&context_len, sizeof(context_len) ) )
			return FALSE;

		//总长度
		size_t total_len = USED_LEN + sizeof(type_sign) + sizeof(context_len) + context_len;

		//读各项值
		while( total_len > _buf.GetUsedSize() )
		{
			pair<_KEY_TYPE, _VAL_TYPE> item;
			//创建缓冲区辅助器和类型转换器
			MapMementoReadTrans<_KEY_TYPE, _VAL_TYPE> read_memento(item);
			if( read_memento.ReadMemento(  _buf ) )
			{
				if( !MapMementoReadTrans<_KEY_TYPE, _VAL_TYPE>::IsDefaultVal(item.second) )
				{
					mp_map_data->insert( item );
				}
			}
			else
			{
				char Error[1024];
				_snprintf_s( Error, sizeof(Error) - 1, "读取数据超过供及空间" );
				TRACELN(Error);
				break;
			}
		};
		if( total_len > _buf.GetUsedSize() )
			return FALSE;
		return TRUE;
	}

private:
	MAP_TYPE*												mp_map_data;
};



template< class _KEY_TYPE, class _VAL_TYPE >
class UseDefaultMap
{
public:
	//设值
	void Set( _KEY_TYPE _key, _VAL_TYPE _val )
	{
		if( MapMementoWriteTrans<_KEY_TYPE, _VAL_TYPE>::IsDefaultVal(_val) )
		{
			m_data_map.erase( _key );
		}
		else
		{
			m_data_map[_key] = _val;
		}
	}

	//取值
	void Get( _KEY_TYPE _key, _VAL_TYPE& _val )
	{
		hash_map< _KEY_TYPE, _VAL_TYPE >::iterator it = m_data_map.find(_key);
		if( m_data_map.end() != it )
		{
			_val = m_data_map[_key];
		}
		else
		{
			MapMementoWriteTrans<_KEY_TYPE, _VAL_TYPE>::GetDefaultVal( _val );
		}
	}

	MapMemento<_KEY_TYPE, _VAL_TYPE>  CreateMemento()
	{
		MapMemento<_KEY_TYPE, _VAL_TYPE> ret(m_data_map);
		return ret;
	}

private:
	hash_map< _KEY_TYPE, _VAL_TYPE >				m_data_map;
};

/********************************************************************
 author  :   Clark/陈泽丹
 created :   2014-4-1
 purpose :   备忘器
*********************************************************************/
#pragma once
#include "BufHelper.h"
#include "string"





//--------------------------------------------------------------------------
//写数据
template< class _KEY_TYPE, class _VAL_TYPE >
struct MapMementoWriteTrans{};

//读数据
template< class _KEY_TYPE, class _VAL_TYPE >
struct MapMementoReadTrans{};


//--------------------------------------------------------------------------
//check
struct VbosVesion{};
struct TransByteCheckDBData
{
	static const BYTE QueryInterfaceType();
	static BOOL IsDefaultVal( const VbosVesion& _val );
	static void GetDefaultVal( VbosVesion& _val );
};
template<>
class MapMementoWriteTrans< BYTE, VbosVesion >: public TransByteCheckDBData, public IWriteMemento
{
public:
	MapMementoWriteTrans( const pair< BYTE const, VbosVesion >& _item );
	virtual BOOL WriteMemento( PushBufHelper& _dest_buf );
};
template<>
class MapMementoReadTrans< BYTE, VbosVesion >: public TransByteCheckDBData, public IReadMemento
{
public:
	MapMementoReadTrans( pair< BYTE, VbosVesion >& _item );
	virtual BOOL ReadMemento( PopBufHelper& _src_buf );
};



//--------------------------------------------------------------------------
//check
struct ItaGDataVesion{};
struct TransByteCheckItaGDBData
{
	static const BYTE QueryInterfaceType();
	static BOOL IsDefaultVal( const ItaGDataVesion& _val );
	static void GetDefaultVal( ItaGDataVesion& _val );
};
template<>
class MapMementoWriteTrans< BYTE, ItaGDataVesion >: public TransByteCheckItaGDBData, public IWriteMemento
{
public:
	MapMementoWriteTrans( const pair< BYTE const, ItaGDataVesion >& _item );
	virtual BOOL WriteMemento( PushBufHelper& _dest_buf );
};
template<>
class MapMementoReadTrans< BYTE, ItaGDataVesion >: public TransByteCheckItaGDBData, public IReadMemento
{
public:
	MapMementoReadTrans( pair< BYTE, ItaGDataVesion >& _item );
	virtual BOOL ReadMemento( PopBufHelper& _src_buf );
};


//--------------------------------------------------------------------------
//word存贮
struct TransWordWord
{
	static const BYTE QueryInterfaceType();
	static BOOL IsDefaultVal( const WORD& _val );
	static void GetDefaultVal( WORD& _val );
};
template<>
class MapMementoWriteTrans< WORD, WORD >: public TransWordWord, public IWriteMemento
{
public:
	MapMementoWriteTrans( const pair< WORD const, WORD >& _item ):mp_item(&_item){}
	virtual BOOL WriteMemento( PushBufHelper& _dest_buf );
private:
	const pair< WORD const, WORD >* mp_item;
};
template<>
class MapMementoReadTrans< WORD, WORD >: public TransWordWord, public IReadMemento
{
public:
	MapMementoReadTrans( pair< WORD, WORD >& _item ):mp_item(&_item){}
	virtual BOOL ReadMemento( PopBufHelper& _src_buf );
private:
	pair< WORD, WORD >* mp_item;
};

//--------------------------------------------------------------------------
//dword存贮
struct TransDWordWord
{
	static const BYTE QueryInterfaceType();
	static BOOL IsDefaultVal( const DWORD& _val );
	static void GetDefaultVal( DWORD& _val );
};
template<>
class MapMementoWriteTrans< WORD, DWORD >: public TransDWordWord, public IWriteMemento
{
public:
	MapMementoWriteTrans( const pair< WORD const, DWORD >& _item ):mp_item(&_item){}
	virtual BOOL WriteMemento( PushBufHelper& _dest_buf );
private:
	const pair< WORD const, DWORD >* mp_item;
};
template<>
class MapMementoReadTrans< WORD, DWORD >: public TransDWordWord, public IReadMemento
{
public:
	MapMementoReadTrans( pair< WORD, DWORD >& _item ):mp_item(&_item){}
	virtual BOOL ReadMemento( PopBufHelper& _src_buf );
private:
	pair< WORD, DWORD >* mp_item;
};

//--------------------------------------------------------------------------
//字符串存贮
struct DbStr
{
	DbStr( char* _p_txt = "" );
	DbStr& operator = (DbStr& _left);
	char txt[256];
};
struct TransWordDbStr
{
	static const BYTE QueryInterfaceType();
	static BOOL IsDefaultVal( const DbStr& _val );
	static void GetDefaultVal( DbStr& _val );
};
template<>
class MapMementoWriteTrans< WORD, DbStr >: public TransWordDbStr, public IWriteMemento
{
public:
	MapMementoWriteTrans( const pair< WORD const, DbStr >& _item ):mp_item(&_item){}
	virtual BOOL WriteMemento( PushBufHelper& _dest_buf );
private:
	const pair< WORD const, DbStr >* mp_item;
};
template<>
class MapMementoReadTrans< WORD, DbStr >: public TransWordDbStr, public IReadMemento
{
public:
	MapMementoReadTrans( pair< WORD, DbStr >& _item ):mp_item(&_item){}
	virtual BOOL ReadMemento( PopBufHelper& _src_buf );
private:
	pair< WORD, DbStr >* mp_item;
};

#include "stdafx.h"
#include "MapMementoTrans.h"



const BYTE TransByteCheckDBData::QueryInterfaceType()
{
	return MEMENTO_TRANS_TYPE::VBOS_SAVE_VERSION; //验证码
}
BOOL TransByteCheckDBData::IsDefaultVal( const VbosVesion& _val ){ return TRUE; }
void TransByteCheckDBData::GetDefaultVal( VbosVesion& _val ){}
MapMementoWriteTrans< BYTE, VbosVesion >::MapMementoWriteTrans( const pair< BYTE const, VbosVesion >& _item ){}
BOOL MapMementoWriteTrans< BYTE, VbosVesion >::WriteMemento( PushBufHelper& _dest_buf ){ return TRUE; }
MapMementoReadTrans< BYTE, VbosVesion >::MapMementoReadTrans( pair< BYTE, VbosVesion >& _item ){}
BOOL MapMementoReadTrans< BYTE, VbosVesion >::ReadMemento( PopBufHelper& _src_buf ){ return TRUE; }



const BYTE TransByteCheckItaGDBData::QueryInterfaceType()
{
	return MEMENTO_TRANS_TYPE::ITS_W_DB_VERSION; //验证码
}
BOOL TransByteCheckItaGDBData::IsDefaultVal( const ItaGDataVesion& _val ){ return TRUE; }
void TransByteCheckItaGDBData::GetDefaultVal( ItaGDataVesion& _val ){}
MapMementoWriteTrans< BYTE, ItaGDataVesion >::MapMementoWriteTrans( const pair< BYTE const, ItaGDataVesion >& _item ){}
BOOL MapMementoWriteTrans< BYTE, ItaGDataVesion >::WriteMemento( PushBufHelper& _dest_buf ){ return TRUE; }
MapMementoReadTrans< BYTE, ItaGDataVesion >::MapMementoReadTrans( pair< BYTE, ItaGDataVesion >& _item ){}
BOOL MapMementoReadTrans< BYTE, ItaGDataVesion >::ReadMemento( PopBufHelper& _src_buf ){ return TRUE; }

//--------------------------------------------------------------------------
//word型
const BYTE TransWordWord::QueryInterfaceType()
{
	return MEMENTO_TRANS_TYPE::VBOS_W_W_VERSION;
}
BOOL TransWordWord::IsDefaultVal( const WORD& _val )
{
	return ( 0 == _val );
}
void TransWordWord::GetDefaultVal( WORD& _val )
{
	_val = 0;
}
BOOL MapMementoWriteTrans< WORD, WORD >::WriteMemento( PushBufHelper& _dest_buf ) 
{
	//记录Key值
	if( !_dest_buf.Push( (BYTE*)&(mp_item->first), sizeof(mp_item->first) ) )
		return FALSE;
	//记录内容
	if( !_dest_buf.Push( (BYTE*)&(mp_item->second), sizeof(mp_item->second) ) )
		return FALSE;
	return TRUE;
}
BOOL MapMementoReadTrans< WORD, WORD >::ReadMemento( PopBufHelper& _src_buf )
{
	//读取Key值
	if( !_src_buf.Pop( (BYTE*)&(mp_item->first), sizeof(mp_item->first) ) )
		return FALSE;
	//读取Key值
	if( !_src_buf.Pop( (BYTE*)&(mp_item->second), sizeof(mp_item->second) ) )
		return FALSE;
	return TRUE;
}


//--------------------------------------------------------------------------
//dword型
const BYTE TransDWordWord::QueryInterfaceType()
{
	return MEMENTO_TRANS_TYPE::VBOS_W_DW_VERSION;
}
BOOL TransDWordWord::IsDefaultVal( const DWORD& _val )
{
	return ( 0 == _val );
}
void TransDWordWord::GetDefaultVal( DWORD& _val )
{
	_val = 0;
}
BOOL MapMementoWriteTrans< WORD, DWORD >::WriteMemento( PushBufHelper& _dest_buf ) 
{
	//记录Key值
	if( !_dest_buf.Push( (BYTE*)&(mp_item->first), sizeof(mp_item->first) ) )
		return FALSE;
	//记录内容
	if( !_dest_buf.Push( (BYTE*)&(mp_item->second), sizeof(mp_item->second) ) )
		return FALSE;
	return TRUE;
}
BOOL MapMementoReadTrans< WORD, DWORD >::ReadMemento( PopBufHelper& _src_buf )
{
	//读取Key值
	if( !_src_buf.Pop( (BYTE*)&(mp_item->first), sizeof(mp_item->first) ) )
		return FALSE;
	//读取Key值
	if( !_src_buf.Pop( (BYTE*)&(mp_item->second), sizeof(mp_item->second) ) )
		return FALSE;
	return TRUE;
}


//--------------------------------------------------------------------------
//字符串型
DbStr::DbStr( char* _p_txt )
{
	if( NULL == _p_txt )
	{
		strcpy( txt, "" );
	}
	else
	{
		size_t len = strlen(_p_txt);
		if( len >= sizeof(txt) )
		{
			strcpy( txt, "" );
		}
		else
		{
			memcpy( txt, _p_txt, len );
			txt[len] = '\0';
		}
	}
}
DbStr& DbStr::operator = (DbStr& _left)
{
	memcpy( txt, _left.txt, sizeof(txt)-1 );
	txt[sizeof(txt)-1] = '\0';
	return *this;
}

//---------------------------------------------------------------
const BYTE TransWordDbStr::QueryInterfaceType()
{
	return MEMENTO_TRANS_TYPE::VBOS_W_S_VERSION;
}
BOOL TransWordDbStr::IsDefaultVal( const DbStr& _val )
{
	if( 0 == strcmp(_val.txt, "") )
		return TRUE;
	else
		return FALSE;
}
void TransWordDbStr::GetDefaultVal( DbStr& _val )
{
	strcpy( _val.txt, "" );
}
BOOL MapMementoWriteTrans< WORD, DbStr >::WriteMemento( PushBufHelper& _dest_buf )
{
	//记录Key值
	if( !_dest_buf.Push( (BYTE*)&(mp_item->first), sizeof(mp_item->first) ) )
		return FALSE;
	//记录长度
	BYTE len = strlen(mp_item->second.txt);
	if( !_dest_buf.Push( (BYTE*)&len, sizeof(len) ) )
		return FALSE;
	//记录内容
	if( !_dest_buf.Push( (BYTE*)&(mp_item->second), len ) )
		return FALSE;
	return TRUE;
}
BOOL MapMementoReadTrans< WORD, DbStr >::ReadMemento( PopBufHelper& _src_buf )
{
	//读取Key值
	if( !_src_buf.Pop( (BYTE*)&(mp_item->first), sizeof(mp_item->first) ) )
		return FALSE;
	//读取长度
	BYTE len = 0;
	if( !_src_buf.Pop( (BYTE*)&len, sizeof(len) ) )
		return FALSE;
	//读取内容
	char txt[1024];
	if( !_src_buf.Pop( (BYTE*)&txt, len ) )
		return FALSE;
	if( len < 1024 )
		txt[len] = '\0';
	else
		txt[1023] = '\0';
	DbStr temp(txt);
	mp_item->second = temp;
	return TRUE;
}

/********************************************************************
 author  :   Clark/陈泽丹
 created :   2014-4-1
 purpose :   数据存贮流程框架
*********************************************************************/
#pragma once
#include "BufHelper.h"
#include "VctMementoTrans.h"



//数据存贮流程框架
template< class _VAL_TYPE >
class VctMemento: public IWriteMemento, public IReadMemento
{
public:
	VctMemento( vector< _VAL_TYPE >& _vct_data ):mp_vct_data(&_vct_data){}

protected:
	// 全部保存
	BOOL WriteMemento( PushBufHelper& _buf )
	{
		//已用过的长度
		const size_t USED_LEN = _buf.GetUsedSize();

		//写入类型标志,标志既用于识别类型也用于说明该内存是初始化过的(因为合服的问题,数据初始化值可能有异常)
		BYTE type_sign = VctMementoWriteTrans< _VAL_TYPE >::QueryInterfaceType();
		if( !_buf.Push( &type_sign, sizeof(type_sign) ) )
			return FALSE;

		//记录长度空间的游标位置
		BYTE *p_nonius = _buf.CreateNoniusMemento();
		WORD write_size = 0;
		if( !_buf.Push( (BYTE*)&write_size, sizeof(write_size) ) )
			return FALSE;

		//写值
		auto FailedIn_valWrite = [&_buf]( _VAL_TYPE& _val ) -> bool
		{
			if( VctMementoWriteTrans<_VAL_TYPE>::IsDefaultVal(_val) )
			{
				return false;
			}
			VctMementoWriteTrans<_VAL_TYPE> write_memento(_val);
			if( write_memento.WriteMemento( _buf ) )
			{
				return false;
			}
			else
			{
				char Error[1024];
				_snprintf_s( Error, sizeof(Error) - 1, "VctMementoWriteTrans 保存数据超过容纳空间 %d ", _buf.GetUsedSize() );
				TRACEERR(Error);
				return true;
			}
		};
		vector< _VAL_TYPE >::iterator it = find_if( mp_vct_data->begin(), mp_vct_data->end(), FailedIn_valWrite );

		//填写长度
		write_size = _buf.GetUsedSize() - ( sizeof(type_sign) + sizeof(write_size) + USED_LEN );
		memcpy(p_nonius, (BYTE*)&write_size, sizeof(write_size));
		if( mp_vct_data->end() != it )
			return FALSE;
		return TRUE;
	}

	// 全部加载
	BOOL ReadMemento( PopBufHelper& _buf )
	{
		//已用过的长度
		const size_t USED_LEN = _buf.GetUsedSize();

		//清空
		mp_vct_data->clear();

		//判断标志
		BYTE type_sign = 0;
		if( !_buf.Pop( &type_sign, sizeof(type_sign) ) )
			return FALSE;
		if( VctMementoReadTrans<_VAL_TYPE>::QueryInterfaceType() != type_sign )
			return FALSE;

		//读取长度
		WORD context_len = 0;
		if( !_buf.Pop( (BYTE*)&context_len, sizeof(context_len) ) )
			return FALSE;

		//总长度
		size_t total_len = USED_LEN + sizeof(type_sign) + sizeof(context_len) + context_len;

		//读各项值
		while( total_len > _buf.GetUsedSize() )
		{
			_VAL_TYPE _val;
			//创建缓冲区辅助器和类型转换器
			VctMementoReadTrans<_VAL_TYPE> read_memento(_val);
			if( read_memento.ReadMemento(_buf) )
			{
				if( !VctMementoReadTrans<_VAL_TYPE>::IsDefaultVal(_val) )
				{
					mp_vct_data->push_back(_val);
				}
			}
			else
			{
				char Error[1024];
				_snprintf_s( Error, sizeof(Error) - 1, "读取数据超过供及空间" );
				TRACEERR(Error);
				break;
			}
		};
		if( total_len > _buf.GetUsedSize() )
			return FALSE;
		return TRUE;
	}

private:
	vector< _VAL_TYPE >*	mp_vct_data;
};

/********************************************************************
 author  :   Clark/陈泽丹
 created :   2014-4-1
 purpose :   备忘器
*********************************************************************/
#pragma once
#include "BufHelper.h"



//--------------------------------------------------------------------------
//写数据
template< class _VAL_TYPE >
class VctMementoWriteTrans
{
public:
	VctMementoWriteTrans( _VAL_TYPE& _item ):mp_item(&_item){}
	static const BYTE QueryInterfaceType()
	{
		return _VAL_TYPE::QueryInterfaceType();
	}
	static BOOL IsDefaultVal( const _VAL_TYPE& _val )
	{
		return _VAL_TYPE::IsDefaultVal(_val);
	}
	static void GetDefaultVal( _VAL_TYPE& _val )
	{
		return _VAL_TYPE::GetDefaultVal(_val);
	}
	virtual BOOL WriteMemento( PushBufHelper& _dest_buf )
	{
		IWriteMemento *p_write = static_cast<IWriteMemento*>(mp_item);
		return p_write->WriteMemento(_dest_buf);
	}

private:
	_VAL_TYPE* mp_item;
};

//读数据
template< class _VAL_TYPE >
class VctMementoReadTrans
{
public:
	VctMementoReadTrans( _VAL_TYPE& _item ):mp_item(&_item){}
	static const BYTE QueryInterfaceType()
	{
		return _VAL_TYPE::QueryInterfaceType();
	}
	static BOOL IsDefaultVal( const _VAL_TYPE& _val )
	{
		return _VAL_TYPE::IsDefaultVal(_val);
	}
	static void GetDefaultVal( _VAL_TYPE& _val )
	{
		return _VAL_TYPE::GetDefaultVal(_val);
	}
	virtual BOOL ReadMemento( PopBufHelper& _dest_buf )
	{
		IReadMemento *p_read = static_cast<IReadMemento*>(mp_item);
		return p_read->ReadMemento(_dest_buf);
	}

private:
	_VAL_TYPE* mp_item;
};



//--------------------------------------------------------------------------
//check
struct RuneVesion{};
struct TransVctByteCheckDBData
{
	static const BYTE QueryInterfaceType();
	static BOOL IsDefaultVal( const RuneVesion& _val );
	static void GetDefaultVal( RuneVesion& _val );
};
template<>
class VctMementoWriteTrans< RuneVesion >: public TransVctByteCheckDBData, public IWriteMemento
{
public:
	VctMementoWriteTrans( const RuneVesion& _item ){}
	virtual BOOL WriteMemento( PushBufHelper& _dest_buf );
};
template<>
class VctMementoReadTrans< RuneVesion >: public TransVctByteCheckDBData, public IReadMemento
{
public:
	VctMementoReadTrans( RuneVesion& _item ){}
	virtual BOOL ReadMemento( PopBufHelper& _src_buf );
};


//OfflinePrize
struct OfflinePrize
{
	OfflinePrize():prize_index(0){}
	WORD prize_index;
};
struct TransVctOfflinePrize
{
	static const BYTE QueryInterfaceType();
	static BOOL IsDefaultVal( const OfflinePrize& _val );
	static void GetDefaultVal( OfflinePrize& _val );
};
template<>
class VctMementoWriteTrans< OfflinePrize >: public TransVctOfflinePrize, public IWriteMemento
{
public:
	VctMementoWriteTrans( const OfflinePrize& _item ):m_item(_item){}
	virtual BOOL WriteMemento( PushBufHelper& _dest_buf );
private:
	OfflinePrize m_item;
};
template<>
class VctMementoReadTrans< OfflinePrize >: public TransVctOfflinePrize, public IReadMemento
{
public:
	VctMementoReadTrans( OfflinePrize& _item ):m_item(&_item){}
	virtual BOOL ReadMemento( PopBufHelper& _src_buf );
private:
	OfflinePrize* m_item;
};

#include "stdafx.h"
#include "VctMementoTrans.h"




const BYTE TransVctByteCheckDBData::QueryInterfaceType()
{
	return MEMENTO_TRANS_TYPE::RUNE_PART_VERSION; //验证码
}
BOOL TransVctByteCheckDBData::IsDefaultVal( const RuneVesion& _val ){ return TRUE; }
void TransVctByteCheckDBData::GetDefaultVal( RuneVesion& _val ){}
BOOL VctMementoWriteTrans< RuneVesion >::WriteMemento( PushBufHelper& _dest_buf ){ return TRUE; }
BOOL VctMementoReadTrans< RuneVesion >::ReadMemento( PopBufHelper& _src_buf ){ return TRUE; }





const BYTE TransVctOfflinePrize::QueryInterfaceType()
{
	return MEMENTO_TRANS_TYPE::VBOS_OLP_VERSION;
}
BOOL TransVctOfflinePrize::IsDefaultVal( const OfflinePrize& _val )
{
	return FALSE;
}
void TransVctOfflinePrize::GetDefaultVal( OfflinePrize& _val )
{
	_val.prize_index = 0;
}
BOOL VctMementoWriteTrans< OfflinePrize >::WriteMemento( PushBufHelper& _dest_buf )
{ 
	_dest_buf.Push( (BYTE*)&(m_item.prize_index), sizeof(m_item.prize_index) );
	return TRUE; 
}
BOOL VctMementoReadTrans< OfflinePrize >::ReadMemento( PopBufHelper& _src_buf )
{ 
	_src_buf.Pop( (BYTE*)&(m_item->prize_index), sizeof(m_item->prize_index) );
	return TRUE; 
}


你可能感兴趣的:(数据库操作集)