]> rtime.felk.cvut.cz Git - eurobot/public.git/commitdiff
TimedFSM: Tested that event can succesfully carry pointer
authorPetr Silhavik <silhavik.p@gmail.com>
Sun, 14 Oct 2012 16:08:47 +0000 (18:08 +0200)
committerPetr Silhavik <silhavik.p@gmail.com>
Sun, 14 Oct 2012 16:08:47 +0000 (18:08 +0200)
src/boostFSM/test/timed_fsm_test.cpp

index 7f71bbc60a34589749f2ae27b7455ebaed88b217..116b77e7f978bb6e9ef27e9d531a95174d2affb7 100644 (file)
@@ -3,6 +3,7 @@
 #include <boost/statechart/state.hpp>
 #include <boost/statechart/transition.hpp>
 #include <boost/statechart/event.hpp>
+#include <boost/statechart/custom_reaction.hpp>
 #include <deque>
 
 #include <stdio.h>
@@ -55,10 +56,9 @@ public:
                friend class Scheduler;
        };
 
-       Scheduler() {
+       Scheduler() : terminated_(false) {
                pthread_mutex_init(& queue_lock, NULL);
                sem_init(&waiting_sem, 0, 0);
-               terminated_ = false;
        }
 
        template<class Processor>
@@ -194,7 +194,8 @@ struct MyStateMachine2 : sc::asynchronous_state_machine<MyStateMachine2, State3,
 };
 
 struct Ev1 : sc::event< Ev1 > {
-       Ev1() { printf("%s\n", __FUNCTION__); }
+       int *x;
+       Ev1(int *y = NULL) : x(y) { printf("%s\n", __FUNCTION__); }
        ~Ev1() { printf("%s\n", __FUNCTION__); }
 };
 
@@ -216,8 +217,8 @@ struct State2 : sc::state<State2, MyStateMachine>
        Timer time;
        Timer time1;
        State2 (my_context ctx ) : my_base( ctx ) {
-         outermost_context().addTimer<Ev1>(time1, 2000, new Ev1());
-         outermost_context().addTimer<Ev2>(time, 8000, new Ev2());
+               outermost_context().addTimer<Ev1>(time1, 2000, new Ev1());
+               outermost_context().addTimer<Ev2>(time, 8000, new Ev2());
                printf("%s\n", __FUNCTION__);
        }
        typedef sc::transition< Ev2, State1 > reactions;
@@ -225,19 +226,26 @@ struct State2 : sc::state<State2, MyStateMachine>
 
 struct State3 : sc::state<State3, MyStateMachine2>
 {
+       int x;
        Timer time;
        State3(my_context ctx): my_base(ctx) {
-               outermost_context().addTimer<Ev1>(time, 5000, new Ev1());
+               x = 6;
+               outermost_context().addTimer<Ev1>(time, 5000, new Ev1(&x));
                printf("%s\n", __FUNCTION__);
        }
-       typedef sc::transition< Ev1, State4 > reactions;
+       typedef sc::custom_reaction< Ev1 > reactions;
+       sc::result react( const Ev1 & udalost)
+       {
+               if(udalost.x) std::cout<<*(udalost.x)<<"\n";
+               return transit<State4>();
+       }
 };
 
 struct State4 : sc::state<State4, MyStateMachine2>
 {
        Timer time1;
        State4 (my_context ctx ) : my_base( ctx ) {
-         outermost_context().addTimer<Ev2>(time1, 5000, new Ev2());
+               outermost_context().addTimer<Ev2>(time1, 5000, new Ev2());
                printf("%s\n", __FUNCTION__);
        }
        typedef sc::transition< Ev2, State3 > reactions;
@@ -250,9 +258,8 @@ int main(int argc, char *argv[])
 #ifdef USE_FIFO_SCHEDULER
        scheduler.initiate_processor(machine);
 #endif
-       scheduler.queue_event(machine, new Ev1);
-//     scheduler.queue_event(machine, new Ev2);
-Scheduler::processor_handle machine2 = scheduler.create_processor<MyStateMachine2>();
+       scheduler.queue_event(machine, new Ev1());
+       Scheduler::processor_handle machine2 = scheduler.create_processor<MyStateMachine2>();
 #ifdef USE_FIFO_SCHEDULER
        scheduler.initiate_processor(machine2);
 #endif