C++容器queue

class template

std::queue

template <class T, class Container = deque<T> > class queue;

先进先出的容器;常用函数:

Member functions

例子:

  

#include <iostream>
#include <queue>

class DoorFSM
{
public:
    /*States*/
    enum States { Closed, Unlocked, Locked, Opened   }; // states

    /*Events*/
    enum Event { Lock, Unlock, Open, Close   };

    /// Constructor
    DoorFSM()
    {
        __Y = Opened;
    }

    /// Destructor
    virtual ~DoorFSM() {}

    /** Get current FSM state
        @returns current FSM state
     */
    States currentState()
    {
        return __Y;
    }

    /** Send event to FSM
        Use this function to send event to DoorFSM After
        you call it given event will be handled, and, if some of
        transition conditions match, appropriate transition will
        be triggered, and currentState() will be changed.
        If this function is called during existing event handling
        process, given event will be added to pending event queue,
        and will be handled after current transition. See examples for details.
    */
    void A( Event e );

protected:
    virtual void enterOpened() = 0;
    virtual void enterLocked() = 0;
    virtual void enterUnlocked() = 0;
    virtual void enterClosed() = 0;

private:
    States __Y;
    std::queue<Event> __events;
    void __processEvent( Event e );

};

void DoorFSM::__processEvent( Event e )
{
    States yOld = __Y;
    bool pass = false;

    switch( __Y )   //transitions
    {
    case Closed:
        if( e == Open )
        {
            //outcome actions
            __Y = Opened;
            pass = true;
        }
        else if( e == Lock )
        {
            //outcome actions
            __Y = Locked;
            pass = true;
        }
        break;
    case Unlocked:
        if( e == Lock )
        {
            //outcome actions
            __Y = Locked;
            pass = true;
        }
        else if( e == Open )
        {
            //outcome actions
            __Y = Opened;
            pass = true;
        }
        break;
    case Locked:
        if( e == Unlock )
        {
            //outcome actions
            __Y = Unlocked;
            pass = true;
        }
        break;
    case Opened:
        if( e == Close )
        {
            //outcome actions
            __Y = Closed;
            pass = true;
        }
        break;
    }

    if( yOld == __Y && !pass )
    {
        return;
    }

    switch( __Y )   // income actions
    {
    case Closed:
        enterClosed();
        break;
    case Unlocked:
        enterUnlocked();
        break;
    case Locked:
        enterLocked();
        break;
    case Opened:
        enterOpened();
        break;
    }
}

void DoorFSM::A( Event e )

{
    bool __empty = __events.empty();
    __events.push( e );

    if( __empty )
    {
        while( !__events.empty() )
        {
            __processEvent( __events.front() );
            __events.pop();
        }
    }
}

class DoorFSMLogic : public DoorFSM
{
protected:
    virtual void enterOpened()
    {
        std::cout << "Enter Opened state." << std::endl;
    }

    virtual void enterLocked()
    {
        std::cout << "Enter Closed state." << std::endl;
    }

    virtual void enterUnlocked()
    {
        std::cout << "Enter Locked state." << std::endl;
    }

    virtual void enterClosed()
    {
        std::cout << "Enter Unlocked state." << std::endl;
    }
};

//测试程序
int main()
{
    DoorFSMLogic door;

    door.A(DoorFSM::Close);
    door.A(DoorFSM::Lock);
    door.A(DoorFSM::Unlock);
    door.A(DoorFSM::Open);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值