Blame libdjvu/GException.h

Packit df99a1
//C-  -*- C++ -*-
Packit df99a1
//C- -------------------------------------------------------------------
Packit df99a1
//C- DjVuLibre-3.5
Packit df99a1
//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
Packit df99a1
//C- Copyright (c) 2001  AT&T
Packit df99a1
//C-
Packit df99a1
//C- This software is subject to, and may be distributed under, the
Packit df99a1
//C- GNU General Public License, either Version 2 of the license,
Packit df99a1
//C- or (at your option) any later version. The license should have
Packit df99a1
//C- accompanied the software or you may obtain a copy of the license
Packit df99a1
//C- from the Free Software Foundation at http://www.fsf.org .
Packit df99a1
//C-
Packit df99a1
//C- This program is distributed in the hope that it will be useful,
Packit df99a1
//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit df99a1
//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit df99a1
//C- GNU General Public License for more details.
Packit df99a1
//C- 
Packit df99a1
//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
Packit df99a1
//C- Lizardtech Software.  Lizardtech Software has authorized us to
Packit df99a1
//C- replace the original DjVu(r) Reference Library notice by the following
Packit df99a1
//C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
Packit df99a1
//C-
Packit df99a1
//C-  ------------------------------------------------------------------
Packit df99a1
//C- | DjVu (r) Reference Library (v. 3.5)
Packit df99a1
//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
Packit df99a1
//C- | The DjVu Reference Library is protected by U.S. Pat. No.
Packit df99a1
//C- | 6,058,214 and patents pending.
Packit df99a1
//C- |
Packit df99a1
//C- | This software is subject to, and may be distributed under, the
Packit df99a1
//C- | GNU General Public License, either Version 2 of the license,
Packit df99a1
//C- | or (at your option) any later version. The license should have
Packit df99a1
//C- | accompanied the software or you may obtain a copy of the license
Packit df99a1
//C- | from the Free Software Foundation at http://www.fsf.org .
Packit df99a1
//C- |
Packit df99a1
//C- | The computer code originally released by LizardTech under this
Packit df99a1
//C- | license and unmodified by other parties is deemed "the LIZARDTECH
Packit df99a1
//C- | ORIGINAL CODE."  Subject to any third party intellectual property
Packit df99a1
//C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
Packit df99a1
//C- | non-exclusive license to make, use, sell, or otherwise dispose of 
Packit df99a1
//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
Packit df99a1
//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
Packit df99a1
//C- | General Public License.   This grant only confers the right to 
Packit df99a1
//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
Packit df99a1
//C- | the extent such infringement is reasonably necessary to enable 
Packit df99a1
//C- | recipient to make, have made, practice, sell, or otherwise dispose 
Packit df99a1
//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
Packit df99a1
//C- | any greater extent that may be necessary to utilize further 
Packit df99a1
//C- | modifications or combinations.
Packit df99a1
//C- |
Packit df99a1
//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
Packit df99a1
//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
Packit df99a1
//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
Packit df99a1
//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
Packit df99a1
//C- +------------------------------------------------------------------
Packit df99a1
Packit df99a1
#ifndef _GEXCEPTION_H_
Packit df99a1
#define _GEXCEPTION_H_
Packit df99a1
#ifdef HAVE_CONFIG_H
Packit df99a1
# include "config.h"
Packit df99a1
#endif
Packit df99a1
#if NEED_GNUG_PRAGMAS
Packit df99a1
# pragma interface
Packit df99a1
#endif
Packit df99a1
Packit df99a1
#ifndef no_return
Packit df99a1
#ifdef __GNUC__
Packit df99a1
#define no_return __attribute__ ((noreturn))
Packit df99a1
#else
Packit df99a1
#define no_return
Packit df99a1
#endif
Packit df99a1
#endif
Packit df99a1
Packit df99a1
/** @name GException.h
Packit df99a1
Packit df99a1
    Files #"GException.h"# and #"GException.cpp"# are left over from a
Packit df99a1
    portable exception scheme used through the DjVu Reference Library.  The
Packit df99a1
    setjmp/longjmp exception emulation code was motivated because many
Packit df99a1
    compilers did not properly support exceptions as mandated by the C++
Packit df99a1
    standard documents. This emulation is now considered obsolete. All
Packit df99a1
    exception are now based on C++ exception.
Packit df99a1
    
Packit df99a1
    There are four macros for handling exceptions.  Macros #G_TRY#, #G_CATCH# and
Packit df99a1
    #G_ENDCATCH# are used to define an exception catching block.  Exceptions can
Packit df99a1
    be thrown at all times using macro #G_THROW(cause)#. An exception can be
Packit df99a1
    re-thrown from a catch block using macro #G_RETHROW#.
Packit df99a1
    
Packit df99a1
    Example:
Packit df99a1
    \begin{verbatim}
Packit df99a1
    G_TRY
Packit df99a1
      {
Packit df99a1
        // program lines which may result in a call to THROW()
Packit df99a1
        G_THROW("message");
Packit df99a1
      }
Packit df99a1
    G_CATCH(ex) 
Packit df99a1
      {
Packit df99a1
        // Variable ex refers to a GException object.
Packit df99a1
        ex.perror();  
Packit df99a1
        // You can rethrow the exception to an outer exception handler.
Packit df99a1
        G_RETHROW;
Packit df99a1
      }
Packit df99a1
    G_ENDCATCH;
Packit df99a1
    \end{verbatim} 
Packit df99a1
Packit df99a1
    @memo 
Packit df99a1
    Portable exceptions.
Packit df99a1
    @author 
Packit df99a1
    L\'eon Bottou <leonb@research.att.com> -- initial implementation.\\
Packit df99a1
    Andrei Erofeev <eaf@geocities.com> -- fixed message memory allocation.
Packit df99a1
*/
Packit df99a1
//@{
Packit df99a1
Packit df99a1
#include "DjVuGlobal.h"
Packit df99a1
Packit df99a1
// Check if compiler supports native exceptions
Packit df99a1
#ifdef HAVE_CONFIG_H
Packit df99a1
# ifndef HAVE_EXCEPTIONS
Packit df99a1
#  define USE_EXCEPTION_EMULATION 1
Packit df99a1
# endif
Packit df99a1
#endif
Packit df99a1
Packit df99a1
#ifdef HAVE_NAMESPACES
Packit df99a1
namespace DJVU {
Packit df99a1
# ifdef NOT_DEFINED // Just to fool emacs c++ mode
Packit df99a1
}
Packit df99a1
# endif
Packit df99a1
#endif
Packit df99a1
Packit df99a1
/** Exception class.  
Packit df99a1
    The library always uses macros #G_TRY#, #G_THROW#, #G_CATCH# and #G_ENDCATCH# for
Packit df99a1
    throwing and catching exceptions (see \Ref{GException.h}). These macros
Packit df99a1
    only deal with exceptions of type #GException#. */
