event_base.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #ifndef BOOST_STATECHART_EVENT_BASE_HPP_INCLUDED
  2. #define BOOST_STATECHART_EVENT_BASE_HPP_INCLUDED
  3. //////////////////////////////////////////////////////////////////////////////
  4. // Copyright 2002-2006 Andreas Huber Doenni
  5. // Distributed under the Boost Software License, Version 1.0. (See accompany-
  6. // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //////////////////////////////////////////////////////////////////////////////
  8. #include <boost/statechart/detail/rtti_policy.hpp>
  9. #include <boost/statechart/detail/counted_base.hpp>
  10. #include <boost/assert.hpp>
  11. #include <boost/intrusive_ptr.hpp>
  12. #include <boost/config.hpp>
  13. namespace boost
  14. {
  15. namespace statechart
  16. {
  17. namespace detail
  18. {
  19. // This helper is necessary because there doesn't seem to be consensus among
  20. // compilers on how a friend declaration for a function in another namespace
  21. // has to look like.
  22. class delete_helper
  23. {
  24. public:
  25. template< class T >
  26. static void delete_object( const T * pObject )
  27. {
  28. delete pObject;
  29. }
  30. };
  31. } // namespace detail
  32. //////////////////////////////////////////////////////////////////////////////
  33. class event_base : public detail::rtti_policy::rtti_base_type<
  34. detail::counted_base<> >
  35. {
  36. typedef detail::rtti_policy::rtti_base_type<
  37. detail::counted_base<> > base_type;
  38. public:
  39. //////////////////////////////////////////////////////////////////////////
  40. intrusive_ptr< const event_base > intrusive_from_this() const;
  41. protected:
  42. //////////////////////////////////////////////////////////////////////////
  43. event_base( detail::rtti_policy::id_provider_type idProvider ) :
  44. base_type( idProvider )
  45. {
  46. }
  47. virtual ~event_base() {}
  48. private:
  49. //////////////////////////////////////////////////////////////////////////
  50. virtual intrusive_ptr< const event_base > clone() const = 0;
  51. friend class detail::delete_helper;
  52. };
  53. #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
  54. } // namespace statechart
  55. #endif
  56. inline void intrusive_ptr_add_ref( const ::boost::statechart::event_base * pBase )
  57. {
  58. pBase->add_ref();
  59. }
  60. inline void intrusive_ptr_release( const ::boost::statechart::event_base * pBase )
  61. {
  62. if ( pBase->release() )
  63. {
  64. ::boost::statechart::detail::delete_helper::delete_object( pBase );
  65. }
  66. }
  67. #ifndef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
  68. } // namespace statechart
  69. #endif
  70. namespace statechart
  71. {
  72. // We're implementing this here so that GCC3.4.2 can find
  73. // intrusive_ptr_add_ref, which is indirectly called from the intrusive_ptr
  74. // ctor.
  75. inline intrusive_ptr< const event_base > event_base::intrusive_from_this() const
  76. {
  77. if ( base_type::ref_counted() )
  78. {
  79. return intrusive_ptr< const event_base >( this );
  80. }
  81. else
  82. {
  83. return clone();
  84. }
  85. }
  86. } // namespace statechart
  87. } // namespace boost
  88. #endif