/*
 *  ZThreads, a platform-independant, multithreading and 
 *  synchroniation library
 *
 *  Copyright (C) 2000-2002, Eric Crahen, See LGPL.TXT for details
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 *  SUNY @ Buffalo, hereby disclaims all copyright interest in the
 *  ZThreads library written by Eric Crahen
 */

#ifndef __ZTEXCEPTIONS_H__
#define __ZTEXCEPTIONS_H__

/**
 * @author Eric Crahen <crahen@cse.buffalo.edu>
 * @date <2002-02-24T09:31:11-0500>
 * @version 1.1.0
 *
 * Revised all the exception classes. They now can carry some useful
 * information. Also, many of the exceptions that were previously provided 
 * were not necessary and overlapped with other exception. A tighter
 * more concise set of exceptions has been created to replace the 
 * old exceptions
 */

#include "zthread/ZThread.h"
#include <string>

namespace ZThread { 
  
/**
 * @class Synchronization_Exception
 *
 * Serves as a general base class for the Exception hierarchy used within
 * this package.
 *
 */
class ZTHREAD_API Synchronization_Exception {

  private:

  // Restrict heap allocation
  static void * operator new(size_t size);
  static void * operator new[](size_t size);

  Synchronization_Exception& operator=(const Synchronization_Exception&);

  std::string *_msg;
  int* _count;

  public:
  
  /**
   * Create a new exception with a default error message 'Synchronization 
   * Exception'
   */
  Synchronization_Exception() throw();

  /**
   * Create a new exception by copying another
   *
   * @param const Synchronization_Exception& - exception to copy
   */
  Synchronization_Exception(const Synchronization_Exception&) throw();

  /**
   * Create a new exception with a given error message
   *
   * @param const char* - error message
   */
  Synchronization_Exception(const char*) throw();

  /**
   * Destroy the exception object
   */
  ~Synchronization_Exception() throw();

  /**
   * Get additional info about the exception
   *
   * @return const char* for the error message
   */
   const char * what() const throw();
  
};
  

/**
 * @class Interrupted_Exception
 *
 * Used to describe an interrupted operation that would have normally
 * blocked the calling thread
 */
class ZTHREAD_API Interrupted_Exception : public Synchronization_Exception {
  public:

  //! Create a new exception
  Interrupted_Exception(const char*) throw();
  //! Create a new exception
  Interrupted_Exception() throw();

};
  

/**
 * @class Unexpected_Exception
 *
 * Used to describe an interrupted operation that would have normally
 * blocked the calling thread
 */
class ZTHREAD_API Unexpected_Exception : public Synchronization_Exception {
  public:

  //! Create a new exception
  Unexpected_Exception(const char*) throw();
  //! Create a new exception
  Unexpected_Exception() throw();

};
  

  
  
/**
 * @class Deadlock_Exception
 *
 * Thrown when deadlock has been detected
 */
class ZTHREAD_API Deadlock_Exception : public Synchronization_Exception {
  public:

  //! Create a new exception
  Deadlock_Exception(const char*) throw();    
  //! Create a new exception
  Deadlock_Exception() throw();

};
  
  
/**
 * @class InvalidOp_Exception
 *
 * Thrown when performing an illegal operation this object
 */
class ZTHREAD_API InvalidOp_Exception : public Synchronization_Exception {
  public:

  //! Create a new exception
  InvalidOp_Exception(const char*) throw();
  //! Create a new exception
  InvalidOp_Exception() throw();

};

  
  
/**
 * @class Initialization_Exception
 *
 * Thrown when the system has no more resources to create new 
 * synchronization controls
 */
class ZTHREAD_API Initialization_Exception : public Synchronization_Exception {
  
  public:

  //! Create a new exception
  Initialization_Exception(const char*) throw();
  //! Create a new exception
  Initialization_Exception() throw();

};
  
/**
 * @class Cancelation_Exception
 *
 * Cancelation_Exceptions are thrown by 'Canceled' objects. 
 * @see Cancelable
 */
class ZTHREAD_API Cancelation_Exception : public Synchronization_Exception {

  public:
  
  //! Create a new Canceltion_Exception
  Cancelation_Exception() throw();
  //! Create a new Canceltion_Exception
  Cancelation_Exception(const char*) throw();
  
};
  

/**
 * @class Timeout_Exception
 *
 * There is no need for error messaged simply indicates the last
 * operation timed out
 */
class ZTHREAD_API Timeout_Exception : public Synchronization_Exception {
  public:

  //! Create a new exception
  Timeout_Exception() throw();

};

/**
 * @class NoSuchElement_Exception
 * 
 * The last operation that was attempted on a Queue could not find 
 * the item that was indicated (during that last Queue method invocation)
 */
class ZTHREAD_API NoSuchElement_Exception {
  private:
  
  NoSuchElement_Exception(NoSuchElement_Exception*);
  NoSuchElement_Exception& operator=(NoSuchElement_Exception&);
  
  public:
  
  //! Create a new exception
  NoSuchElement_Exception() throw() {};

};
  
}; 

#endif