Packit df99a1
Packit df99a1
class DJVUAPI GException {
Packit df99a1
public:
Packit df99a1
  enum source_type { GINTERNAL=0, GEXTERNAL, GAPPLICATION, GOTHER };
Packit df99a1
  /** Constructs a GException.  This constructor is usually called by macro
Packit df99a1
      #G_THROW#.  Argument #cause# is a plain text error message. As a
Packit df99a1
      convention, string #ByteStream::EndOfFile# is used when reaching an unexpected
Packit df99a1
      end-of-file condition and string #DataPool::Stop# is used when the user
Packit df99a1
      interrupts the execution. The remaining arguments are usually provided
Packit df99a1
      by the predefined macros #__FILE__#, #__LINE__#, and (G++ and EGCS only)
Packit df99a1
      #__PRETTY_FUNCTION__#.  */
Packit df99a1
  GException (const char *cause, const char *file=0, int line=0, 
Packit df99a1
              const char *func=0, const source_type source=GINTERNAL);
Packit df99a1
Packit df99a1
  /** Copy Constructor. */
Packit df99a1
  GException (const GException & exc);
Packit df99a1
  
Packit df99a1
  /** Null Constructor. */
Packit df99a1
  GException ();
Packit df99a1
  
Packit df99a1
  /** Destructor. */
Packit df99a1
  virtual ~GException(void);
Packit df99a1
  
Packit df99a1
  /** Copy Operator. */
Packit df99a1
  GException & operator=(const GException & exc);
Packit df99a1
  
Packit df99a1
  /** Prints an error message on stderr.
Packit df99a1
      This function no longer takes a message parameter because 
Packit df99a1
      some instances used a i18n message id and other instances
Packit df99a1
      used a literal string. */
Packit df99a1
  void perror(void) const;
Packit df99a1
  
Packit df99a1
  /** Returns the string describing the cause of the exception.  The returned
Packit df99a1
      pointer is never null.  Exception handlers should not rely on the value
Packit df99a1
      of the string #cause#.  As a convention however, string
Packit df99a1
      #ByteStream::EndOfFile# is used
Packit df99a1
      when reaching an unexpected end-of-file condition and string
Packit df99a1
      #DataPool::Stop# is used when the user interrupts the execution. These
Packit df99a1
      strings can be tested by the exception handlers, with
Packit df99a1
      #cmp_cause#. Similar conventional strings may be defined
Packit df99a1
      in the future. They all will be small strings with only uppercase
Packit df99a1
      characters. */
Packit df99a1
  const char* get_cause(void) const;
Packit df99a1
Packit df99a1
  /** Compares the cause with the specified string, ignoring anything after
Packit df99a1
      the first tab. */
Packit df99a1
  int cmp_cause(const char s2[]) const;
Packit df99a1
Packit df99a1
  /** Compares the cause with the specified string, ignoring anything after
Packit df99a1
      the first tab. */
Packit df99a1
  static int cmp_cause(const char s1[],const char s2[]);
Packit df99a1
Packit df99a1
  /** Returns the function name from which the exception was thrown.
Packit df99a1
      A null pointer is returned if no function name is available. */
Packit df99a1
  const char* get_function(void) const { return func; }
Packit df99a1
  
Packit df99a1
  /** Returns the file name from which the exception was thrown.
Packit df99a1
      A null pointer is returned if no file name is available. */
Packit df99a1
  const char* get_file(void) const { return file; }
Packit df99a1
 
Packit df99a1
  /** Returns the exception source */
Packit df99a1
  source_type get_source(void) const { return source; }
Packit df99a1
 
Packit df99a1
  /** Returns the line number from which the exception was thrown.
Packit df99a1
      A zero is returned if no line number is available. */
Packit df99a1
  int get_line(void) const { return line; };
Packit df99a1
  
Packit df99a1
  //  Magic cause string
Packit df99a1
  static const char * const outofmemory;
Packit df99a1
Packit df99a1
private:
Packit df99a1
  const char *cause;
Packit df99a1
  const char *file;
Packit df99a1
  const char *func;
Packit df99a1
  int line;
Packit df99a1
  source_type source;
Packit df99a1
};
Packit df99a1
Packit df99a1
//@}
Packit df99a1
Packit df99a1
#undef G_TRY
Packit df99a1
#undef G_CATCH
Packit df99a1
#undef G_CATCH_ALL
Packit df99a1
#undef G_ENDCATCH
Packit df99a1
#undef G_RETHROW
Packit df99a1
#undef G_THROW
Packit df99a1
Packit df99a1
#ifdef USE_EXCEPTION_EMULATION
Packit df99a1
# error "Libdjvulibre requires c++ exceptions"
Packit df99a1
#else
Packit df99a1
Packit df99a1
// Compiler supports ANSI C++ exceptions.
Packit df99a1
// Defined exception macros accordingly.
Packit df99a1
Packit df99a1
# define G_TRY         try
Packit df99a1
# define G_CATCH(n)    catch(const GException &n) { 
Packit df99a1
# define G_CATCH_ALL   catch(...) { 
Packit df99a1
# define G_ENDCATCH    } 
Packit df99a1
# define G_RETHROW     throw
Packit df99a1
# ifdef __GNUG__
Packit df99a1
#  define G_THROW(msg) throw GException(msg,__FILE__,__LINE__,__PRETTY_FUNCTION__)
Packit df99a1
# else
Packit df99a1
#  define G_THROW(msg) throw GException(msg,__FILE__,__LINE__)
Packit df99a1
# endif
Packit df99a1
Packit df99a1
#endif
Packit df99a1
Packit df99a1
Packit df99a1
// -------------- THE END
Packit df99a1
Packit df99a1
#ifdef HAVE_NAMESPACES
Packit df99a1
}
Packit df99a1
# ifndef NOT_USING_DJVU_NAMESPACE
Packit df99a1
using namespace DJVU;
Packit df99a1
# endif
Packit df99a1
#endif
Packit df99a1
#endif