在boost :: msm基本状态机中寻找多态的例子

时间:2013-08-31 11:47:46

标签: class polymorphism boost-msm

我是c ++的新手,提升并使用状态机。 有没有人知道多态boost :: msm状态机的例子?

我尝试在提供的基本示例中将转换操作更改为虚拟,但链接器说:

未定义引用`vtable for DerivedClass'

编辑: 错误是通过分析代码解决的基本且无聊的包含/定义错误。

在仔细阅读基本示例之后,我意识到为了使其具有多态性,我应该在子类中定义后端并从前端继承。

波纹管源文件定义了一个多态基本msm:

  • 花是一个没有状态的死对象
  • FlowerMSM是状态机(一种给予MSM行为的方式)
  • LiveFlower从两者继承并覆盖来自FlowerMSM的转换操作

LiveFlower.cpp

#include "Flower.hpp"
#include "FlowerMSM.hpp"

class LiveFlower: public Flower, public player_ 
{
        void waterAction(evNoWater const&);
        void stopWateringAction(evEnoughWater const&);
};
//Idle
void LiveFlower::waterAction(evNoWater const&)       { std::cout << "Here goes call to LiveFlower::waterAction"; }
//Watering
void LiveFlower::stopWateringAction(evEnoughWater const&)       { std::cout << "Here goes call to LiveFlower::stopWateringAction"; }

typedef msm::back::state_machine<LiveFlower> FonFon;

int main()
{
    FonFon =new FonFon f();
    f.start();
    f.process_event(evNoWater());
}

FlowerMSM.cpp

#include <iostream>
// back-end
#include <boost/msm/back/state_machine.hpp>
//front-end
#include <boost/msm/front/state_machine_def.hpp>
// FlowerMSM is the boost::msm state machine of Flower and defines the logic of its dynamic behavior in response to external events.
namespace msm = boost::msm;
namespace mpl = boost::mpl;


namespace
{

    // events

    struct evNoWater {};
    struct evEnoughWater {};
    // front-end: define the FSM structure 
    struct player_ : public msm::front::state_machine_def<player_>
    {
        template <class Event,class FSM>
        void on_entry(Event const& ,FSM&) 
        {
            std::cout << "entering: Flower Player" << std::endl;
        }
        template <class Event,class FSM>
        void on_exit(Event const&,FSM& ) 
        {
            std::cout << "leaving: Flower Player" << std::endl;
        }

// The list of Flower states
        struct Idle : public msm::front::state<> 
        {
            // every (optional) entry/exit methods get the event passed.
            template <class Event,class FSM>
            void on_entry(Event const&,FSM& ) {std::cout << "Flower entering: Idle" << std::endl;}
            template <class Event,class FSM>
            void on_exit(Event const&,FSM& ) {std::cout << "Flower leaving: Idle" << std::endl;}
        };

        struct Watering : public msm::front::state<> 
        {
            // every (optional) entry/exit methods get the event passed.
            template <class Event,class FSM>
            void on_entry(Event const&,FSM& ) {std::cout << "Flower entering: Watering" << std::endl;}
            template <class Event,class FSM>
            void on_exit(Event const&,FSM& ) {std::cout << "Flower leaving: Watering" << std::endl;}
        };

        // the initial state of the Flower player SM. Must be defined
        typedef Idle initial_state;
        // transition actions

    //Idle
        virtual void waterAction(evNoWater const&)       { std::cout << "Here goes call to Flower::waterAction"; }


    //Watering
        virtual void stopWateringAction(evEnoughWater const&)       { std::cout << "Here goes call to Flower::stopWateringAction"; }

        typedef player_ p; // makes transition table cleaner

        // Transition table for player
        struct transition_table : mpl::vector<
            //    Start     Event         Next      Action               Guard
            //  +---------+-------------+---------+---------------------+----------------------+

          a_row < Idle , evNoWater        , Watering , &p::waterAction                      >,
          a_row < Watering , evEnoughWater        , Idle , &p::stopWateringAction                      >
            //  +---------+-------------+---------+---------------------+----------------------+
        > {};
        // Replaces the default no-transition response.
        template <class FSM,class Event>
        void no_transition(Event const& e, FSM&,int state)
        {
            std::cout << "no transition from state " << state
                << " on event " << typeid(e).name() << std::endl;
        }


    };
    // Pick a back-end
    typedef msm::back::state_machine<player_> FlowerMSM;
}

为了完整起见,“死”花类: 的 Flower.hpp

#include <string>
#include <iostream>

class Flower
{
    public:
        Flower();
        Flower(std::string type);
        std::string getType();
        void setType(std::string type);
        void water();
        void stopWatering();
    private:
        std::string typ;
};
Flower::Flower(){}

Flower::Flower(std::string type){typ=type;}
std::string Flower::getType(){return typ;}
void Flower::setType(std::string type){typ=type;}
void Flower::water(){std::cout<<"watering now";}
void Flower::stopWatering(){std::cout<<"stopped watering ";}

编辑: 如果有人提供更好的解决方案,请不要接受我自己的答案。

1 个答案:

答案 0 :(得分:0)

在原帖中,现在提供了答案。