// Copyright 1997-1998 Omni Development, Inc.  All rights reserved.
//
// $Header: /LocalDeveloper/Source/CVS/OmniGroup/OmniBase/assertions.h,v 1.10 1998/01/12 06:53:11 kc Exp $

#import "FrameworkDefines.h"
#import <objc/objc.h>

#if defined(DEBUG) || defined(OMNIMAKE_FORCE_ASSERTIONS)
#define OMNI_ASSERTIONS_ON
#endif

// This allows you to turn off assertions when debugging
#if defined(OMNIMAKE_FORCE_ASSERTIONS_OFF)
#undef OMNI_ASSERTIONS_ON
#warning Forcing assertions off!
#endif

// Define OMNI_ABORT_ON_ASSERTION_FAILURE if you want applications to exit when assertions fail.  (You must rebuild assertions.m for this to take effect.)

// #define OMNI_ABORT_ON_ASSERTION_FAILURE



typedef void (*OBAssertionFailureHandler)(const char *type,
                                          const char *expression,
                                          const char *file,
                                          unsigned int lineNumber);

#if defined(OMNI_ASSERTIONS_ON)

    OmniBase_EXTERN void OBSetAssertionFailureHandler(OBAssertionFailureHandler handler);

    OmniBase_EXTERN void OBAssertFailed(const char *type, const char *expression,
                                        const char *file, unsigned int lineNumber);


    #define PRECONDITION(expression)                                            \
    do {                                                                        \
        if (!(expression))                                                      \
            OBAssertFailed("PRECONDITION", #expression, __FILE__, __LINE__);    \
    } while (NO)

    #define POSTCONDITION(expression)                                           \
    do {                                                                        \
        if (!(expression))                                                      \
            OBAssertFailed("POSTCONDITION", #expression, __FILE__, __LINE__);   \
    } while (NO)

    #define INVARIANT(expression)                                               \
    do {                                                                        \
        if (!(expression))                                                      \
            OBAssertFailed("INVARIANT", #expression, __FILE__, __LINE__);       \
    } while (NO)

    #ifdef ASSERT
    #undef ASSERT
    #endif
    #define ASSERT(expression)                                                  \
    do {                                                                        \
        if (!(expression))                                                      \
            OBAssertFailed("ASSERT", #expression, __FILE__, __LINE__);          \
    } while (NO)

#else	// else insert blank lines into the code

    #define PRECONDITION(expression)
    #define POSTCONDITION(expression)
    #define INVARIANT(expression)
    #ifdef ASSERT
    #undef ASSERT
    #endif
    #define ASSERT(expression)

#endif
