Rcpp Version 0.10.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
exceptions.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // exceptions.h: Rcpp R/C++ interface class library -- exceptions
4 //
5 // Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
6 //
7 // This file is part of Rcpp.
8 //
9 // Rcpp is free software: you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Rcpp is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21 
22 #ifndef Rcpp__exceptions__h
23 #define Rcpp__exceptions__h
24 
25 #define R_NO_REMAP
26 #include <Rinternals.h>
27 #include <sstream>
28 
29 SEXP stack_trace( const char *file, int line) ;
30 #define GET_STACKTRACE() stack_trace( __FILE__, __LINE__ )
31 
32 namespace Rcpp{
33 
34 class exception : public std::exception {
35 public:
36  explicit exception(const char* message_) ;
37  exception(const char* message_, const char* file, int line ) ;
38  virtual ~exception() throw() ;
39  virtual const char* what() const throw() ;
40 private:
41  std::string message ;
42 } ;
43 
44 // simple helper
45 static std::string toString(const int i) {
46  std::ostringstream ostr;
47  ostr << i;
48  return ostr.str();
49 }
50 
51 class no_such_env : public std::exception{
52 public:
53  no_such_env( const std::string& name ) throw() : message( std::string("no such environment: '") + name + "'" ){} ;
54  no_such_env( int pos ) throw() : message( "no environment in given position '" + toString(pos) + "'") {} ;
55  virtual ~no_such_env() throw(){} ;
56  virtual const char* what() const throw(){ return message.c_str() ; } ;
57 private:
58  std::string message ;
59 } ;
60 
61 class file_io_error : public std::exception {
62 public:
63  file_io_error(const std::string& file) throw() : message( std::string("file io error: '") + file + "'" ), file(file) {} ;
64  file_io_error(int code, const std::string& file) throw() : message( "file io error " + toString(code) + ": '" + file + "'"), file(file) {} ;
65  file_io_error(const std::string& msg, const std::string& file) throw() : message( msg + ": '" + file + "'"), file(file) {} ;
66  virtual ~file_io_error() throw(){} ;
67  virtual const char* what() const throw(){ return message.c_str() ; } ;
68  std::string filePath() const throw(){ return file ; } ;
69 private:
70  std::string message ;
71  std::string file;
72 } ;
73 
74 class file_not_found : public file_io_error {
75 public:
76  file_not_found(const std::string& file) throw() : file_io_error("file not found", file) {}
77 };
78 
79 class file_exists : public file_io_error {
80 public:
81  file_exists(const std::string& file) throw() : file_io_error("file already exists", file) {}
82 };
83 
84 #define RCPP_EXCEPTION_CLASS(__CLASS__,__WHAT__) \
85 class __CLASS__ : public std::exception{ \
86 public: \
87  __CLASS__( const std::string& message ) throw() : message( __WHAT__ ){} ; \
88  virtual ~__CLASS__() throw(){} ; \
89  virtual const char* what() const throw() ; \
90 private: \
91  std::string message ; \
92 } ;
93 
94 #define RCPP_SIMPLE_EXCEPTION_CLASS(__CLASS__,__MESSAGE__) \
95 class __CLASS__ : public std::exception{ \
96 public: \
97  __CLASS__() throw() {} ; \
98  virtual ~__CLASS__() throw(){} ; \
99  virtual const char* what() const throw() ; \
100 } ;
101 
102 RCPP_SIMPLE_EXCEPTION_CLASS(not_a_matrix, "not a matrix")
103 RCPP_SIMPLE_EXCEPTION_CLASS(index_out_of_bounds, "index out of bounds")
104 RCPP_SIMPLE_EXCEPTION_CLASS(parse_error, "parse error")
105 RCPP_SIMPLE_EXCEPTION_CLASS(not_s4, "not an S4 object")
106 RCPP_SIMPLE_EXCEPTION_CLASS(not_reference, "not an S4 object of a reference class")
107 RCPP_SIMPLE_EXCEPTION_CLASS(not_initialized, "C++ object not initialized (missing default constructor?)")
108 RCPP_SIMPLE_EXCEPTION_CLASS(no_such_slot, "no such slot")
109 RCPP_SIMPLE_EXCEPTION_CLASS(no_such_field, "no such field")
110 RCPP_SIMPLE_EXCEPTION_CLASS(not_a_closure, "not a closure")
111 RCPP_SIMPLE_EXCEPTION_CLASS(no_such_function, "no such function")
112 RCPP_SIMPLE_EXCEPTION_CLASS(unevaluated_promise, "promise not yet evaluated")
113 
114 RCPP_EXCEPTION_CLASS(not_compatible, message )
115 RCPP_EXCEPTION_CLASS(S4_creation_error, std::string("error creating object of S4 class : ") + message )
116 RCPP_EXCEPTION_CLASS(reference_creation_error, std::string("error creating object of reference class : ") + message )
117 RCPP_EXCEPTION_CLASS(no_such_binding, std::string("no such binding : '") + message + "'" )
118 RCPP_EXCEPTION_CLASS(binding_not_found, std::string("binding not found: '") + message + "'" )
119 RCPP_EXCEPTION_CLASS(binding_is_locked, std::string("binding is locked: '") + message + "'" )
120 RCPP_EXCEPTION_CLASS(no_such_namespace, std::string("no such namespace: '") + message + "'" )
121 RCPP_EXCEPTION_CLASS(function_not_exported, std::string("function not exported: ") + message)
122 RCPP_EXCEPTION_CLASS(eval_error, message )
123 
124 #undef RCPP_EXCEPTION_CLASS
125 #undef RCPP_SIMPLE_EXCEPTION_CLASS
126 
127 } // namesapce Rcpp
128 
129 void forward_exception_to_r( const std::exception& ) ;
130 SEXP exception_to_try_error( const std::exception& ) ;
131 SEXP exception_to_r_condition( const std::exception& ) ;
132 SEXP string_to_try_error( const std::string& ) ;
133 
134 std::string demangle( const std::string& name) ;
135 #define DEMANGLE(__TYPE__) demangle( typeid(__TYPE__).name() ).c_str()
136 
137 namespace Rcpp{
138  /* internal namespace for things not intended to be used by the user */
139  namespace internal{
140 
141  SEXP try_catch( SEXP expr, SEXP env );
142  SEXP try_catch( SEXP expr );
143 
144  } // namespace internal
145 } // namespace Rcpp
146 
147 namespace Rcpp {
148  inline void stop(const std::string& message) { throw Rcpp::exception(message.c_str()); }
149 } // namespace Rcpp
150 
151 #endif