//-*-c++-*--------------------------------------------------------------
//  rwexcept.cpp
//   Source file for the rwExceptionHandler class.
//
//      Unpublished Confidential Information of Hewlett-Packard
//      Company. Do not disclose.  Copyright (c) Hewlett-Packard
//      Company 1995. All rights reserved
// 
// The contents of this software are proprietary and confidential to
// the Hewlett- Packard Company, and are limited in distribution to
// those with a direct need to know.  Individuals having access to
// this software are responsible for maintaining the confidentiality
// of the content and for keeping the software secure when not in use.
// Transfer to any party is strictly forbidden other than as expressly
// permitted in writing by Hewlett-Packard Company.  Unauthorized
// transfer to or possession by any unauthorized party may be a
// criminal offense.
//
//                  RESTRICTED RIGHTS LEGEND
//
//      Use,  duplication,  or disclosure by the U. S. Government is
//      subject to restrictions as set forth in subparagraph (c) (1)
//      (ii) in the Rights in Technical Data and Computer Software
//      clause in DFARS 252.227-7013 or any other successor clause.
//
//                      HEWLETT-PACKARD COMPANY
//                      3000 Hanover St.
//                      Palo Alto, CA  94304 U.S.A.
//
//      Rights for non-DOD U.S. Government Departments and Agencies
//      are as set forth in FAR 52.227-19 (c) (1,2) or any other
//      successor clause.
//
//  CLASSES:
//   rwExceptionHandler
//  
//----------------------------------------------------------------------
//
// $Source: /projects/HMS/nsr/udfCore/sys/rwexcept.cpp,v $
// $Revision: 1.3 $     $Author: hmgr $
// $State: Exp $        $Locker:  $
// $Date: 96/06/13 16:47:27 $
//
//-----------------------------------------------------------------------------

#include <stdio.h>
#include "rwexcept.h"

//-----------------------------------------------------------------------------
// Class static variable declarations
//-----------------------------------------------------------------------------

// void         (rwExceptionHandler::*oldHandler_)(const RWxmsg&) = NULL;
oldRWHandler    rwExceptionHandler::oldHandler_ = NULL;
int             rwExceptionHandler::caughtException_ = 0;
int             rwExceptionHandler::references_ = 0;
RWCString       rwExceptionHandler::theMessage_;

//-----------------------------------------------------------------------------
// METHOD:  rwExceptionHandler::rwExceptionHandler()
//
// Constructor.  If oldHandler_ is non-NULL, the RogueWave library function
// rwSetErrHandler is called to intercept the generic exception handler, and
// replaces it with the member function handler().
//
// ARGUMENTS:  None.
//
// RETURNS:  None.
//-----------------------------------------------------------------------------

rwExceptionHandler::rwExceptionHandler
(
    void
)
{
    if(oldHandler_ == NULL)
    {
        oldHandler_ = rwSetErrHandler(handler);
    }
    references_++;
}

//-----------------------------------------------------------------------------
// METHOD:  rwExceptionHandler::~rwExceptionHandler()
//
// Destructor.  Decrements the reference count; if it becomes zero, then the
// old RogueWave handler is installed.
//
// ARGUMENTS:  None.
//
// RETURNS:  None.
//-----------------------------------------------------------------------------

rwExceptionHandler::~rwExceptionHandler
(
    void
)
{
    references_--;
    if(references_ == 0)
    {
        rwSetErrHandler(oldHandler_);
        oldHandler_ = NULL;
        caughtException_ = 0;
    }
}

//-----------------------------------------------------------------------------
// METHOD:  rwExceptionHandler::handler()
//
// Exception handler.  
//
// ARGUMENTS:  
//
// const RWxmsg&   msg  IN  Message describing type of exception
//
// RETURNS:  None.
//-----------------------------------------------------------------------------

void
rwExceptionHandler::handler
(
    const RWxmsg& msg
)
{
    caughtException_ = 1;
    theMessage_ = msg.why();
}
