// -*- c++ -*-
//------------------------------------------------------------------------------
// $Id: Assert.h,v 1.4 2003/07/25 02:51:46 vlg Exp $
//------------------------------------------------------------------------------
//                           Assert.h
//------------------------------------------------------------------------------
//  Copyright (C) 1997-2000  Vladislav Grinchenko <vladg@erols.com>
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Library General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
#ifndef ASSERT_H
#define ASSERT_H

#include <unistd.h>
#include <errno.h>              /* errno                   */
#include <signal.h>             /* raise                   */

#include "assa/Logger.h"

namespace ASSA {

/** @file Assert.h

	A collection of assert function wrappers.
*/

/** @def Assert_exit(exp_)
	Macro that makes program exit if assert fails.

	assert a la ASSA. If expression exp_ is evaluated to false, error message
	is logged and current process is terminated with SIGTERM signal.
 
	@param exp_ expression to evaluate
*/

#define Assert_exit( exp_ ) \
    do { \
        if ( !(exp_) ) { \
	       DL((ASSA::ERROR,"Assert Aborted False Expression!\n")); \
	       DL((ASSA::ERROR,"Error on line %d in file %s\n", __LINE__, __FILE__)); \
	       ::raise( SIGTERM ); \
        } \
    } while (0)


/** @def Assert_return(exp_)
	Test condition and return bool from a function if assertion fails
    
	Expression exp_ is evaluated and tested for the truth.
	If expression is false, error message with file name and line
	number is logged to the log file, and program control is returned back 
	from current execution scope with return value equal to FALSE.
	
	@param exp_ expression to evaluate
 
	@return FALSE if expression evaluates to false; if expression happens
	to evaluate to TRUE, Assert_return() DOES NOT return - program continues
	its natural execution flow.
 */

#define Assert_return(exp_) \
    do { \
        if ( !(exp_) ) { \
	        DL((ASSA::ERROR,"Assert Returned False Expression!\n")); \
            DL((ASSA::ERROR,"Error on line %d in file %s\n", __LINE__, __FILE__)); \
	        return (false); \
        } \
    } while (0) 

} // end namespace ASSA

#endif /* ASSERT_H */  
