/* AXPbox Alpha Emulator
 * Copyright (C) 2020 Tomáš Glozar
 * Website: https://github.com/lenticularis39/axpbox
 *
 * Forked from: ES40 emulator
 * Copyright (C) 2007-2008 by the ES40 Emulator Project
 * Copyright (C) 2007 by Camiel Vanderhoeven
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 *
 * Although this is not required, the author would appreciate being notified of,
 * and receiving any modifications you may make to the source code that might
 * serve the general public.
 *
 * Parts of this file based upon the Poco C++ Libraries, which is Copyright (C)
 * 2004-2006, Applied Informatics Software Engineering GmbH. and Contributors.
 */

//
// Exception.h
//
// $Id: Exception.h,v 1.1 2008/05/31 15:47:23 iamcamiel Exp $
//
// Library: Foundation
// Package: Core
// Module:  Exception
//
// Definition of various Poco exception classes.
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//

#ifndef Foundation_Exception_INCLUDED
#define Foundation_Exception_INCLUDED

#include "Foundation.hpp"
#include <stdexcept>

class CException : public std::exception
/// This is the base class for all exceptions defined
/// in the Poco class library.
{
public:
  CException(const std::string &msg, int code = 0);
  /// Creates an exception.

  CException(const std::string &msg, const std::string &arg, int code = 0);
  /// Creates an exception.

  CException(const std::string &msg, const CException &nested, int code = 0);
  /// Creates an exception and stores a clone
  /// of the nested exception.

  CException(const CException &exc);
  /// Copy constructor.

  ~CException() throw();
  /// Destroys the exception and deletes the nested exception.

  CException &operator=(const CException &exc);
  /// Assignment operator.

  virtual const char *name() const throw();
  /// Returns a static string describing the exception.

  virtual const char *className() const throw();
  /// Returns the name of the exception class.

  virtual const char *what() const throw();
  /// Returns a static string describing the exception.
  ///
  /// Same as name(), but for compatibility with std::exception.

  const CException *nested() const;
  /// Returns a pointer to the nested exception, or
  /// null if no nested exception exists.

  const std::string &message() const;
  /// Returns the message text.

  int code() const;
  /// Returns the exception code if defined.

  std::string displayText() const;
  /// Returns a string consisting of the
  /// message name and the message text.

  virtual CException *clone() const;
  /// Creates an exact copy of the exception.
  ///
  /// The copy can later be thrown again by
  /// invoking rethrow() on it.

  virtual void rethrow() const;
  /// (Re)Throws the exception.
  ///
  /// This is useful for temporarily storing a
  /// copy of an exception (see clone()), then
  /// throwing it again.

protected:
  CException(int code = 0);
  /// Standard constructor.

private:
  std::string _msg;
  CException *_pNested;
  int _code;
};

//
// inlines
//
inline const CException *CException::nested() const { return _pNested; }

inline const std::string &CException::message() const { return _msg; }

inline int CException::code() const { return _code; }

//
// Macros for quickly declaring and implementing exception classes.
// Unfortunately, we cannot use a template here because character
// pointers (which we need for specifying the exception name)
// are not allowed as template arguments.
//
#define POCO_DECLARE_EXCEPTION(CLS, BASE)                                      \
  class CLS : public BASE {                                                    \
  public:                                                                      \
    CLS(int code = 0);                                                         \
    CLS(const std::string &msg, int code = 0);                                 \
    CLS(const std::string &msg, const std::string &arg, int code = 0);         \
    CLS(const std::string &msg, const CException &exc, int code = 0);          \
    CLS(const CLS &exc);                                                       \
    ~CLS() throw();                                                            \
    CLS &operator=(const CLS &exc);                                            \
    const char *name() const throw();                                          \
    const char *className() const throw();                                     \
    CException *clone() const;                                                 \
    void rethrow() const;                                                      \
  };

#define POCO_IMPLEMENT_EXCEPTION(CLS, BASE, NAME)                              \
  CLS::CLS(int code) : BASE(code) {}                                           \
  CLS::CLS(const std::string &msg, int code) : BASE(msg, code) {}              \
  CLS::CLS(const std::string &msg, const std::string &arg, int code)           \
      : BASE(msg, arg, code) {}                                                \
  CLS::CLS(const std::string &msg, const CException &exc, int code)            \
      : BASE(msg, exc, code) {}                                                \
  CLS::CLS(const CLS &exc) : BASE(exc) {}                                      \
  CLS::~CLS() throw() {}                                                       \
  CLS &CLS::operator=(const CLS &exc) {                                        \
    BASE::operator=(exc);                                                      \
    return *this;                                                              \
  }                                                                            \
  const char *CLS::name() const throw() { return NAME; }                       \
  const char *CLS::className() const throw() { return typeid(*this).name(); }  \
  CException *CLS::clone() const { return new CLS(*this); }                    \
  void CLS::rethrow() const { throw *this; }

