Nullable (bug)

 

#ifndef NULLABLE_H
#define NULLABLE_H

#include "odbclib.h"

namespace odbclib
{
template<typename T>
class Nullable
{
	typedef T const& const_reference;
	typedef T const* const_pointer;
public:
	Nullable()
		:m_value(T()),
		m_isNull(true)
	{
	}

	Nullable(T const& value)
		:m_value(value)
	{
	}

	bool isNull() const{return m_isNull;}

	operator T()throw(runtime_error)
	{
		if(isNull())
			throw runtime_error("null has no value");
		return m_value;
	}

	operator T()const throw(runtime_error)
	{
		if(isNull())
			throw runtime_error("null has no value");
		return m_value;
	}

	Nullable& operator=(Nullable const& value)
	{
		if(value.isNull())
			m_isNull = true;
		return *this;
	}

	Nullable& operator=(T const& value)
	{
		m_value = value;
		m_isNull = false;
		return *this;
	}


	T const& value()const throw(runtime_error)
	{
		if(isNull())
			throw runtime_error("null has no value");
		return m_value;
	}

	T& value()throw(runtime_error)
	{
		if(isNull())
			throw runtime_error("null has no value");
		return m_value;
	}
public:
	static Nullable const& Null;

private:
	T m_value;
	bool m_isNull;

	template<typename V>
	friend ostream& operator<<(ostream&,Nullable<V> const&) throw(runtime_error);

	template<typename V>
	friend istream& operator>>(istream&,Nullable<V> &);
};

template<typename T>
Nullable<T> const& Nullable<T>::Null = Nullable<T>();

template<typename T> 
ostream& operator<<(ostream& os,Nullable<T> const& value) throw(runtime_error)
{
	if(value.m_isNull)
		throw runtime_error("null has no value");
	os << value.m_value;
	return os;
}

template<typename T>
istream& operator>>(istream& is,Nullable<T> &value)
{
	is >> value.m_value;
	return is;
}

template<>
class Nullable<string>
{
public:
	Nullable()
		:m_value(string()),
		m_isNull(true)
	{

	}

	Nullable(string const& value)
		:m_value(value),
		m_isNull(false)
	{
	}

	Nullable(char const* value)
		:m_value(value),
		m_isNull(false)
	{
	}

	Nullable(Nullable const& other)
	{
		m_value = other.m_value;
		m_isNull = other.m_isNull;
	}

	template<size_t N>
	Nullable(char const (&a)[N])
		:m_value(a),
		m_isNull(false)
	{
	}

	bool isNull() const{return m_isNull;}

	operator string()throw(runtime_error)
	{
		if(isNull())
			throw runtime_error("null has no value");
		return m_value;
	}

	operator string()const throw(runtime_error)
	{
		if(isNull())
			throw runtime_error("null has no value");
		return m_value;
	}

	Nullable& operator=(char const* value)
	{
		m_value.assign(value);
		m_isNull = false;
		return *this;
	}

	string const& value()const throw(runtime_error)
	{
		if(isNull())
			throw runtime_error("null has no value");
		return m_value;
	}

	string& value()throw(runtime_error)
	{
		if(isNull())
			throw runtime_error("null has no value");
		return m_value;
	}
public:
	static Nullable const& Null;

private:
	string m_value;
	bool m_isNull;

	template<typename V>
	friend ostream& operator<<(ostream&,Nullable<V> const&) throw(runtime_error);

	template<typename V>
	friend istream& operator>>(istream&,Nullable<V> &);
};
}

#endif

 

 

#include "odbclib.h"

namespace odbclib
{
	Nullable<string> const& Nullable<string>::Null = Nullable<string>();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值