except.h

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C头文件 代码 · 共 773 行 · 第 1/2 页

H
773
字号

#ifndef EXCEPT_HPP
#define EXCEPT_HPP
#include <stddef.h>
#include <string.h>
#include <stdio.h>

//----------------------------------------------------------------
// WELCOME TO EHMIX Version 1.00, 5/12/94
//
// An EXPERIMENTAL MIXIN STYLE EXCEPTION CLASS HIERARCHY
//
// Purpose:
//
// This code is primarily intended to familiarise
// people with mixins and develop a useful exception class
// hierarchy with an aim to inclusion in the
// C++ Standard Library.
//
// The class set in the first section should be somewhat
// richer than what is required for the Standard Library.
// We need enough classes so some will be _excluded_.
//
// The test code is primarily intended to demonstrate
// how a mixin EH library might be used "as is" or extended
// by users or implementors.
//
// Arguments about convenience, ease of use, and extensibility
// make more sense with experience (however limited).
//
// The exact structure of the hierarchy including the names of
// the classes requires further consideration. Some of the names
// I have use are deliberately different from the current
// Working Paper names exactly to avoid a clash in this code.
//
// There is an intent to develop some templates to
// make mixins even easier to use, and to provide
// assertional and debugging tools corresponding
// to some of the exceptions. I have not done this
// yet because I do not yet know exactly how :-)
//
// Although it is not the primary purpose, the variety of
// techniques, including nested rethrows, may be useful
// for regression testing compilers. I may add some
// extra stress tests later.
//
// This code is Copyright (C) 1994 by Maxtal P/L, Sydney Australia,
// and is hereby released freely for any use which
// does not restrict such free public availability.
//
//----------------------------------------------------------------
//
// A PLEA:
//
// Unfortunately, some compilers support exceptions
// but do not provide RTTI or dynamic_cast. There are
// ugly pre-processor hacks here to allow these compilers
// to execute most of the code. Unfortunately, this obscures
// the code somewhat. Since this is supposed to be "hands on"
// stuff, please try to see past this hackery at the
// underlying structures.
//
// SUPPORTED COMPILERS (so far)
//
// BORLAND 4.0
// METAWARE HIGH C/C++
// WATCOM 10.0a (soon)
// EDG
//
// If your compiler is not on the list it is because
// I do not have it, and no one else has provided
// a configuration.
//
//
// John Max Skaller
// maxtal@suphys.physics.su.oz.au
//
//----------------------------------------------------------------
// VENDOR CONFIGURATION SECTION
//
// This section is for VENDORS to define their compiler attributes.
//
// I hope to just have a sequence of #if .. #elif ..
// switches here with the bits in the middle supplied
// by each vendor. This should include inner tests for
// supported memory models, etc.
//
// Vendors MUST support "set_unexpected()"
// Dynamic casts and Type_info are optional but prefered.
//
// Portable implementation of the bad_cast exception is not possible
// without explicitly qualified (non-deducible) template functions.
//
// define this switch if your compiler does not have dynamic_cast
#ifndef __WATCOM_RTTI__
#define NODYNCAST
#endif

// select how to delete a const pointer: using a const_cast,
// an old style cast, or no cast
#ifdef __WATCOM_RTTI__
#define DELCONSTCAST
#else
#define DELOLDCAST
#endif
//#define DELNOCAST

// define this switch if your compiler does not have Type_info
#ifndef __WATCOM_RTTI__
#define NORTTI
#endif

// define this switch to configure the malloc test
// for compilers with 64K segment limits

#if defined(__BORLANDC__)
#define SIZE64K
#endif

// VENDOR must configure this section to define
// Type_info and set_unexpected.
// If NORTTI is defined, a replacement mechanism is supplied
// automatically -- do nothing.
// Otherwise you must define the type Type_info here somehow.
//
#if defined(NORTTI)
typedef void (*pfv)();
pfv set_unexpected(pfv p);
#else
#include <typeinfo.h>
#endif

#if defined(__BORLANDC__)
#include <except.h>
#endif

void *getidx( void const *p );

//END VENDOR CONFIGURATION SECTION
//----------------------------------------------------------------

#if defined(NORTTI)
struct Type_info { char const *name()const { return "NO RTTI"; } };
Type_info dummy_type_info;
#define typeid(x) (x)
#endif

#if defined(NODYNCAST)
class exception_backtrace;
class source_reference;
class exception_message;
#endif


// HERE FOR USERS TO FIDDLE
// define this switch to get debug trace of EH
#define EHDEBUG

#if defined(EHDEBUG)
#define dprintf(x) printf x
#else
#define dprintf(x)
#endif



struct strng // dummy string class
{
  char data[512];
  strng(char const *p) { strcpy(data,p); }
};

//namespace MaxTal
//{
//----------------------------------------------------------------------
// ABSTRACTIONS
//
//----------------------------------------------------------------------
// ROOTS

  // exception
  // common root of all standard exceptions

