类成员函数模板特化 默认参数

 

#ifndef MEMORYBLOCK_H
#define MEMORYBLOCK_H

#include "odbclib.h"
#include <cstdlib>

using std::min;

namespace odbclib
{
	class MemoryBlock
	{
	public:
		typedef unsigned char byte;
		explicit MemoryBlock(size_t = 0xff) throw(runtime_error);
		MemoryBlock(MemoryBlock const&) throw(runtime_error);
		virtual ~MemoryBlock();
		MemoryBlock& initBlock(byte);
		void const* getRawBlock() const;
		void* getRawBlock();
		void writeRawBlock(void const*,size_t);
		size_t getSize()const;
		MemoryBlock& setSize(size_t,bool = true) throw(runtime_error);
		template<typename T>
		T getValue(size_t offset = 0) const
		{
			//TODO:may cross memory boundary
			T *p = (T *)((byte*)m_buffer + offset);
			return *p;
		}		
		template<typename T>
		MemoryBlock& setValue(T const& t,size_t offset = 0)
		{
			memcpy((byte*)m_buffer + offset,(void const*)&t,min(sizeof(T),m_size));
			return *this;
		}
		template<typename T,size_t N>
		MemoryBlock& setValue(T const (&t)[N],size_t offset = 0)
		{			
			T * p = (T *)((byte*)m_buffer + offset);
			T * end = (T *)((byte*)m_buffer + m_size);
			int i = 0;
			while(end - p > 0)
				memcpy(p++,&t[i++],sizeof(T));
			return *this;
		}
	private:
		void * m_buffer;
		size_t m_size;
	};
	template<> string MemoryBlock::getValue(size_t)const;
	template<> MemoryBlock& MemoryBlock::setValue<string>(string const&,size_t);
}

#endif

 

 

#include "odbclib.h"

namespace odbclib
{
	MemoryBlock::MemoryBlock(size_t sz) throw(runtime_error)
		:m_buffer(0),
		m_size(0)
	{
		m_buffer = (byte*)::malloc(sz);
		if(!m_buffer)
			throw runtime_error("malloc failed!");
		m_size = sz;
	}

	MemoryBlock::MemoryBlock(MemoryBlock const& other) throw(runtime_error)
		:m_buffer(0),
		m_size(0)
	{
		m_buffer = (byte*)::malloc(other.m_size);
		if(!m_buffer)
			throw runtime_error("malloc failed!");
		m_size = other.m_size;
		memcpy(m_buffer,(void*)other.m_buffer,m_size);
	}

	MemoryBlock::~MemoryBlock()
	{
		::free(m_buffer);
		m_buffer = (void*)0;
		m_size = 0u;
	}

	void const* MemoryBlock::getRawBlock() const{return (void*)m_buffer;}

	void* MemoryBlock::getRawBlock(){return (void*)m_buffer;}

	void MemoryBlock::writeRawBlock(void const* mem,size_t memsz)
	{
		memcpy(m_buffer,mem,min(memsz,m_size));
	}

	size_t MemoryBlock::getSize()const{return m_size;}

	MemoryBlock& MemoryBlock::setSize(size_t sz,bool preserve) throw(runtime_error)
	{			
		byte* newAddr =	(byte*)::realloc(m_buffer,sz);
		if(!newAddr)
			throw runtime_error("realloc failed!");
		if((void*)newAddr != m_buffer && preserve)
			memcpy(newAddr,m_buffer,sz < m_size ? sz : m_size);		
		m_buffer = newAddr;
		m_size = sz;
		return *this;
	}

	MemoryBlock& MemoryBlock::initBlock(byte data)
	{
		memset(m_buffer,data,m_size);
		return *this;
	}

	template<>
	string MemoryBlock::getValue(size_t offset)const
	{
		byte *lastByte = (byte*)m_buffer + m_size - 1;
		byte data = *lastByte;
		*lastByte = byte(0);
		string s((char const*)((byte*)m_buffer + offset));
		*lastByte = data;
		return s;
	}

	template<>
	MemoryBlock& MemoryBlock::setValue<string>(string const& s,size_t offset)
	{
		long len;
		if((len = m_size - offset - 1) < 0)
			return *this;		
		len = std::min<long>(s.size(),len);
		memcpy((byte*)m_buffer + offset,s.data(),len);
		*((byte*)m_buffer + offset + len) = byte(0);
		return *this;
	}
}

 

给getValue,setValue添加了默认参数 ,表示从第几个字节开始提取数据,值得注意的是 在头文件中声明类成员模板特化版本的时候并没有写上默认参数的值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值