fix doc example typo
[boost.git] / boost / exception / info.hpp
blob328563865d7e0b4f8bd1c371a1191dae53005c63
1 //Copyright (c) 2006-2009 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/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>
15 namespace
16 boost
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 )
23 return to_string(x.value());
26 template <class Tag,class T>
27 inline
28 error_info<Tag,T>::
29 error_info( value_type const & value ):
30 value_(value)
34 template <class Tag,class T>
35 inline
36 error_info<Tag,T>::
37 ~error_info() throw()
41 template <class Tag,class T>
42 inline
43 char const *
44 error_info<Tag,T>::
45 tag_typeid_name() const
47 return tag_type_name<Tag>();
50 template <class Tag,class T>
51 inline
52 std::string
53 error_info<Tag,T>::
54 value_as_string() const
56 return to_string_stub(*this);
59 namespace
60 exception_detail
62 class
63 error_info_container_impl:
64 public error_info_container
66 public:
68 error_info_container_impl():
69 count_(0)
73 ~error_info_container_impl() throw()
77 void
78 set( shared_ptr<error_info_base const> const & x, type_info_ const & typeid_ )
80 BOOST_ASSERT(x);
81 info_[typeid_] = x;
82 diagnostic_info_str_.clear();
85 shared_ptr<error_info_base const>
86 get( type_info_ const & ti ) const
88 error_info_map::const_iterator i=info_.find(ti);
89 if( info_.end()!=i )
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;
97 return shared_ptr<error_info_base const>();
100 char const *
101 diagnostic_information() const
103 if( diagnostic_info_str_.empty() )
105 std::ostringstream tmp;
106 for( error_info_map::const_iterator i=info_.begin(),end=info_.end(); i!=end; ++i )
108 shared_ptr<error_info_base const> const & x = i->second;
109 tmp << '[' << x->tag_typeid_name() << "] = " << x->value_as_string() << '\n';
111 tmp.str().swap(diagnostic_info_str_);
113 return diagnostic_info_str_.c_str();
116 private:
118 friend class boost::exception;
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_;
125 void
126 add_ref() const
128 ++count_;
131 void
132 release() const
134 if( !--count_ )
135 delete this;
140 template <class E,class Tag,class T>
141 inline
142 E const &
143 operator<<( E const & x, error_info<Tag,T> const & v )
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;
155 #endif