info.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 defined(__GNUC__) && __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 & )
  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 BOOST_FINAL:
  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. return p;
  78. }
  79. return shared_ptr<error_info_base>();
  80. }
  81. char const *
  82. diagnostic_information( char const * header ) const
  83. {
  84. if( header )
  85. {
  86. std::ostringstream tmp;
  87. tmp << header;
  88. for( error_info_map::const_iterator i=info_.begin(),end=info_.end(); i!=end; ++i )
  89. {
  90. error_info_base const & x = *i->second;
  91. tmp << x.name_value_string();
  92. }
  93. tmp.str().swap(diagnostic_info_str_);
  94. }
  95. return diagnostic_info_str_.c_str();
  96. }
  97. private:
  98. friend class boost::exception;
  99. typedef std::map< type_info_, shared_ptr<error_info_base> > error_info_map;
  100. error_info_map info_;
  101. mutable std::string diagnostic_info_str_;
  102. mutable int count_;
  103. error_info_container_impl( error_info_container_impl const & );
  104. error_info_container_impl & operator=( error_info_container const & );
  105. void
  106. add_ref() const
  107. {
  108. ++count_;
  109. }
  110. bool
  111. release() const
  112. {
  113. if( --count_ )
  114. return false;
  115. else
  116. {
  117. delete this;
  118. return true;
  119. }
  120. }
  121. refcount_ptr<error_info_container>
  122. clone() const
  123. {
  124. refcount_ptr<error_info_container> p;
  125. error_info_container_impl * c=new error_info_container_impl;
  126. p.adopt(c);
  127. for( error_info_map::const_iterator i=info_.begin(),e=info_.end(); i!=e; ++i )
  128. {
  129. shared_ptr<error_info_base> cp(i->second->clone());
  130. c->info_.insert(std::make_pair(i->first,cp));
  131. }
  132. return p;
  133. }
  134. };
  135. template <class E,class Tag,class T>
  136. inline
  137. E const &
  138. set_info( E const & x, error_info<Tag,T> const & v )
  139. {
  140. typedef error_info<Tag,T> error_info_tag_t;
  141. shared_ptr<error_info_tag_t> p( new error_info_tag_t(v) );
  142. exception_detail::error_info_container * c=x.data_.get();
  143. if( !c )
  144. x.data_.adopt(c=new exception_detail::error_info_container_impl);
  145. c->set(p,BOOST_EXCEPTION_STATIC_TYPEID(error_info_tag_t));
  146. return x;
  147. }
  148. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  149. template <class E,class Tag,class T>
  150. E const & set_info( E const &, error_info<Tag,T> && );
  151. template <class T>
  152. struct set_info_rv;
  153. template <class Tag,class T>
  154. struct
  155. set_info_rv<error_info<Tag,T> >
  156. {
  157. template <class E,class Tag1,class T1>
  158. friend E const & set_info( E const &, error_info<Tag1,T1> && );
  159. template <class E>
  160. static
  161. E const &
  162. set( E const & x, error_info<Tag,T> && v )
  163. {
  164. typedef error_info<Tag,T> error_info_tag_t;
  165. shared_ptr<error_info_tag_t> p( new error_info_tag_t(std::move(v)) );
  166. exception_detail::error_info_container * c=x.data_.get();
  167. if( !c )
  168. x.data_.adopt(c=new exception_detail::error_info_container_impl);
  169. c->set(p,BOOST_EXCEPTION_STATIC_TYPEID(error_info_tag_t));
  170. return x;
  171. }
  172. };
  173. template <>
  174. struct
  175. set_info_rv<throw_function>
  176. {
  177. template <class E,class Tag1,class T1>
  178. friend E const & set_info( E const &, error_info<Tag1,T1> && );
  179. template <class E>
  180. static
  181. E const &
  182. set( E const & x, throw_function && y )
  183. {
  184. x.throw_function_=y.v_;
  185. return x;
  186. }
  187. };
  188. template <>
  189. struct
  190. set_info_rv<throw_file>
  191. {
  192. template <class E,class Tag1,class T1>
  193. friend E const & set_info( E const &, error_info<Tag1,T1> && );
  194. template <class E>
  195. static
  196. E const &
  197. set( E const & x, throw_file && y )
  198. {
  199. x.throw_file_=y.v_;
  200. return x;
  201. }
  202. };
  203. template <>
  204. struct
  205. set_info_rv<throw_line>
  206. {
  207. template <class E,class Tag1,class T1>
  208. friend E const & set_info( E const &, error_info<Tag1,T1> && );
  209. template <class E>
  210. static
  211. E const &
  212. set( E const & x, throw_line && y )
  213. {
  214. x.throw_line_=y.v_;
  215. return x;
  216. }
  217. };
  218. template <class E,class Tag,class T>
  219. inline
  220. E const &
  221. set_info( E const & x, error_info<Tag,T> && v )
  222. {
  223. return set_info_rv<error_info<Tag,T> >::template set<E>(x,std::move(v));
  224. }
  225. #endif
  226. template <class T>
  227. struct
  228. derives_boost_exception
  229. {
  230. enum e { value = (sizeof(dispatch_boost_exception((T*)0))==sizeof(large_size)) };
  231. };
  232. }
  233. template <class E,class Tag,class T>
  234. inline
  235. typename enable_if<exception_detail::derives_boost_exception<E>,E const &>::type
  236. operator<<( E const & x, error_info<Tag,T> const & v )
  237. {
  238. return exception_detail::set_info(x,v);
  239. }
  240. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  241. template <class E,class Tag,class T>
  242. inline
  243. typename enable_if<exception_detail::derives_boost_exception<E>,E const &>::type
  244. operator<<( E const & x, error_info<Tag,T> && v )
  245. {
  246. return exception_detail::set_info(x,std::move(v));
  247. }
  248. #endif
  249. }
  250. #if defined(_MSC_VER) && !defined(BOOST_EXCEPTION_ENABLE_WARNINGS)
  251. #pragma warning(pop)
  252. #endif
  253. #endif