|
1 //Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc. |
|
2 |
|
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) |
|
5 |
|
6 #ifndef UUID_8D22C4CA9CC811DCAA9133D256D89593 |
|
7 #define UUID_8D22C4CA9CC811DCAA9133D256D89593 |
|
8 |
|
9 #include <boost/exception/exception.hpp> |
|
10 #include <boost/exception/to_string_stub.hpp> |
|
11 #include <boost/exception/detail/error_info_impl.hpp> |
|
12 #include <boost/shared_ptr.hpp> |
|
13 #include <map> |
|
14 |
|
15 namespace |
|
16 boost |
|
17 { |
|
18 template <class Tag,class T> |
|
19 inline |
|
20 typename enable_if<has_to_string<T>,std::string>::type |
|
21 to_string( error_info<Tag,T> const & x ) |
|
22 { |
|
23 return to_string(x.value()); |
|
24 } |
|
25 |
|
26 template <class Tag,class T> |
|
27 inline |
|
28 error_info<Tag,T>:: |
|
29 error_info( value_type const & value ): |
|
30 value_(value) |
|
31 { |
|
32 } |
|
33 |
|
34 template <class Tag,class T> |
|
35 inline |
|
36 error_info<Tag,T>:: |
|
37 ~error_info() throw() |
|
38 { |
|
39 } |
|
40 |
|
41 template <class Tag,class T> |
|
42 inline |
|
43 char const * |
|
44 error_info<Tag,T>:: |
|
45 tag_typeid_name() const |
|
46 { |
|
47 return tag_type_name<Tag>(); |
|
48 } |
|
49 |
|
50 template <class Tag,class T> |
|
51 inline |
|
52 std::string |
|
53 error_info<Tag,T>:: |
|
54 value_as_string() const |
|
55 { |
|
56 return to_string_stub(*this); |
|
57 } |
|
58 |
|
59 namespace |
|
60 exception_detail |
|
61 { |
|
62 class |
|
63 error_info_container_impl: |
|
64 public error_info_container |
|
65 { |
|
66 public: |
|
67 |
|
68 error_info_container_impl(): |
|
69 count_(0) |
|
70 { |
|
71 } |
|
72 |
|
73 ~error_info_container_impl() throw() |
|
74 { |
|
75 } |
|
76 |
|
77 void |
|
78 set( shared_ptr<error_info_base const> const & x, type_info_ const & typeid_ ) |
|
79 { |
|
80 BOOST_ASSERT(x); |
|
81 info_[typeid_] = x; |
|
82 diagnostic_info_str_.clear(); |
|
83 } |
|
84 |
|
85 shared_ptr<error_info_base const> |
|
86 get( type_info_ const & ti ) const |
|
87 { |
|
88 error_info_map::const_iterator i=info_.find(ti); |
|
89 if( info_.end()!=i ) |
|
90 { |
|
91 shared_ptr<error_info_base const> const & p = i->second; |
|
92 #ifndef BOOST_NO_RTTI |
|
93 BOOST_ASSERT( BOOST_EXCEPTION_DYNAMIC_TYPEID(*p)==ti ); |
|
94 #endif |
|
95 return p; |
|
96 } |
|
97 return shared_ptr<error_info_base const>(); |
|
98 } |
|
99 |
|
100 char const * |
|
101 diagnostic_information() const |
|
102 { |
|
103 if( diagnostic_info_str_.empty() ) |
|
104 { |
|
105 std::ostringstream tmp; |
|
106 for( error_info_map::const_iterator i=info_.begin(),end=info_.end(); i!=end; ++i ) |
|
107 { |
|
108 shared_ptr<error_info_base const> const & x = i->second; |
|
109 tmp << '[' << x->tag_typeid_name() << "] = " << x->value_as_string() << std::endl; |
|
110 } |
|
111 tmp.str().swap(diagnostic_info_str_); |
|
112 } |
|
113 return diagnostic_info_str_.c_str(); |
|
114 } |
|
115 |
|
116 private: |
|
117 |
|
118 friend class boost::exception; |
|
119 |
|
120 typedef std::map< type_info_, shared_ptr<error_info_base const> > error_info_map; |
|
121 error_info_map info_; |
|
122 mutable std::string diagnostic_info_str_; |
|
123 mutable int count_; |
|
124 |
|
125 void |
|
126 add_ref() const |
|
127 { |
|
128 ++count_; |
|
129 } |
|
130 |
|
131 void |
|
132 release() const |
|
133 { |
|
134 if( !--count_ ) |
|
135 delete this; |
|
136 } |
|
137 }; |
|
138 } |
|
139 |
|
140 template <class E,class Tag,class T> |
|
141 inline |
|
142 E const & |
|
143 operator<<( E const & x, error_info<Tag,T> const & v ) |
|
144 { |
|
145 typedef error_info<Tag,T> error_info_tag_t; |
|
146 shared_ptr<error_info_tag_t> p( new error_info_tag_t(v) ); |
|
147 exception_detail::error_info_container * c; |
|
148 if( !(c=x.data_.get()) ) |
|
149 x.data_.adopt(c=new exception_detail::error_info_container_impl); |
|
150 c->set(p,BOOST_EXCEPTION_STATIC_TYPEID(error_info_tag_t)); |
|
151 return x; |
|
152 } |
|
153 } |
|
154 |
|
155 #endif |