Player.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #ifndef BOOST_STATECHART_EXAMPLE_PLAYER_HPP_INCLUDED
  2. #define BOOST_STATECHART_EXAMPLE_PLAYER_HPP_INCLUDED
  3. //////////////////////////////////////////////////////////////////////////////
  4. // Copyright 2008 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/event.hpp>
  9. #include <boost/statechart/fifo_scheduler.hpp>
  10. #include <boost/statechart/asynchronous_state_machine.hpp>
  11. #include <boost/config.hpp>
  12. #include <boost/intrusive_ptr.hpp>
  13. #include <boost/mpl/list.hpp>
  14. #include <boost/function.hpp>
  15. #ifdef CUSTOMIZE_MEMORY_MANAGEMENT
  16. # ifdef BOOST_HAS_THREADS
  17. // for some reason the following is not automatically defined
  18. # if defined( BOOST_MSVC ) | defined( BOOST_INTEL )
  19. # define __WIN32__
  20. # endif
  21. # else
  22. # define BOOST_NO_MT
  23. # endif
  24. # ifdef BOOST_MSVC
  25. # pragma warning( push )
  26. # pragma warning( disable: 4127 ) // conditional expression is constant
  27. # endif
  28. # include <boost/pool/pool_alloc.hpp>
  29. # ifdef BOOST_MSVC
  30. # pragma warning( pop )
  31. # endif
  32. #endif
  33. #include <memory> // std::allocator
  34. namespace sc = boost::statechart;
  35. //////////////////////////////////////////////////////////////////////////////
  36. template< class T >
  37. boost::intrusive_ptr< T > MakeIntrusive( T * pObject )
  38. {
  39. return boost::intrusive_ptr< T >( pObject );
  40. }
  41. //////////////////////////////////////////////////////////////////////////////
  42. struct BallReturned : sc::event< BallReturned >
  43. {
  44. boost::function1< void, const boost::intrusive_ptr< const BallReturned > & >
  45. returnToOpponent;
  46. boost::function0< void > abortGame;
  47. };
  48. struct GameAborted : sc::event< GameAborted > {};
  49. #ifdef CUSTOMIZE_MEMORY_MANAGEMENT
  50. typedef boost::fast_pool_allocator< int > MyAllocator;
  51. typedef sc::fifo_scheduler<
  52. sc::fifo_worker< MyAllocator >, MyAllocator > MyScheduler;
  53. #else
  54. typedef std::allocator< sc::none > MyAllocator;
  55. typedef sc::fifo_scheduler<> MyScheduler;
  56. #endif
  57. //////////////////////////////////////////////////////////////////////////////
  58. struct Player;
  59. struct Waiting;
  60. namespace boost
  61. {
  62. namespace statechart
  63. {
  64. // The following class member specialization ensures that
  65. // state_machine<>::initiate is not instantiated at a point where Waiting
  66. // is not defined yet.
  67. template<>
  68. inline void asynchronous_state_machine<
  69. Player, Waiting, MyScheduler, MyAllocator >::initiate_impl() {}
  70. }
  71. }
  72. struct Player : sc::asynchronous_state_machine<
  73. Player, Waiting, MyScheduler, MyAllocator >
  74. {
  75. public:
  76. Player( my_context ctx, unsigned int maxNoOfReturns ) :
  77. my_base( ctx ),
  78. maxNoOfReturns_( maxNoOfReturns )
  79. {
  80. }
  81. static unsigned int & TotalNoOfProcessedEvents()
  82. {
  83. return totalNoOfProcessedEvents_;
  84. }
  85. unsigned int GetMaxNoOfReturns() const
  86. {
  87. return maxNoOfReturns_;
  88. }
  89. private:
  90. // This function is defined in the Player.cpp
  91. virtual void initiate_impl();
  92. static unsigned int totalNoOfProcessedEvents_;
  93. const unsigned int maxNoOfReturns_;
  94. };
  95. #endif