类成员函数模板特化

 

//header file
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) throw(runtime_error);
		template<typename T>
		T getValue() const
		{
			//TODO:may cross memory boundary
			T *p = (T *)m_buffer;
			return *p;
		}		
		template<typename T>
		MemoryBlock& setValue(T const& t)
		{			
			initBlock(0);
			memcpy(m_buffer,(void const*)&t,min(sizeof(T),m_size));
			return *this;
		}
		template<typename T,size_t N>
		MemoryBlock& setValue(T const (&t)[N])
		{			
			T * p = (T *)m_buffer;
			T * end = (T *)((byte*)m_buffer + m_size);
			int i = 0;
			initBlock(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()const;
	template<> MemoryBlock& MemoryBlock::setValue<string>(string const&);
}

 

//source file
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) throw(runtime_error)
	{			
		byte* newAddr =	(byte*)::realloc(m_buffer,sz);
		if(!newAddr)
			throw runtime_error("realloc failed!");			
		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()const
	{
		byte *lastByte = (byte*)m_buffer + m_size - 1;
		byte data = *lastByte;
		*lastByte = byte(0);
		string s((char const*)m_buffer);
		*lastByte = data;
		return s;
	}

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

 通过 vc 2010 , g++ 4.5.2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值