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