//
// Standard exception classes
//
POCO_DECLARE_EXCEPTION(CLogicException, CException)
POCO_DECLARE_EXCEPTION(CAssertionViolationException, CLogicException)
POCO_DECLARE_EXCEPTION(CNullPointerException, CLogicException)
POCO_DECLARE_EXCEPTION(CBugcheckException, CLogicException)
POCO_DECLARE_EXCEPTION(CInvalidArgumentException, CLogicException)
POCO_DECLARE_EXCEPTION(CNotImplementedException, CLogicException)
POCO_DECLARE_EXCEPTION(CRangeException, CLogicException)
POCO_DECLARE_EXCEPTION(CIllegalStateException, CLogicException)
POCO_DECLARE_EXCEPTION(CInvalidAccessException, CLogicException)
POCO_DECLARE_EXCEPTION(CSignalException, CLogicException)
POCO_DECLARE_EXCEPTION(CUnhandledException, CLogicException)

POCO_DECLARE_EXCEPTION(CRuntimeException, CException)
POCO_DECLARE_EXCEPTION(CNotFoundException, CRuntimeException)
POCO_DECLARE_EXCEPTION(CExistsException, CRuntimeException)
POCO_DECLARE_EXCEPTION(CTimeoutException, CRuntimeException)
POCO_DECLARE_EXCEPTION(CSystemException, CRuntimeException)
POCO_DECLARE_EXCEPTION(CRegularExpressionException, CRuntimeException)
POCO_DECLARE_EXCEPTION(CLibraryLoadException, CRuntimeException)
POCO_DECLARE_EXCEPTION(CLibraryAlreadyLoadedException, CRuntimeException)
POCO_DECLARE_EXCEPTION(CNoThreadAvailableException, CRuntimeException)
POCO_DECLARE_EXCEPTION(CPropertyNotSupportedException, CRuntimeException)
POCO_DECLARE_EXCEPTION(CPoolOverflowException, CRuntimeException)
POCO_DECLARE_EXCEPTION(CNoPermissionException, CRuntimeException)
POCO_DECLARE_EXCEPTION(COutOfMemoryException, CRuntimeException)
POCO_DECLARE_EXCEPTION(CDataException, CRuntimeException)

POCO_DECLARE_EXCEPTION(CDataFormatException, CDataException)
POCO_DECLARE_EXCEPTION(CSyntaxException, CDataException)
POCO_DECLARE_EXCEPTION(CCircularReferenceException, CDataException)
POCO_DECLARE_EXCEPTION(CPathSyntaxException, CSyntaxException)
POCO_DECLARE_EXCEPTION(CIOException, CRuntimeException)
POCO_DECLARE_EXCEPTION(CFileException, CIOException)
POCO_DECLARE_EXCEPTION(CFileExistsException, CFileException)
POCO_DECLARE_EXCEPTION(CFileNotFoundException, CFileException)
POCO_DECLARE_EXCEPTION(CPathNotFoundException, CFileException)
POCO_DECLARE_EXCEPTION(CFileReadOnlyException, CFileException)
POCO_DECLARE_EXCEPTION(CFileAccessDeniedException, CFileException)
POCO_DECLARE_EXCEPTION(CCreateFileException, CFileException)
POCO_DECLARE_EXCEPTION(COpenFileException, CFileException)
POCO_DECLARE_EXCEPTION(CWriteFileException, CFileException)
POCO_DECLARE_EXCEPTION(CReadFileException, CFileException)
POCO_DECLARE_EXCEPTION(CUnknownURISchemeException, CRuntimeException)

POCO_DECLARE_EXCEPTION(CApplicationException, CException)
POCO_DECLARE_EXCEPTION(CBadCastException, CRuntimeException)

POCO_DECLARE_EXCEPTION(CConfigurationException, CException);
POCO_DECLARE_EXCEPTION(CThreadException, CException);
POCO_DECLARE_EXCEPTION(CWin32Exception, CException);
POCO_DECLARE_EXCEPTION(CSDLException, CException);
POCO_DECLARE_EXCEPTION(CGracefulException,
                       CException);                  /* User request to exit */
POCO_DECLARE_EXCEPTION(CAbortException, CException); /* User request to abort */

#endif // Foundation_Exception_INCLUDED
