您的位置:首页 > 编程语言 > C语言/C++

[C++]assert的加强版——Ensure的简易实现

2014-01-06 02:23 429 查看
原文:http://www.cnblogs.com/cbscan/archive/2012/10/26/2740838.html

刘未鹏牛最近的一篇文章“C++11和快速迭代开发”中,提到一个小工具Ensure,很有用,但他不方便提供具体代码,这里我给出一个简易实现。

 

Ensure用法如:
ENSURE(0 <= index && index < v.size())(index)(v.size());


断言失败时,会打印:
Failed: 0 <= index && index < v.size()
File: xxx.cpp Line: 123
Context Variables:
index = 12345
v.size() = 100


 

概括来说,Ensure至少包括以下特性:

1) 不区分debug和release,始终生效。

2) 断言失败时,打印表达式,同assert。

3) 打印文件和行号。

4) 可以追加无限个()来捕获上下文中表达式的文本和值。

 

另外,可以选择抛出异常,对于没有被捕获的异常,在windows下生成mindump。

 

我的简易版:

#include <exception>
#include <sstream>

class EnsureException:
public std::exception
{
public:
EnsureException(const char *exp, const char *file, int line)
{
std::ostringstream so;
so << "ensure failed : " << exp << '\n';
so << file << '(' << line << ')' << '\n';
so << "context variables:\n";
m_what = so.str();
}
~EnsureException() throw (){}

template<typename T>
EnsureException& operator << (const std::pair<const char *, T>& p)
{
std::ostringstream so;
so << '\t' << p.first << " : " << p.second << '\n';
m_what += so.str();
return *this;
}
EnsureException& operator << (int){ return *this; }

const char *what() const throw ()
{
return m_what.c_str();
}
private:
mutable std::string m_what;
};

static int A = 0, B = 0;
#define AB(a, N) std::make_pair(#a, a) << N
#define A(a) AB(a, B)
#define B(a) AB(a, A)
#define ENSURE(b) if (b); else throw EnsureException(#b, __FILE__, __LINE__) << A

int main()
{
try
{
int a = 2, b = 3;
ENSURE(a > 1);
ENSURE(a > 1)(a);
ENSURE(a > 2 && b <= 3)(a)(b)(5);
}
catch (const EnsureException& e)
{
cout << "catch exception:\n" << e.what() << endl;
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: