void_cast.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // void_cast.cpp: implementation of run-time casting of void pointers
  3. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  4. // Use, modification and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // <gennadiy.rozental@tfn.com>
  8. // See http://www.boost.org for updates, documentation, and revision history.
  9. #if (defined _MSC_VER) && (_MSC_VER == 1200)
  10. # pragma warning (disable : 4786) // too long name, harmless warning
  11. #endif
  12. // STL
  13. #include <set>
  14. #include <functional>
  15. #include <algorithm>
  16. #include <cstddef> // NULL
  17. #ifdef BOOST_SERIALIZATION_LOG
  18. #include <iostream>
  19. #endif
  20. // BOOST
  21. #include <boost/config.hpp>
  22. #include <boost/assert.hpp>
  23. #define BOOST_SERIALIZATION_SOURCE
  24. #include <boost/serialization/config.hpp>
  25. // it marks our code with proper attributes as being exported when
  26. // we're compiling it while marking it import when just the headers
  27. // is being included.
  28. #include <boost/serialization/singleton.hpp>
  29. #include <boost/serialization/extended_type_info.hpp>
  30. #include <boost/serialization/void_cast.hpp>
  31. namespace boost {
  32. namespace serialization {
  33. namespace void_cast_detail {
  34. // note that void_casters are keyed on value of
  35. // member extended type info records - NOT their
  36. // addresses. This is necessary in order for the
  37. // void cast operations to work across dll and exe
  38. // module boundaries.
  39. bool void_caster::operator<(const void_caster & rhs) const {
  40. // include short cut to save time and eliminate
  41. // problems when when base class aren't virtual
  42. if(m_derived != rhs.m_derived){
  43. if(*m_derived < *rhs.m_derived)
  44. return true;
  45. if(*rhs.m_derived < *m_derived)
  46. return false;
  47. }
  48. // m_derived == rhs.m_derived
  49. if(m_base != rhs.m_base)
  50. return *m_base < *rhs.m_base;
  51. else
  52. return false;
  53. }
  54. struct void_caster_compare {
  55. bool operator()(const void_caster * lhs, const void_caster * rhs) const {
  56. return *lhs < *rhs;
  57. }
  58. };
  59. typedef std::set<const void_caster *, void_caster_compare> set_type;
  60. typedef boost::serialization::singleton<set_type> void_caster_registry;
  61. #ifdef BOOST_MSVC
  62. # pragma warning(push)
  63. # pragma warning(disable : 4511 4512)
  64. #endif
  65. // implementation of shortcut void caster
  66. class void_caster_shortcut : public void_caster
  67. {
  68. bool m_includes_virtual_base;
  69. void const *
  70. vbc_upcast(
  71. void const * const t
  72. ) const;
  73. void const *
  74. vbc_downcast(
  75. void const * const t
  76. ) const;
  77. void const *
  78. upcast(void const * const t) const BOOST_OVERRIDE {
  79. if(m_includes_virtual_base)
  80. return vbc_upcast(t);
  81. return static_cast<const char *> ( t ) - m_difference;
  82. }
  83. void const *
  84. downcast(void const * const t) const BOOST_OVERRIDE {
  85. if(m_includes_virtual_base)
  86. return vbc_downcast(t);
  87. return static_cast<const char *> ( t ) + m_difference;
  88. }
  89. virtual bool is_shortcut() const {
  90. return true;
  91. }
  92. bool has_virtual_base() const BOOST_OVERRIDE {
  93. return m_includes_virtual_base;
  94. }
  95. public:
  96. void_caster_shortcut(
  97. extended_type_info const * derived,
  98. extended_type_info const * base,
  99. std::ptrdiff_t difference,
  100. bool includes_virtual_base,
  101. void_caster const * const parent
  102. ) :
  103. void_caster(derived, base, difference, parent),
  104. m_includes_virtual_base(includes_virtual_base)
  105. {
  106. recursive_register(includes_virtual_base);
  107. }
  108. ~void_caster_shortcut() BOOST_OVERRIDE {
  109. recursive_unregister();
  110. }
  111. };
  112. #ifdef BOOST_MSVC
  113. # pragma warning(pop)
  114. #endif
  115. void const *
  116. void_caster_shortcut::vbc_downcast(
  117. void const * const t
  118. ) const {
  119. // try to find a chain that gives us what we want
  120. const void_cast_detail::set_type & s
  121. = void_cast_detail::void_caster_registry::get_const_instance();
  122. void_cast_detail::set_type::const_iterator it;
  123. for(it = s.begin(); it != s.end(); ++it){
  124. // if the current candidate casts to the desired target type
  125. if ((*it)->m_derived == m_derived){
  126. // and if it's not us
  127. if ((*it)->m_base != m_base){
  128. // try to cast from the candidate base to our base
  129. const void * t_new;
  130. t_new = void_downcast(*(*it)->m_base, *m_base, t);
  131. // if we were successful
  132. if(NULL != t_new){
  133. // recast to our derived
  134. const void_caster * vc = *it;
  135. return vc->downcast(t_new);
  136. }
  137. }
  138. }
  139. }
  140. return NULL;
  141. }
  142. void const *
  143. void_caster_shortcut::vbc_upcast(
  144. void const * const t
  145. ) const {
  146. // try to find a chain that gives us what we want
  147. const void_cast_detail::set_type & s
  148. = void_cast_detail::void_caster_registry::get_const_instance();
  149. void_cast_detail::set_type::const_iterator it;
  150. for(it = s.begin(); it != s.end(); ++it){
  151. // if the current candidate casts from the desired base type
  152. if((*it)->m_base == m_base){
  153. // and if it's not us
  154. if ((*it)->m_derived != m_derived){
  155. // try to cast from the candidate derived to our our derived
  156. const void * t_new;
  157. t_new = void_upcast(*m_derived, *(*it)->m_derived, t);
  158. if(NULL != t_new)
  159. return (*it)->upcast(t_new);
  160. }
  161. }
  162. }
  163. return NULL;
  164. }
  165. #ifdef BOOST_MSVC
  166. # pragma warning(push)
  167. # pragma warning(disable : 4511 4512)
  168. #endif
  169. // just used as a search key
  170. class void_caster_argument : public void_caster
  171. {
  172. void const *
  173. upcast(void const * const /*t*/) const BOOST_OVERRIDE {
  174. BOOST_ASSERT(false);
  175. return NULL;
  176. }
  177. void const *
  178. downcast( void const * const /*t*/) const BOOST_OVERRIDE {
  179. BOOST_ASSERT(false);
  180. return NULL;
  181. }
  182. bool has_virtual_base() const BOOST_OVERRIDE {
  183. BOOST_ASSERT(false);
  184. return false;
  185. }
  186. public:
  187. void_caster_argument(
  188. extended_type_info const * derived,
  189. extended_type_info const * base
  190. ) :
  191. void_caster(derived, base)
  192. {}
  193. ~void_caster_argument() BOOST_OVERRIDE {}
  194. };
  195. #ifdef BOOST_MSVC
  196. # pragma warning(pop)
  197. #endif
  198. // implementation of void caster base class
  199. BOOST_SERIALIZATION_DECL void
  200. void_caster::recursive_register(bool includes_virtual_base) const {
  201. void_cast_detail::set_type & s
  202. = void_cast_detail::void_caster_registry::get_mutable_instance();
  203. #ifdef BOOST_SERIALIZATION_LOG
  204. std::clog << "recursive_register\n";
  205. std::clog << m_derived->get_debug_info();
  206. std::clog << "<-";
  207. std::clog << m_base->get_debug_info();
  208. std::clog << "\n";
  209. #endif
  210. std::pair<void_cast_detail::set_type::const_iterator, bool> result;
  211. // comment this out for now.
  212. result = s.insert(this);
  213. //assert(result.second);
  214. // generate all implied void_casts.
  215. void_cast_detail::set_type::const_iterator it;
  216. for(it = s.begin(); it != s.end(); ++it){
  217. if(* m_derived == * (*it)->m_base){
  218. const void_caster_argument vca(
  219. (*it)->m_derived,
  220. m_base
  221. );
  222. void_cast_detail::set_type::const_iterator i;
  223. i = s.find(& vca);
  224. if(i == s.end()){
  225. new void_caster_shortcut(
  226. (*it)->m_derived,
  227. m_base,
  228. m_difference + (*it)->m_difference,
  229. (*it)->has_virtual_base() || includes_virtual_base,
  230. this
  231. );
  232. }
  233. }
  234. if(* (*it)->m_derived == * m_base){
  235. const void_caster_argument vca(
  236. m_derived,
  237. (*it)->m_base
  238. );
  239. void_cast_detail::set_type::const_iterator i;
  240. i = s.find(& vca);
  241. if(i == s.end()){
  242. new void_caster_shortcut(
  243. m_derived,
  244. (*it)->m_base,
  245. m_difference + (*it)->m_difference,
  246. (*it)->has_virtual_base() || includes_virtual_base,
  247. this
  248. );
  249. }
  250. }
  251. }
  252. }
  253. BOOST_SERIALIZATION_DECL void
  254. void_caster::recursive_unregister() const {
  255. // note: it's been discovered that at least one platform is not guaranteed
  256. // to destroy singletons reverse order of construction. So we can't
  257. // use a runtime assert here. Leave this in a reminder not to do this!
  258. // BOOST_ASSERT(! void_caster_registry::is_destroyed());
  259. if(void_caster_registry::is_destroyed())
  260. return;
  261. #ifdef BOOST_SERIALIZATION_LOG
  262. std::clog << "recursive_unregister\n";
  263. std::clog << m_derived->get_debug_info();
  264. std::clog << "<-";
  265. std::clog << m_base->get_debug_info();
  266. std::clog << "\n";
  267. #endif
  268. void_cast_detail::set_type & s
  269. = void_caster_registry::get_mutable_instance();
  270. // delete all shortcuts which use this primitive
  271. void_cast_detail::set_type::iterator it;
  272. for(it = s.begin(); it != s.end();){
  273. const void_caster * vc = *it;
  274. if(vc == this){
  275. s.erase(it++);
  276. }
  277. else
  278. if(vc->m_parent == this){
  279. s.erase(it);
  280. delete vc;
  281. it = s.begin();
  282. }
  283. else
  284. it++;
  285. }
  286. }
  287. } // namespace void_cast_detail
  288. BOOST_SYMBOL_VISIBLE void const *
  289. void_upcast(
  290. extended_type_info const & derived,
  291. extended_type_info const & base,
  292. void const * const t
  293. );
  294. // Given a void *, assume that it really points to an instance of one type
  295. // and alter it so that it would point to an instance of a related type.
  296. // Return the altered pointer. If there exists no sequence of casts that
  297. // can transform from_type to to_type, return a NULL.
  298. BOOST_SERIALIZATION_DECL void const *
  299. void_upcast(
  300. extended_type_info const & derived,
  301. extended_type_info const & base,
  302. void const * const t
  303. ){
  304. // same types - trivial case
  305. if (derived == base)
  306. return t;
  307. // check to see if base/derived pair is found in the registry
  308. const void_cast_detail::set_type & s
  309. = void_cast_detail::void_caster_registry::get_const_instance();
  310. const void_cast_detail::void_caster_argument ca(& derived, & base);
  311. void_cast_detail::set_type::const_iterator it;
  312. it = s.find(& ca);
  313. if (s.end() != it)
  314. return (*it)->upcast(t);
  315. return NULL;
  316. }
  317. BOOST_SYMBOL_VISIBLE void const *
  318. void_downcast(
  319. extended_type_info const & derived,
  320. extended_type_info const & base,
  321. void const * const t
  322. );
  323. BOOST_SERIALIZATION_DECL void const *
  324. void_downcast(
  325. extended_type_info const & derived,
  326. extended_type_info const & base,
  327. void const * const t
  328. ){
  329. // same types - trivial case
  330. if (derived == base)
  331. return t;
  332. // check to see if base/derived pair is found in the registry
  333. const void_cast_detail::set_type & s
  334. = void_cast_detail::void_caster_registry::get_const_instance();
  335. const void_cast_detail::void_caster_argument ca(& derived, & base);
  336. void_cast_detail::set_type::const_iterator it;
  337. it = s.find(&ca);
  338. if (s.end() != it)
  339. return(*it)->downcast(t);
  340. return NULL;
  341. }
  342. } // namespace serialization
  343. } // namespace boost