Changeset 3585 in Sophya for trunk/SophyaLib/BaseTools


Ignore:
Timestamp:
Mar 5, 2009, 2:24:17 PM (17 years ago)
Author:
ansari
Message:

Ajout heritage exceptions SOPHYA de std::exception (+method what()) - nettoyage/suppression TRY/CATCH... , Reza 05/03/2009

Location:
trunk/SophyaLib/BaseTools
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/BaseTools/pexceptions.cc

    r2615 r3585  
    33#include <iostream>
    44#include <stdio.h>
     5#include <string.h>
    56
    6 void PFailHandler(void);
    7 
     7/* --- Nettoyage par Reza, Mars 2009
    88// egcs ne semble pas connaitre set_new_handler (Reza 26/04/99)
    99// ca ne passe plus avec SGI-CC -LANG:std $CHECK$ Reza 14/02/2003
    10 #if defined( __GNUG__ ) || defined(__SGICC__)
    11 void PFailHandler(void) {}
    12 void InitFailNewHandler() {}
    13 #else
    14 
    1510void PFailHandler(void)
    1611{
     
    1914  throw(AllocationError("new"));
    2015}
    21 
    2216void InitFailNewHandler()
    2317{
    2418   set_new_handler(PFailHandler);
    2519}
    26 #endif
     20--- FIN nettoyage */
    2721
    28 string SOPHYA::BuildLongExceptionMessage(const char * s, const char *file, int line)
     22
     23namespace SOPHYA {
     24
     25/*!
     26  \ingroup BaseTools
     27  \brief Utility function for appending a file name and line number to a message
     28
     29  In practice, the macro \b PExcLongMessage(a) can be used to append file name
     30  and line number to the message a.
     31*/
     32
     33string BuildLongExceptionMessage(const char * s, const char *file, int line)
    2934{
    3035  char buff[32];
     
    3439  return(rs);
    3540}
     41
     42/*!
     43    \class PThrowable
     44    \ingroup BaseTools
     45    \brief Base exception class in Sophya, inherits from std::exception
     46
     47    This class is the ancestor class for PError and PException classes which are
     48    the base classes for all exceptions used in SOPHYA. The PThrowable class inherits
     49    from the standard c++ exception base class, std::exception and implements the what()
     50    method. A message (string/char*) passed to the constructor is kept in the exception object,
     51    and can be retrieved  using the \b what() method or \b Msg() method. An integer identifier
     52    can also be associated with the exception objet and retrieved by \b Id().
     53    The message passed to the constructor is truncated to a maximum length defined by
     54    SEXC_MAXMSGLEN (=160 characters).
     55*/
     56
     57PThrowable::PThrowable(const string& m, int ident) throw()
     58{
     59  id_ = ident;
     60  strncpy(msg_, m.c_str(), SEXC_MAXMSGLEN-1);
     61  msg_[SEXC_MAXMSGLEN-1] = '\0';
     62}
     63
     64PThrowable::PThrowable(const char* m, int ident) throw()
     65{
     66  id_ = ident;
     67  if (m!=NULL) {
     68    strncpy(msg_, m, SEXC_MAXMSGLEN-1);
     69    msg_[SEXC_MAXMSGLEN-1] = '\0';
     70  }
     71  else msg_[0] = '\0';
     72}
     73
     74PThrowable::~PThrowable() throw()
     75{
     76}
     77const char* PThrowable::what() const throw()
     78{
     79  return msg_;
     80}
     81
     82string const PThrowable::Msg() const
     83{
     84  return (string(msg_));
     85}
     86
     87int PThrowable::Id() const
     88{
     89  return id_;
     90}
     91
     92} // namespace SOPHYA
  • trunk/SophyaLib/BaseTools/pexceptions.h

    r2505 r3585  
    1010// Petit utilitaire pour accoler numero de ligne et nom de fichier aux messages
    1111// d'exception
     12//! Utiliy macro to append file name and line number to a const char* string 
    1213#define PExcLongMessage(a) BuildLongExceptionMessage(a,__FILE__,__LINE__)
    1314
     15#define SEXC_MAXMSGLEN 160
    1416namespace SOPHYA {
    1517
     
    1719  string BuildLongExceptionMessage(const char * s, const char *file, int line);
    1820
    19 /*! \ingroup BaseTools
    20     \brief Base exception class in Sophya
    21 
    22     Ancestor for PError and PException
    23     It has a message, and an id to give more
    24     information on the exception.
    25 */
    26   class PThrowable {
     21  class PThrowable : public std::exception {
    2722  public:
    2823    //! Constructor with the message and error-id (optional) specification
    29     explicit PThrowable(const string& m, int ident=0)
    30       : msg(m), id(ident) {}
    31     explicit PThrowable(const char* m, int ident=0)
    32       : msg(m), id(ident) {}
    33     virtual ~PThrowable() { }
     24    explicit PThrowable(const string& m, int ident=0) throw() ;
     25    explicit PThrowable(const char* m, int ident=0) throw() ;
     26    virtual ~PThrowable() throw();
     27    //! Implementation of std::exception what() method, returning the exception message
     28    virtual const char* what() const throw();
    3429    //! Returns the associated message string
    35     virtual string const& Msg() const  {return msg;}
     30    virtual string const Msg() const;
    3631    //! Returns the associated error-id
    37     virtual int Id() const {return id; }
     32    virtual int Id() const;
    3833  private:
    39     string msg;
    40     int    id;
     34    char msg_[SEXC_MAXMSGLEN];
     35    int    id_;
    4136  };
    4237
     
    4944  class PError : public PThrowable {
    5045  public:
    51     explicit PError(const string& m, int id=0) : PThrowable(m,id) {}
    52     explicit PError(const char* m, int id=0) : PThrowable(m,id) {}
     46    explicit PError(const string& m, int id=0) throw() : PThrowable(m,id) {}
     47    explicit PError(const char* m, int id=0) throw() : PThrowable(m,id) {}
    5348  };
    5449 
     
    5752  class PException : public PThrowable {
    5853  public:
    59     explicit PException(const string& m, int id=0) : PThrowable(m,id) {}
     54    explicit PException(const string& m, int id=0) throw() : PThrowable(m,id) {}
     55    explicit PException(const char* m, int id=0) throw() : PThrowable(m,id) {}
    6056  };
    6157 
     
    7167  class AllocationError : public PError {
    7268  public:
    73     explicit AllocationError(const string& m, int id=0) : PError(m,id) {}
    74     explicit AllocationError(const char* m, int id=0) : PError(m,id) {}
     69    explicit AllocationError(const string& m, int id=0) throw() : PError(m,id) {}
     70    explicit AllocationError(const char* m, int id=0) throw() : PError(m,id) {}
    7571  };
    7672 
     
    7975  class NullPtrError : public PError {
    8076  public:
    81     explicit NullPtrError(const string& m, int id=0) : PError(m,id) {}
    82     explicit NullPtrError(const char* m, int id=0) : PError(m,id) {}
     77    explicit NullPtrError(const string& m, int id=0) throw() : PError(m,id) {}
     78    explicit NullPtrError(const char* m, int id=0) throw() : PError(m,id) {}
    8379  };
    8480 
     
    8884  class ForbiddenError : public PError {
    8985  public:
    90     explicit ForbiddenError(const string& m, int id=0) : PError(m,id) {}
    91     explicit ForbiddenError(const char* m, int id=0) : PError(m,id) {}
     86    explicit ForbiddenError(const string& m, int id=0) throw() : PError(m,id) {}
     87    explicit ForbiddenError(const char* m, int id=0) throw() : PError(m,id) {}
    9288  };
    9389
     
    9793  class AssertionFailedError : public PError {
    9894  public:
    99     explicit AssertionFailedError(const string& m, int id=0) : PError(m,id) {}
    100     explicit AssertionFailedError(const char* m, int id=0) : PError(m,id) {}
     95    explicit AssertionFailedError(const string& m, int id=0) throw() : PError(m,id) {}
     96    explicit AssertionFailedError(const char* m, int id=0) throw() : PError(m,id) {}
    10197  };
    10298 
     
    120116  class IOExc : public PException {
    121117  public:
    122     explicit IOExc(const string& m, int id=0) : PException(m,id) {}
    123     explicit IOExc(const char* m, int id=0) : PException(m,id) {}
     118    explicit IOExc(const string& m, int id=0) throw() : PException(m,id) {}
     119    explicit IOExc(const char* m, int id=0) throw() : PException(m,id) {}
    124120  };
    125121
     
    128124  class FileFormatExc : public IOExc {
    129125  public:
    130     explicit FileFormatExc(const string& m, int id=0) : IOExc(m,id) {}
    131     explicit FileFormatExc(const char* m, int id=0) : IOExc(m,id) {}
     126    explicit FileFormatExc(const string& m, int id=0) throw() : IOExc(m,id) {}
     127    explicit FileFormatExc(const char* m, int id=0) throw() : IOExc(m,id) {}
    132128  };
    133129
     
    136132  class SzMismatchError : public PException {
    137133  public:
    138     explicit SzMismatchError(const string& m, int id=0) : PException(m,id) {}
    139     explicit SzMismatchError(const char* m, int id=0) : PException(m,id) {}
     134    explicit SzMismatchError(const string& m, int id=0) throw() : PException(m,id) {}
     135    explicit SzMismatchError(const char* m, int id=0) throw() : PException(m,id) {}
    140136  };
    141137 
     
    144140  class RangeCheckError : public PException {
    145141  public:
    146     explicit RangeCheckError(const string& m, int id=0) : PException(m,id) {}
    147     explicit RangeCheckError(const char* m, int id=0) : PException(m,id) {}
     142    explicit RangeCheckError(const string& m, int id=0) throw() : PException(m,id) {}
     143    explicit RangeCheckError(const char* m, int id=0) throw() : PException(m,id) {}
    148144  };
    149145 
     
    152148  class ParmError : public PException {
    153149  public:
    154     explicit ParmError(const string& m, int id=0) : PException(m,id) {}
    155     explicit ParmError(const char* m, int id=0) : PException(m,id) {}
     150    explicit ParmError(const string& m, int id=0) throw() : PException(m,id) {}
     151    explicit ParmError(const char* m, int id=0) throw() : PException(m,id) {}
    156152  };
    157153
     
    160156  class NotAvailableOperation : public PException {
    161157  public:
    162     explicit NotAvailableOperation(const string& m, int id=0) : PException(m,id) {}
    163     explicit NotAvailableOperation(const char* m, int id=0) : PException(m,id) {}
     158    explicit NotAvailableOperation(const string& m, int id=0) throw() : PException(m,id) {}
     159    explicit NotAvailableOperation(const char* m, int id=0) throw() : PException(m,id) {}
    164160  };
    165161
     
    168164  class TypeMismatchExc : public PException {
    169165  public:
    170     explicit TypeMismatchExc(const string& m, int id=0) : PException(m,id) {}
    171     explicit TypeMismatchExc(const char* m, int id=0) : PException(m,id) {}
     166    explicit TypeMismatchExc(const string& m, int id=0) throw() : PException(m,id) {}
     167    explicit TypeMismatchExc(const char* m, int id=0) throw() : PException(m,id) {}
    172168  };
    173169
     
    176172  class MathExc : public PException {
    177173  public:
    178     explicit MathExc(const string& m, int id=0) : PException(m,id) {}
    179     explicit MathExc(const char* m, int id=0) : PException(m,id) {}
     174    explicit MathExc(const string& m, int id=0) throw() : PException(m,id) {}
     175    explicit MathExc(const char* m, int id=0) throw() : PException(m,id) {}
    180176  };
    181177 
     
    184180  class SingMatrixExc : public MathExc {
    185181  public:
    186     explicit SingMatrixExc(const string& m, int id=0) : MathExc(m,id) {}
    187     explicit SingMatrixExc(const char* m, int id=0) : MathExc(m,id) {}
     182    explicit SingMatrixExc(const string& m, int id=0) throw() : MathExc(m,id) {}
     183    explicit SingMatrixExc(const char* m, int id=0) throw() : MathExc(m,id) {}
    188184  };
    189185
     
    192188  class DuplicateIdExc : public PException {
    193189  public:
    194     explicit DuplicateIdExc(const string& m, int id=0) : PException(m,id) {}
    195     explicit DuplicateIdExc(const char* m, int id=0) : PException(m,id) {}
     190    explicit DuplicateIdExc(const string& m, int id=0) throw() : PException(m,id) {}
     191    explicit DuplicateIdExc(const char* m, int id=0) throw() : PException(m,id) {}
    196192  };
    197193 
     
    200196  class NotFoundExc : public PException {
    201197  public:
    202     explicit NotFoundExc(const string& m, int id=0) : PException(m,id) {}
    203     explicit NotFoundExc(const char* m, int id=0) : PException(m,id) {}
     198    explicit NotFoundExc(const string& m, int id=0) throw() : PException(m,id) {}
     199    explicit NotFoundExc(const char* m, int id=0) throw() : PException(m,id) {}
    204200  };
    205201 
     
    208204  class CaughtSignalExc : public PException {
    209205  public:
    210     explicit CaughtSignalExc(const string& m, int id=0) : PException(m,id) {}
    211     explicit CaughtSignalExc(const char* m, int id=0) : PException(m,id) {}
     206    explicit CaughtSignalExc(const string& m, int id=0) throw() : PException(m,id) {}
     207    explicit CaughtSignalExc(const char* m, int id=0) throw() : PException(m,id) {}
    212208 };
    213209   
     
    223219       {if (!(_x_)) throw(NullPtrError(#_x_))}
    224220
    225 void InitFailNewHandler();
    226  
     221
    227222     
    228 // Compatibility with EROS-PEIDA exceptions
    229 
    230 
    231 #define TRY try
    232 #define CATCH(_var) catch(long _var)
    233 #define CATCHALL catch(...)
    234 #define ENDTRY
    235 
    236 #define THROW(_i) throw( _i);
    237 
    238 #define THROW_ THROW(0)
    239 
    240 #define THROW_SAME throw;
    241 
    242 
    243 #define DBASSERT(_x_) ASSERT(_x_)
    244 
    245223#endif
  • trunk/SophyaLib/BaseTools/sophyainit.cc

    r3572 r3585  
    6161  if (FgInit > 1)  return;
    6262
    63   InitFailNewHandler();
    6463  ModListP = new map<string, double>;
    6564 
Note: See TracChangeset for help on using the changeset viewer.