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

刘未鹏牛最近的一篇文章“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;
    }
}
posted @ 2012-10-26 11:24  Scan.  阅读(3458)  评论(2编辑  收藏  举报