#ifdef __WATCOM_RTTI__
#undef exception
#define exception max_exception
#endif

  struct exception
  {
    #if defined(NORTTI)
    virtual char const *name()const=0;
    #endif

    #if defined(NODYNCAST)
    virtual exception_backtrace const *
      downcast_backtrace() const { return 0; }
    virtual exception_message const *
      downcast_exception_message() const { return 0; }
    virtual source_reference const *
      downcast_source_reference() const { return 0; }
    #endif
    virtual ~exception(){} //=0 !! Too many compilers get this wrong
    virtual exception* clone()const =0; // polymorphic copy
  };

//  inline exception::~exception(){}

  // catastrophic system failure has compromised system
  // integrity. This exception should generally be thrown
  // only in debugging versions of programs

  struct catastrohpe {};

  // an exception has arisen during exception processing
  // for example, memory has been exhausted during copying the
  // object of a throw

  struct double_fault {};

//----------------------------------------------------------------------
// RESOURCE ACQUISITION

  // exception to signal an attempt by the library
  // to acquire a resource from
  // the operating system host has failed
  // this includes memory, file handles, files, etc

  struct resource_acquisition_failure :
    public virtual exception {};

    // a program, task, or function has exceeded its alloted time
    // possibly indicating an infinite loop, deadlock, or
    // other condition. Can also be used for deliberate timeouts.

    struct timeout :
      public virtual resource_acquisition_failure {};

    // exception to signal insufficient or fragmented memory
    // prevents satisfaction of an allocation request

    struct bad_alloc :
      public virtual resource_acquisition_failure
    {
      virtual size_t requested_store() const=0;
        // amount of request if not zero

      virtual size_t available_block() const=0;
        // amount that might have worked if not zero
    };

//----------------------------------------------------------------------
// PRECONDITIONS

  // exception to report that the functional precondition
  // on the arguments of a function has not been met
  //
  // replaces domain_error
  //
  // if a function has several arguments, a precondition
  // is any subset of the cartesian product of the possible valid values
  // of the explicit parameters of the function

  struct precondition_violation :
    public virtual exception {};

    // a pointer argument was null, when a non-null pointer was required

    struct null_pointer_error :
      public virtual precondition_violation {};

    // an arithmetic function or operator has detected
    // the combination of arguments given is invalid

    struct arithmetic_domain_error :
      public virtual precondition_violation {};

      // a single arithmetic argument has a value outside its domain

      struct arithmetic_argument_error :
        public virtual arithmetic_domain_error {};

        // an index is out of the valid domain of a container
        // or array. This class should be extended to
        // provide more (optional) information

        struct index_error :
          public virtual arithmetic_argument_error {};

    // the results of a calculation would not be representable
    // for example the conversion of a long to an int
    // overflow on division, etc.
    //
    // This is a domain error which is a consequence of implementation
    // restrictions rather than program logic -- although the
    // distinction is thin

    struct implementation_limit_exceeded :
      public virtual precondition_violation {};

      // the exponent of a floating number is too large to be
      // represented

      struct floating_overflow :
        public virtual implementation_limit_exceeded,
        public virtual arithmetic_domain_error {};

      // the exponent of a floating number is to small (negative) to be
      // represented

      struct floating_underflow :
        public virtual implementation_limit_exceeded,
        public virtual arithmetic_domain_error {};


      // results of a conversion or integral operation
      // would produce an integer out of the range
      // or a target or computation

      struct integral_overflow :
        public virtual implementation_limit_exceeded,
        public virtual arithmetic_domain_error {};



//----------------------------------------------------------------------
// PROTOCOL VIOLATIONS

  // exception to report that the environment state required for
  // successful establishment of the post conditions of a function
  // have not been met, probably due to a violation of a required
  // dynamic protocol, such as a specific sequence of calls,
  // or dynamic initialisation, deleting an auto variable,
  // deleting a function without using delete [] syntax
  //
  // Protocol violations are violations of _non-functional_
  // preconditions.


  struct protocol_violation :
    public virtual exception {};

    // A resource deadlock is typically a _translated_
    // exception. A timeout on resource acquisition
    // in certain contexts may indicate two tasks await
    // resources held by the other.

    struct resource_deadlock :
      public virtual protocol_violation,
      public virtual resource_acquisition_failure {};

    // to be thrown if a dynamic reference cast is attempted
    // to a type not derived from the source object
#ifdef __WATCOM_RTTI__
typedef type_info Type_info;
#define bad_cast max_bad_cast
#endif

    struct bad_cast :
      protocol_violation {};

      // following optional diagnostic structure may be supported

      struct bad_cast_diagnostic :
        public virtual bad_cast
      {
        virtual Type_info const &typeid_source() const=0;
        virtual Type_info const &typeid_destination() const=0;
      };

    // to be thrown if an implementation detects calls to
    // pure virtual functions (optional)

    struct pure_virtual_called :
      public virtual protocol_violation {};

    // an unhandled exception which violates a function's
    // exception specification has been detected
    //
    // usually this exception will be thrown by
    // a debugging version of the unhandled exception handler

    struct exception_specification_violation :
      public virtual protocol_violation {};

//----------------------------------------------------------------------
// DEBUGGING: INVARIANTS

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?