slot_base.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Boost.Signals2 library
  2. // Copyright Frank Mori Hess 2007-2008.
  3. // Copyright Timmo Stange 2007.
  4. // Copyright Douglas Gregor 2001-2004. Use, modification and
  5. // distribution is subject to the Boost Software License, Version
  6. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. // For more information, see http://www.boost.org
  9. #ifndef BOOST_SIGNALS2_SLOT_BASE_HPP
  10. #define BOOST_SIGNALS2_SLOT_BASE_HPP
  11. #include <boost/shared_ptr.hpp>
  12. #include <boost/weak_ptr.hpp>
  13. #include <boost/signals2/detail/foreign_ptr.hpp>
  14. #include <boost/signals2/expired_slot.hpp>
  15. #include <boost/signals2/signal_base.hpp>
  16. #include <boost/variant/apply_visitor.hpp>
  17. #include <boost/variant/variant.hpp>
  18. #include <vector>
  19. namespace boost
  20. {
  21. namespace signals2
  22. {
  23. namespace detail
  24. {
  25. class tracked_objects_visitor;
  26. class trackable_pointee;
  27. typedef boost::variant<boost::weak_ptr<trackable_pointee>, boost::weak_ptr<void>, detail::foreign_void_weak_ptr > void_weak_ptr_variant;
  28. typedef boost::variant<boost::shared_ptr<void>, detail::foreign_void_shared_ptr > void_shared_ptr_variant;
  29. class lock_weak_ptr_visitor
  30. {
  31. public:
  32. typedef void_shared_ptr_variant result_type;
  33. template<typename WeakPtr>
  34. result_type operator()(const WeakPtr &wp) const
  35. {
  36. return wp.lock();
  37. }
  38. // overload to prevent incrementing use count of shared_ptr associated
  39. // with signals2::trackable objects
  40. result_type operator()(const weak_ptr<trackable_pointee> &) const
  41. {
  42. return boost::shared_ptr<void>();
  43. }
  44. };
  45. class expired_weak_ptr_visitor
  46. {
  47. public:
  48. typedef bool result_type;
  49. template<typename WeakPtr>
  50. bool operator()(const WeakPtr &wp) const
  51. {
  52. return wp.expired();
  53. }
  54. };
  55. }
  56. class slot_base
  57. {
  58. public:
  59. typedef std::vector<detail::void_weak_ptr_variant> tracked_container_type;
  60. typedef std::vector<detail::void_shared_ptr_variant> locked_container_type;
  61. const tracked_container_type& tracked_objects() const {return _tracked_objects;}
  62. locked_container_type lock() const
  63. {
  64. locked_container_type locked_objects;
  65. tracked_container_type::const_iterator it;
  66. for(it = tracked_objects().begin(); it != tracked_objects().end(); ++it)
  67. {
  68. locked_objects.push_back(apply_visitor(detail::lock_weak_ptr_visitor(), *it));
  69. if(apply_visitor(detail::expired_weak_ptr_visitor(), *it))
  70. {
  71. throw expired_slot();
  72. }
  73. }
  74. return locked_objects;
  75. }
  76. bool expired() const
  77. {
  78. tracked_container_type::const_iterator it;
  79. for(it = tracked_objects().begin(); it != tracked_objects().end(); ++it)
  80. {
  81. if(apply_visitor(detail::expired_weak_ptr_visitor(), *it)) return true;
  82. }
  83. return false;
  84. }
  85. protected:
  86. friend class detail::tracked_objects_visitor;
  87. void track_signal(const signal_base &signal)
  88. {
  89. _tracked_objects.push_back(signal.lock_pimpl());
  90. }
  91. tracked_container_type _tracked_objects;
  92. };
  93. }
  94. } // end namespace boost
  95. #endif // BOOST_SIGNALS2_SLOT_BASE_HPP