[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至少包括以下特性:
1) 不区分debug和release,始终生效。
2) 断言失败时,打印表达式,同assert。
3) 打印文件和行号。
4) 可以追加无限个()来捕获上下文中表达式的文本和值。
另外,可以选择抛出异常,对于没有被捕获的异常,在windows下生成mindump。
我的简易版:
刘未鹏牛最近的一篇文章“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; } }
相关文章推荐
- [C++]assert的加强版——Ensure的简易实现
- c++实现一个简易vector加强版
- 【RLIB】内存流(Stream) 的简易C++实现
- c++实现的简易的迷宫算法
- 【RLIB】Socket类 的C++简易实现
- 简易C/C++日志代码实现
- 【C++】C++简易实现C#的delegate
- ASSERT的简易实现
- C++ 简易string类实现(四)-自动操作引用次数
- C++ 线程池的简易实现
- C++ 中 make_pair 的简易实现
- C#delegate的C++简易实现
- C++利用链表模板类实现一个简易队列
- C++实现简易通讯录系统
- C++ StringBuilder 简易实现,多个对象共享同一内存
- 用C++实现简易的文本编辑器
- C++实现一个简易的内存池分配器
- c++链表实现学生成绩管理系统(简易版)
- 01-基于C++的简易技能系统实现
- C++ 简易string类实现(五)-进一步抽象