/*
 *  This file is part of the X10 project (http://x10-lang.org).
 *
 *  This file is licensed to You under the Eclipse Public License (EPL);
 *  You may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *      http://www.opensource.org/licenses/eclipse-1.0.php
 *
 *  (C) Copyright IBM Corporation 2006-2010.
 */

#ifndef X10AUX_THROW_H
#define X10AUX_THROW_H

#include <cstdlib>
#include <x10aux/config.h>
#include <x10aux/ref.h>

#include <x10/lang/Throwable.h>

namespace x10aux {

    template<class T> void throwException() X10_PRAGMA_NORETURN;

    void throwException(x10aux::ref<x10::lang::Throwable> e) X10_PRAGMA_NORETURN;

    void throwArithmeticException() X10_PRAGMA_NORETURN;

    inline void throwException(x10aux::ref<x10::lang::Throwable> e) {
        throw e->fillInStackTrace();
    }

    template<class T> void throwException() {
        throwException(T::_make());
    }

    inline x10_byte zeroCheck(x10_byte val) {
        #if !defined(NO_CHECKS) && !defined(NO_EXCEPTIONS)
        if (0 == val) throwArithmeticException();
        #endif
        return val;
    }
    inline x10_ubyte zeroCheck(x10_ubyte val) {
        #if !defined(NO_CHECKS) && !defined(NO_EXCEPTIONS)
        if (0 == val) throwArithmeticException();
        #endif
        return val;
    }
    inline x10_short zeroCheck(x10_short val) {
        #if !defined(NO_CHECKS) && !defined(NO_EXCEPTIONS)
        if (0 == val) throwArithmeticException();
        #endif
        return val;
    }
    inline x10_ushort zeroCheck(x10_ushort val) {
        #if !defined(NO_CHECKS) && !defined(NO_EXCEPTIONS)
        if (0 == val) throwArithmeticException();
        #endif
        return val;
    }
    inline x10_int zeroCheck(x10_int val) {
        #if !defined(NO_CHECKS) && !defined(NO_EXCEPTIONS)
        if (0 == val) throwArithmeticException();
        #endif
        return val;
    }
    inline x10_uint zeroCheck(x10_uint val) {
        #if !defined(NO_CHECKS) && !defined(NO_EXCEPTIONS)
        if (0 == val) throwArithmeticException();
        #endif
        return val;
    }
    inline x10_long zeroCheck(x10_long val) {
        #if !defined(NO_CHECKS) && !defined(NO_EXCEPTIONS)
        if (0 == val) throwArithmeticException();
        #endif
        return val;
    }
    inline x10_ulong zeroCheck(x10_ulong val) {
        #if !defined(NO_CHECKS) && !defined(NO_EXCEPTIONS)
        if (0 == val) throwArithmeticException();
        #endif
        return val;
    }
}

#endif
// vim:tabstop=4:shiftwidth=4:expandtab
