[ create a new paste ] login | about

Link: http://codepad.org/whlwuKMt    [ raw code | output | fork ]

AaronMiller - C++, pasted on Jul 8:
/* Includes */
#include <stdio.h>

/* Check whether compilation is in C or C++ */
#if !defined(__cplusplus) && !defined(cplusplus)
#   define inline __inline
/* or, for maximum compatibility, define inline static */
#endif

/* Make the compilers play nice */
#if defined(__GCC__)
#   define forceinline __attribute__((always_inline))
#   define breakpoint() __asm__ __volatile__("int $0x80")
#elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
#   define forceinline __forceinline
#   define breakpoint() __asm { int 0x80 }
#else
#   define forceinline inline
#   define breakpoint() asm { int 0x80 } /* give it a shot? */
#endif

/* Remove breakpoint support if not available */
#if !defined(DEBUG)
#   undef breakpoint
#   define breakpoint() /* empty */
#endif

/* Internal validation function */
forceinline void __validate__(const char *pszMsg, const char *pszFile, unsigned int line)
{
    printf("debug:%s:%u: %s\n", pszFile, line, pszMsg);
}

/* Validation macro (pass it an expression, like "a==4") */
#define validate(e) { if (!(e)) { __validate__(#e, __FILE__, __LINE__); breakpoint(); } }

/* Test */
int main()
{
    int a = 2;
    int b = 2;
    a = a + b;
    validate(a==3); /* throw an error */
    return 0;
}


Output:
1
debug:t.cpp:44: a==3


Create a new paste based on this one


Comments: