“工业级” 断言

class Assert
{
public:
    Assert():SMART_ASSERT_A(*this), SMART_ASSERT_B(*this) {}
    Assert& SMART_ASSERT_A;
    Assert& SMART_ASSERT_B;
    //whatever member functions
    Assert& print_current_val(int i,const char* c)
    {
        printf("%s=%i\n", c, i);
        return *this;
    }
    Assert& print_current_val(const char * i,const char* c)
    {
        printf("%s=%s\n", c, i);
        return *this;
    }
    Assert& print_context(const char * info, const char* c, int i)
    {
        printf("ASSERT FAIL:%s\nFILE:%s, LINE:%i\n", info, c, i);
        return *this;
    }
};
Assert& make_assert()
{
    static Assert a;
    return a;
}

#define SMART_ASSERT_A(x) SMART_ASSERT_OP(x,B)
#define SMART_ASSERT_B(x) SMART_ASSERT_OP(x,A)
#define SMART_ASSERT_OP(x,next) \
    SMART_ASSERT_A.print_current_val((x),#x).SMART_ASSERT_##next

#define SMART_ASSERT(expr) \
    if( (expr) ) ; \
    else make_assert().print_context(#expr, __FILE__,__LINE__).SMART_ASSERT_A
#define _ASSERT SMART_ASSERT

 

你可能感兴趣的:(断言)