#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>();
}
分享到:
相关推荐
使用javax Nullable注解必须使用的jar包
用例子的形式详解了Nullable的用法
Laravel开发-nullable 对对象进行链调用,无论对象是否为空
Laravel开发-laravel-nullable Laravel 5-向模型添加可空列
最近发现之前写的代码生成器(entity、dao、service、controller、vue) 有点bug,在Service层判断空的时候,少了一部分条件。所以补充上了,随后又同事问我在代码中发下了@Nullable注解不知道怎么用?脑子是个好...
java运行依赖jar包
Laravel开发-nullable .zip
(GOOD,PASS)LINQ TO DATATABLE OR DATASET 完美解决方案,可排除DATASET不支持System.Nullable错误,并为GRIDVIEW增加空行.txt
iOS9新出关键字nonnull,nullable,null_resettable,_Null_unspecified 需要注意的一点只能修饰对象,不能修饰基本数据类型. 虽然在项目的代码编写中不会经常用到,不过在调用苹果系统方法的时候还是会经常遇到,需要做...
主要介绍了PHP7.1新功能之Nullable Type用法,结合实例形式较为详细的对比分析了Nullable Type类型操作的使用技巧,需要的朋友可以参考下
可变参数、不定参数Demo源码,nullable、nonnull、_Nullable 等区别Demo源码。 博客地址: https://dcsnail.blog.csdn.net/article/details/79107962 https://dcsnail.blog.csdn.net/article/details/79230770
Laravel 表单验证规则:required、present、filled 和 nullable
一个快速而肮脏的项目,用于读取带有@Nullable批注的类文件并输出相应的Java源代码。 仅生成存根类,并且仅包含具有@Nullable批注的方法。 生成的源代码是格式正确的Java,但不能替代原始类文件。 至少可以在中分发...
Nullable<int> i = new Nullable(3); int i; //默认值0 int? ii; //默认值null ?? : 双问号 可用于判断一个变量在为 null 时返回一个指定的值。 接下来我们详细说明。 C# 可空类型(Nullable) C# 提供了一个特殊的...
null值用来表示数据类型未被赋予任何值,它是一种引用类型;void表示没有类型,或者说是没有任何值。null与void的区别可以认为void是根本没有,而null是一个空箱子,里面什么都没有。
Laravel的可空数据库字段 通常,未分配值的数据库字段默认为null 。 在创建具有外键约束的记录时,这一点尤其重要。...为了使用此特征,请将其导入到Eloquent模型中,然后将受保护的$nullable属性设置为一个字
今天小编就为大家分享一篇解决Django 在ForeignKey中出现 non-nullable field错误的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
ELinq 是一个轻量简单易用的开源Linq ORM数据访问组件,支持Nullable类型和枚举类型,支持根据实体类自动建库建表建关系,支持根据数据库通过T4模版自动生成实体代码,对Linq的谓词提供了完美的支持,旨在让绝大部份...