info.hpp 8.5 KB

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