]> git.lyx.org Git - lyx.git/blob - boost/boost/exception/info.hpp
update boost to version 1.36
[lyx.git] / boost / boost / exception / info.hpp
1 //Copyright (c) 2006-2008 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/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>
15 #include <map>
16
17 #define BOOST_ERROR_INFO\
18     ::boost::throw_function(BOOST_CURRENT_FUNCTION) <<\
19     ::boost::throw_file(__FILE__) <<\
20     ::boost::throw_line((int)__LINE__)
21
22 namespace
23 boost
24     {
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;
28
29     namespace
30     exception_detail
31         {
32         class
33         error_info_base
34             {
35             public:
36
37             virtual std::type_info const & tag_typeid() const = 0;
38             virtual std::string value_as_string() const = 0;
39
40             protected:
41
42 #if BOOST_WORKAROUND( __GNUC__, BOOST_TESTED_AT(4) )
43 virtual //Disable bogus GCC warning.
44 #endif
45             ~error_info_base()
46                 {
47                 }
48             };
49         }
50
51     template <class Tag,class T>
52     class
53     error_info:
54         public exception_detail::error_info_base
55         {
56         public:
57
58         typedef T value_type;
59
60         error_info( value_type const & value ):
61             value_(value)
62             {
63             }
64
65         value_type const &
66         value() const
67             {
68             return value_;
69             }
70
71         private:
72
73         std::type_info const &
74         tag_typeid() const
75             {
76             return typeid(type<Tag>);
77             }
78
79         std::string
80         value_as_string() const
81             {
82             return to_string_stub(value_);
83             }
84
85         value_type const value_;
86         };
87
88     template <class E,class Tag,class T>
89     inline
90     E const &
91     operator<<( E const & x, error_info<Tag,T> const & v )
92         {
93         shared_ptr< error_info<Tag,T> > p( new error_info<Tag,T>(v) );
94         x.set(p);
95         return x;
96         }
97
98     template <class ErrorInfo,class E>
99     inline
100     shared_ptr<typename ErrorInfo::value_type const>
101     get_error_info( E const & some_exception )
102         {
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)) )
105                 {
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());
109                 }
110         return shared_ptr<typename ErrorInfo::value_type const>();
111         }
112
113     namespace
114     exception_detail
115         {
116         class
117         error_info_container_impl:
118             public error_info_container
119             {
120             public:
121
122             error_info_container_impl():
123                 count_(0)
124                 {
125                 }
126
127             ~error_info_container_impl() throw()
128                 {
129                 }
130
131             shared_ptr<error_info_base const>
132             get( std::type_info const & ti ) const
133                 {
134                 error_info_map::const_iterator i=info_.find(typeinfo(ti));
135                 if( info_.end()!=i )
136                     {
137                     shared_ptr<error_info_base const> const & p = i->second;
138                     BOOST_ASSERT( typeid(*p)==ti );
139                     return p;
140                     }
141                 return shared_ptr<error_info_base const>();
142                 }
143
144             void
145             set( shared_ptr<error_info_base const> const & x )
146                 {
147                 BOOST_ASSERT(x);
148                 info_[typeinfo(typeid(*x))] = x;
149                 what_.clear();
150                 }
151
152             char const *
153             diagnostic_information( char const * std_what, std::type_info const & exception_type ) const
154                 {
155                 if( what_.empty() )
156                     {
157                     std::string tmp;
158                     if( std_what )
159                         {
160                         tmp += std_what;
161                         tmp += '\n';
162                         }
163                     tmp += "Dynamic exception type: ";
164                     tmp += exception_type.name();
165                     tmp += '\n';
166                     for( error_info_map::const_iterator i=info_.begin(),end=info_.end(); i!=end; ++i )
167                         {
168                         shared_ptr<error_info_base const> const & x = i->second;
169                         tmp += '[';
170                         tmp += x->tag_typeid().name();
171                         tmp += "] = ";
172                         tmp += x->value_as_string();
173                         tmp += '\n';
174                         }
175                     what_.swap(tmp);
176                     }
177                 return what_.c_str();
178                 }
179
180             private:
181
182             friend class exception;
183
184             struct
185             typeinfo
186                 {
187                 std::type_info const * type;
188
189                 explicit
190                 typeinfo( std::type_info const & t ):
191                     type(&t)
192                     {
193                     }
194
195                 bool
196                 operator<( typeinfo const & b ) const
197                     {
198                     return 0!=(type->before(*b.type));
199                     }
200                 };
201
202             typedef std::map< typeinfo, shared_ptr<error_info_base const> > error_info_map;
203             error_info_map info_;
204             std::string mutable what_;
205             int mutable count_;
206
207             void
208             add_ref() const
209                 {
210                 ++count_;
211                 }
212
213             void
214             release() const
215                 {
216                 if( !--count_ )
217                     delete this;
218                 }
219             };
220         }
221
222     inline
223     void
224     exception::
225     set( shared_ptr<exception_detail::error_info_base const> const & x ) const
226         {
227         if( !data_ )
228             data_ = intrusive_ptr<exception_detail::error_info_container>(new exception_detail::error_info_container_impl);
229         data_->set(x);
230         }
231
232     inline
233     shared_ptr<exception_detail::error_info_base const>
234     exception::
235     get( std::type_info const & ti ) const
236         {
237         if( data_ )
238             return data_->get(ti);
239         else
240             return shared_ptr<exception_detail::error_info_base const>();
241         }
242     }
243
244 #endif