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