1 //Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.
3 //Distributed under the Boost Software License, Version 1.0. (See accompanying
4 //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef UUID_8D22C4CA9CC811DCAA9133D256D89593
7 #define UUID_8D22C4CA9CC811DCAA9133D256D89593
9 #include <boost/type.hpp>
10 #include <boost/exception/exception.hpp>
11 #include <boost/exception/error_info.hpp>
12 #include <boost/exception/to_string_stub.hpp>
13 #include <boost/current_function.hpp>
14 #include <boost/shared_ptr.hpp>
17 #define BOOST_ERROR_INFO\
18 ::boost::throw_function(BOOST_CURRENT_FUNCTION) <<\
19 ::boost::throw_file(__FILE__) <<\
20 ::boost::throw_line((int)__LINE__)
25 typedef error_info<struct tag_throw_function,char const *> throw_function;
26 typedef error_info<struct tag_throw_file,char const *> throw_file;
27 typedef error_info<struct tag_throw_line,int> throw_line;
37 virtual std::type_info const & tag_typeid() const = 0;
38 virtual std::string value_as_string() const = 0;
42 #if BOOST_WORKAROUND( __GNUC__, BOOST_TESTED_AT(4) )
43 virtual //Disable bogus GCC warning.
51 template <class Tag,class T>
54 public exception_detail::error_info_base
60 error_info( value_type const & value ):
73 std::type_info const &
76 return typeid(type<Tag>);
80 value_as_string() const
82 return to_string_stub(value_);
85 value_type const value_;
88 template <class E,class Tag,class T>
91 operator<<( E const & x, error_info<Tag,T> const & v )
93 shared_ptr< error_info<Tag,T> > p( new error_info<Tag,T>(v) );
98 template <class ErrorInfo,class E>
100 shared_ptr<typename ErrorInfo::value_type const>
101 get_error_info( E const & some_exception )
103 if( exception const * x = dynamic_cast<exception const *>(&some_exception) )
104 if( shared_ptr<exception_detail::error_info_base const> eib = x->get(typeid(ErrorInfo)) )
106 BOOST_ASSERT( 0!=dynamic_cast<ErrorInfo const *>(eib.get()) );
107 ErrorInfo const * w = static_cast<ErrorInfo const *>(eib.get());
108 return shared_ptr<typename ErrorInfo::value_type const>(eib,&w->value());
110 return shared_ptr<typename ErrorInfo::value_type const>();
117 error_info_container_impl:
118 public error_info_container
122 error_info_container_impl():
127 ~error_info_container_impl() throw()
131 shared_ptr<error_info_base const>
132 get( std::type_info const & ti ) const
134 error_info_map::const_iterator i=info_.find(typeinfo(ti));
137 shared_ptr<error_info_base const> const & p = i->second;
138 BOOST_ASSERT( typeid(*p)==ti );
141 return shared_ptr<error_info_base const>();
145 set( shared_ptr<error_info_base const> const & x )
148 info_[typeinfo(typeid(*x))] = x;
153 diagnostic_information( char const * std_what, std::type_info const & exception_type ) const
163 tmp += "Dynamic exception type: ";
164 tmp += exception_type.name();
166 for( error_info_map::const_iterator i=info_.begin(),end=info_.end(); i!=end; ++i )
168 shared_ptr<error_info_base const> const & x = i->second;
170 tmp += x->tag_typeid().name();
172 tmp += x->value_as_string();
177 return what_.c_str();
182 friend class exception;
187 std::type_info const * type;
190 typeinfo( std::type_info const & t ):
196 operator<( typeinfo const & b ) const
198 return 0!=(type->before(*b.type));
202 typedef std::map< typeinfo, shared_ptr<error_info_base const> > error_info_map;
203 error_info_map info_;
204 std::string mutable what_;
225 set( shared_ptr<exception_detail::error_info_base const> const & x ) const
228 data_ = intrusive_ptr<exception_detail::error_info_container>(new exception_detail::error_info_container_impl);
233 shared_ptr<exception_detail::error_info_base const>
235 get( std::type_info const & ti ) const
238 return data_->get(ti);
240 return shared_ptr<exception_detail::error_info_base const>();