/* This file is part of cardwords
   (c) 1998 1999 Tobias Peters
   see file COPYING for the copyright terms.
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// cardwords_exceptions.hh

#ifndef CARDWORDS_EXCEPTIONS_HH
#define CARDWORDS_EXCEPTIONS_HH
#include <errno.h>
#include "cardwords_iostream.hh"
#include "cardwords_gettext.hh"
#include <string.h>
#include <string>

// Defining some types here that indicate exceptional states

// They are all derived from:
class CardWords_Exception
{
public:
  virtual
  void
  print(ostream &) const = 0;
  virtual
  ~CardWords_Exception();
};

ostream &
operator << (ostream & o, const CardWords_Exception & x);


// Thrown when the resources of the computer are exploited:
// Low memory, too many filedescriptors etc.
class CardWords_XLowResources : public CardWords_Exception
{
public:
  // the libc errno that caused this exception
  int error_number;

  // a string describing the situation that caused this exception
  const string description;

  CardWords_XLowResources (int en, const string & desc);
  CardWords_XLowResources (const string & desc);

  void
  print(ostream &) const;
};


// Thrown when a bug in cardwords shows up:
class CardWords_XInternal_Error : public CardWords_Exception
{
public:
  // the libc errno that caused this exception
  int error_number;

  // a string describing the situation that caused this exception
  const string description;

  CardWords_XInternal_Error (const string & desc);

  void
  print(ostream &) const;
};

  
// Thrown when a member of array/vector/map was requested with illegal
// argument:
class CardWords_XOutOfRange : public CardWords_Exception
{
public:
  // a string describing the situation that caused this exception
  const string description;

  CardWords_XOutOfRange (const string & desc) 
    : description(desc) {};
  CardWords_XOutOfRange (const CardWords_XOutOfRange & o)
    : description(o.description) {};

  void
  print(ostream &) const;
};
  

// Thrown when a file does not exist or permissions deny the usage of it:
class CardWords_XAccessingFile : public CardWords_Exception
{
public:
  // the libc errno that caused this exception
  const int error_number;

  // a string describing the situation that caused this exception
  const string description;

  // the filename that caused this exception
  const string filename;

  CardWords_XAccessingFile (int en, const string & desc, const string & fn);
  CardWords_XAccessingFile (const string & desc, const string & fn);

  void
  print(ostream &) const;
};


// thrown when the input does not match the expected format:
class CardWords_XBadInput : public CardWords_Exception
{
public:
  const string description;
  const string expected;

  CardWords_XBadInput (const string & desc, const string & exp);

  void
  print(ostream &) const;
};


// Thrown when a request for a member of a union is scheduled and that member
// is currently not existing and the type of the return value has no way to
// tell that the request was invalid (like a NULL pointer).
class CardWords_XInvalidRequest : public CardWords_Exception
{
public:
  // a string describing the situation that caused this exception
  const string description;

  CardWords_XInvalidRequest (const string & desc)
    : description(desc){};

  void
  print(ostream &) const;
};

#define CardWords_XBadNetworkMessage CardWords_XBadInput

#endif

