]> git.lyx.org Git - lyx.git/blob - boost/boost/exception/info.hpp
boost: add eol property
[lyx.git] / boost / boost / exception / info.hpp
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 & 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>
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 & 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>();
98                 }
99
100             char const *
101             diagnostic_information( char const * header ) const
102                 {
103                 if( header )
104                     {
105                     BOOST_ASSERT(*header!=0);
106                     std::ostringstream tmp;
107                     tmp << header;
108                     for( error_info_map::const_iterator i=info_.begin(),end=info_.end(); i!=end; ++i )
109                         {
110                         shared_ptr<error_info_base const> const & x = i->second;
111                         tmp << '[' << x->tag_typeid_name() << "] = " << x->value_as_string() << '\n';
112                         }
113                     tmp.str().swap(diagnostic_info_str_);
114                     }
115                 return diagnostic_info_str_.c_str();
116                 }
117
118             private:
119
120             friend class boost::exception;
121
122             typedef std::map< type_info_, shared_ptr<error_info_base> > error_info_map;
123             error_info_map info_;
124             mutable std::string diagnostic_info_str_;
125             mutable int count_;
126
127             void
128             add_ref() const
129                 {
130                 ++count_;
131                 }
132
133             void
134             release() const
135                 {
136                 if( !--count_ )
137                     delete this;
138                 }
139             };
140         }
141
142     template <class E,class Tag,class T>
143     inline
144     E const &
145     operator<<( E const & x, error_info<Tag,T> const & v )
146         {
147         typedef error_info<Tag,T> error_info_tag_t;
148         shared_ptr<error_info_tag_t> p( new error_info_tag_t(v) );
149         exception_detail::error_info_container * c;
150         if( !(c=x.data_.get()) )
151             x.data_.adopt(c=new exception_detail::error_info_container_impl);
152         c->set(p,BOOST_EXCEPTION_STATIC_TYPEID(error_info_tag_t));
153         return x;
154         }
155     }
156
157 #endif