exception-abstract.h 3.81 KB
Newer Older
1
2
3
4
5
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * Copyright Projet JRL-Japan, 2007
 *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 *
 * File:      exception-abstract.h
6
 * Project:   DYNAMIC-GRAPH
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 * Author:    Nicolas Mansard
 *
 * Version control
 * ===============
 *
 *  $Id$
 *
 * Description
 * ============
 *
 *
 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/


#ifndef __ABSTRACT_EXCEPTION_H
#define __ABSTRACT_EXCEPTION_H

/* --------------------------------------------------------------------- */
/* --- INCLUDE --------------------------------------------------------- */
/* --------------------------------------------------------------------- */


/* Classes standards. */
#include <iostream>                /* Classe ostream.    */
#include <string>                  /* Classe string.     */
32
#include <dynamic-graph/dynamic-graph-api.h>
33
34
35


// Uncomment this macros to have lines parameter on the throw display
36
// #define DYNAMIC-GRAPH_EXCEPTION_PASSING_PARAM 
37
38
39
40
41
42
43
44
45

/* --------------------------------------------------------------------- */
/* --- CLASS ----------------------------------------------------------- */
/* --------------------------------------------------------------------- */

namespace dynamicgraph {

/* \class ExceptionAbstract
 */
46
class DYNAMICGRAPH_EXPORT ExceptionAbstract : public std::exception
47
48
49
50
51
52
53
54
55
{

 public:

  enum ExceptionEnum
    {
      ABSTRACT = 0
      ,SIGNAL = 100
      ,FACTORY = 200
56
      ,TRACES = 300
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    };

  static const std::string EXCEPTION_NAME;
  virtual const std::string& getExceptionName( void ) const { return EXCEPTION_NAME; }

 protected:
  /** Error code.
   * \sa ErrorCodeEnum */
  int code;

  /**  Error message (can be empty). */
  std::string message;

private:

  /**  forbid the empty constructor (private). */
  ExceptionAbstract( void );
public:

  ExceptionAbstract( const int& code, const std::string & msg = "" );
77
  virtual ~ExceptionAbstract( void ) throw() {}
78
79
80
81
82

  /**  Access to the error code. */
  int getCode (void);

  /** Reference access to the error message (can be empty). */
83
  const std::string& getStringMessage (void) const;
84
85
86
87
88
89

  /** Access to the pointer on the array of  \e char related to the error string.
   * Cannot be  \e NULL.
   */
  const char *getMessage (void);

90
91
92
93
  virtual const char* what() const throw()
  {
    return getStringMessage().c_str();
  }
94
95
96
97
98
  
  /** Print the error structure. */
  DYNAMICGRAPH_EXPORT friend std::ostream & operator << (std::ostream & os,
				     const ExceptionAbstract & err);

99
#ifdef DYNAMICGRAPH_EXCEPTION_PASSING_PARAM 
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
 public:
  class Param
    {
    public:
      static const int BUFFER_SIZE = 80;

      const char * functionPTR;
      char function[ BUFFER_SIZE ];
      int line;
      const char * filePTR;
      char file[ BUFFER_SIZE ];
      bool pointersSet,set;
    public:
      Param( const int& _line, const char * _function, const char * _file );
      Param( void ) : pointersSet(false),set(false) {}
      Param& initCopy( const Param& p );
	
    };

 protected:
  mutable Param p;

  template<class Exc>
    friend const Exc& operator+ ( const ExceptionAbstract::Param& p, const Exc& e )
    { e.p.initCopy(p);   return e;    }
  template<class Exc>
    friend Exc& operator+ ( const ExceptionAbstract::Param& p, Exc& e )
    { e.p.initCopy(p);   return e;    }
128
#endif //#ifdef DYNAMICGRAPH_EXCEPTION_PASSING_PARAM 
129
130
131
132
133
134
135
136
};

} // namespace dynamicgraph

#define DG_RETHROW ( const ExceptionAbstract& err ) { throw err; }



137
#ifdef DYNAMICGRAPH_EXCEPTION_PASSING_PARAM
138
#  define DG_THROW throw ExceptionAbstract::Param(__LINE__,__FUNCTION__,__FILE__) +
139
#else //#ifdef DYNAMICGRAPH_EXCEPTION_PASSING_PARAM 
140
#  define DG_THROW throw
141
#endif //#ifdef DYNAMICGRAPH_EXCEPTION_PASSING_PARAM 
142
143
144
145
146
147
148
149
150


#endif /* #ifndef __ABSTRACT_EXCEPTION_H */

/*
 * Local variables:
 * c-basic-offset: 2
 * End:
 */