info.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //Copyright (c) 2006-2010 Emil Dotchevski and Reverge Studios, Inc.
  2. //Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #ifndef BOOST_EXCEPTION_8D22C4CA9CC811DCAA9133D256D89593
  5. #define BOOST_EXCEPTION_8D22C4CA9CC811DCAA9133D256D89593
  6. #include <boost/config.hpp>
  7. #include <boost/exception/exception.hpp>
  8. #include <boost/exception/to_string_stub.hpp>
  9. #include <boost/exception/detail/error_info_impl.hpp>
  10. #include <boost/exception/detail/shared_ptr.hpp>
  11. #include <map>
  12. #ifndef BOOST_EXCEPTION_ENABLE_WARNINGS
  13. #if __GNUC__*100+__GNUC_MINOR__>301
  14. #pragma GCC system_header
  15. #endif
  16. #ifdef __clang__
  17. #pragma clang system_header
  18. #endif
  19. #ifdef _MSC_VER
  20. #pragma warning(push,1)
  21. #endif
  22. #endif
  23. namespace
  24. boost
  25. {
  26. template <class Tag,class T>
  27. inline
  28. std::string
  29. error_info_name( error_info<Tag,T> const & x )
  30. {
  31. return tag_type_name<Tag>();
  32. }
  33. template <class Tag,class T>
  34. inline
  35. std::string
  36. to_string( error_info<Tag,T> const & x )
  37. {
  38. return '[' + error_info_name(x) + "] = " + to_string_stub(x.value()) + '\n';
  39. }
  40. template <class Tag,class T>
  41. inline
  42. std::string
  43. error_info<Tag,T>::
  44. name_value_string() const
  45. {
  46. return to_string_stub(*this);
  47. }
  48. namespace
  49. exception_detail
  50. {
  51. class
  52. error_info_container_impl:
  53. public error_info_container
  54. {
  55. public:
  56. error_info_container_impl():
  57. count_(0)
  58. {
  59. }
  60. ~error_info_container_impl() BOOST_NOEXCEPT_OR_NOTHROW
  61. {
  62. }
  63. void
  64. set( shared_ptr<error_info_base> const & x, type_info_ const & typeid_ )
  65. {
  66. BOOST_ASSERT(x);
  67. info_[typeid_] = x;
  68. diagnostic_info_str_.clear();
  69. }
  70. shared_ptr<error_info_base>
  71. get( type_info_ const & ti ) const
  72. {
  73. error_info_map::const_iterator i=info_.find(ti);
  74. if( info_.end()!=i )
  75. {
  76. shared_ptr<error_info_base> const & p = i->second;
  77. #ifndef BOOST_NO_RTTI
  78. BOOST_ASSERT( *BOOST_EXCEPTION_DYNAMIC_TYPEID(*p).type_==*ti.type_ );
  79. #endif
  80. return p;
  81. }
  82. return shared_ptr<error_info_base>();
  83. }
  84. char const *
  85. diagnostic_information( char const * header ) const
  86. {
  87. if( header )
  88. {
  89. std::ostringstream tmp;
  90. tmp << header;
  91. for( error_info_map::const_iterator i=info_.begin(),end=info_.end(); i!=end; ++i )
  92. {
  93. error_info_base const & x = *i->second;
  94. tmp << x.name_value_string();
  95. }
  96. tmp.str().swap(diagnostic_info_str_);
  97. }
  98. return diagnostic_info_str_.c_str();
  99. }
  100. private:
  101. friend class boost::exception;
  102. typedef std::map< type_info_, shared_ptr<error_info_base> > error_info_map;
  103. error_info_map info_;
  104. mutable std::string diagnostic_info_str_;
  105. mutable int count_;
  106. error_info_container_impl( error_info_container_impl const & );
  107. error_info_container_impl & operator=( error_info_container const & );
  108. void
  109. add_ref() const
  110. {
  111. ++count_;
  112. }
  113. bool
  114. release() const
  115. {
  116. if( --count_ )
  117. return false;
  118. else
  119. {
  120. delete this;
  121. return true;
  122. }
  123. }
  124. refcount_ptr<error_info_container>
  125. clone() const
  126. {
  127. refcount_ptr<error_info_container> p;
  128. error_info_container_impl * c=new error_info_container_impl;
  129. p.adopt(c);
  130. for( error_info_map::const_iterator i=info_.begin(),e=info_.end(); i!=e; ++i )
  131. {
  132. shared_ptr<error_info_base> cp(i->second->clone());
  133. c->info_.insert(std::make_pair(i->first,cp));
  134. }
  135. return p;
  136. }
  137. };
  138. template <class E,class Tag,class T>
  139. inline
  140. E const &
  141. set_info( E const & x, error_info<Tag,T> const & v )
  142. {
  143. typedef error_info<Tag,T> error_info_tag_t;
  144. shared_ptr<error_info_tag_t> p( new error_info_tag_t(v) );
  145. exception_detail::error_info_container * c=x.data_.get();
  146. if( !c )
  147. x.data_.adopt(c=new exception_detail::error_info_container_impl);
  148. c->set(p,BOOST_EXCEPTION_STATIC_TYPEID(error_info_tag_t));
  149. return x;
  150. }
  151. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  152. template <class E,class Tag,class T>
  153. E const & set_info( E const &, error_info<Tag,T> && );
  154. template <class T>
  155. struct set_info_rv;
  156. template <class Tag,class T>
  157. struct
  158. set_info_rv<error_info<Tag,T> >
  159. {
  160. template <class E,class Tag1,class T1>
  161. friend E const & set_info( E const &, error_info<Tag1,T1> && );
  162. template <class E>
  163. static
  164. E const &
  165. set( E const & x, error_info<Tag,T> && v )
  166. {
  167. typedef error_info<Tag,T> error_info_tag_t;
  168. shared_ptr<error_info_tag_t> p( new error_info_tag_t(std::move(v)) );
  169. exception_detail::error_info_container * c=x.data_.get();
  170. if( !c )
  171. x.data_.adopt(c=new exception_detail::error_info_container_impl);
  172. c->set(p,BOOST_EXCEPTION_STATIC_TYPEID(error_info_tag_t));
  173. return x;
  174. }
  175. };
  176. template <>
  177. struct
  178. set_info_rv<throw_function>
  179. {
  180. template <class E,class Tag1,class T1>
  181. friend E const & set_info( E const &, error_info<Tag1,T1> && );
  182. template <class E>
  183. static
  184. E const &
  185. set( E const & x, throw_function && y )
  186. {
  187. x.throw_function_=y.v_;
  188. return x;
  189. }
  190. };
  191. template <>
  192. struct
  193. set_info_rv<throw_file>
  194. {
  195. template <class E,class Tag1,class T1>
  196. friend E const & set_info( E const &, error_info<Tag1,T1> && );
  197. template <class E>
  198. static
  199. E const &
  200. set( E const & x, throw_file && y )
  201. {
  202. x.throw_file_=y.v_;
  203. return x;
  204. }
  205. };
  206. template <>
  207. struct
  208. set_info_rv<throw_line>
  209. {
  210. template <class E,class Tag1,class T1>
  211. friend E const & set_info( E const &, error_info<Tag1,T1> && );
  212. template <class E>
  213. static
  214. E const &
  215. set( E const & x, throw_line && y )
  216. {
  217. x.throw_line_=y.v_;
  218. return x;
  219. }
  220. };
  221. template <class E,class Tag,class T>
  222. inline
  223. E const &
  224. set_info( E const & x, error_info<Tag,T> && v )
  225. {
  226. return set_info_rv<error_info<Tag,T> >::template set<E>(x,std::move(v));
  227. }
  228. #endif
  229. template <class T>
  230. struct
  231. derives_boost_exception
  232. {
  233. enum e { value = (sizeof(dispatch_boost_exception((T*)0))==sizeof(large_size)) };
  234. };
  235. }
  236. template <class E,class Tag,class T>
  237. inline
  238. typename enable_if<exception_detail::derives_boost_exception<E>,E const &>::type
  239. operator<<( E const & x, error_info<Tag,T> const & v )
  240. {
  241. return exception_detail::set_info(x,v);
  242. }
  243. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  244. template <class E,class Tag,class T>
  245. inline
  246. typename enable_if<exception_detail::derives_boost_exception<E>,E const &>::type
  247. operator<<( E const & x, error_info<Tag,T> && v )
  248. {
  249. return exception_detail::set_info(x,std::move(v));
  250. }
  251. #endif
  252. }
  253. #if defined(_MSC_VER) && !defined(BOOST_EXCEPTION_ENABLE_WARNINGS)
  254. #pragma warning(pop)
  255. #endif
  256. #endif