#ifndef _RS_EXCEPT_H
#define _RS_EXCEPT_H

#include "rs/defs.h"
#include "rs/string.h"

/*#ifdef __MSC__
class Xalloc
 {public:
  size_t x;
  Xalloc(size_t i);
  ~Xalloc();
  void raise();
  size_t requested() {return x;}
 };
#endif
*/

#ifdef _HAS_INT64
#define EXCEPTION_LONG int64
#else
#define EXCEPTION_LONG long
#endif

class xdb
 {public:

   TStr w;//error name
   TStr i;//comma-separated parameters

   xdb(const char * error,const char * info=0);
   xdb(const char * error,const char * a,const char* b,
                          const char * c=0,const char* d=0,
                          const char * e=0,const char* f=0);

   xdb(const char * error,const char * a,const char* b,
                          const char * c,EXCEPTION_LONG  d,
                          const char * e=0,EXCEPTION_LONG  f=0);

   xdb(const char * error,const char * a,EXCEPTION_LONG  b,
                          const char * c=0,EXCEPTION_LONG  d=0,
                          const char * e=0,EXCEPTION_LONG  f=0,
                          const char * g=0,EXCEPTION_LONG  h=0);

   xdb();

   xdb(const xdb& o);

   xdb(const xdb* o);

   const char* why() {return w;}
   const char* info() {return i;}

//#ifdef _AVOID_EXCEPTIONS
   //void operator += (const xdb& o); //!appends error info with newlines
   void operator = (const xdb& o) {w=o.w; i=o.i;}
   operator bool() {return *(char*)(w) != 0;}
   void operator = (int) {w=(char*)NULL; i=(char*)NULL;}
//#endif
 //  void raise();
   ~xdb();
  };

/*
#define OUTOFMEM(i){ char __tempxmem[62]; \
  wsprintf(__tempxmem,"Out of memory. Couldn\'t allocate %i bytes",i); \
  dspMessageAlert(__tempxmem,"Out of Memory");}

//usage: catch XALL(return 0);

#ifdef __BORLANDC__ //windows
#define XALL(f) (xalloc &x) {OUTOFMEM(x.requested());f;} \
           catch(xdb &x)   {dspMessageAlert(x.info(),x.why());f;} \
           catch(xmsg &x) {dspMessageAlert(x.why().c_str(),__FILE__);f;}\
           catch(...) {f;}
#else
#define XALL(f) \
           catch(xdb &x)  {dspMessageAlert(x.info(),x.why()); f;} \
           catch(...) {f;}
#endif

#ifdef __BORLANDC__
#define XANY (xalloc &x) {OUTOFMEM(x.requested());} \
        catch(xmsg &x) {dspMessageAlert(x.why().c_str(),__FILE__);} \
        catch(xdb &x)  {dspMessageAlert(x.info(),x.why());} \
        catch(...) {}
#else
#define XANY \
        (Xalloc &x) {OUTOFMEM(x.requested());} \
        catch(xdb &x)  {dspMessageAlert(x.info(),x.why());} \
        catch(...) {}
#endif
*/
#endif
