Boost :: msm如何使用非默认构造函数初始化state_machine_def和msm :: front :: state

时间:2017-07-19 11:01:58

标签: c++ state-machine boost-msm

我的状态机看起来像这样:

class FsmDef : public boost::msm::front::state_machine_def<FsmDef> {
private:
    Args args;
    using State = boost::msm::front::state<>;
public:
    FsmDef(Args args) : args{args}
    {}


    struct InitState {};
    struct State1 {
        Args1 args1;
        State1(Args1 args1) : args1(args1)
         {}
    };

    struct transition_table : boost::mpl::vector<
        boost::msm::front::Row<Init, boost::msm::front::none, State1>
    > { };

    using initial_state = InitState;
};

using Fsm = boost::msm::back::state_machine<FsmDef>;

Fsm fsm;

如何构建fsm并初始化FsmDef的私有数据。与State1相同的事情。

1 个答案:

答案 0 :(得分:3)

from selenium import webdriver from scrapy.http import HtmlResponse from selenium.common.exceptions import TimeoutException from selenium.webdriver.support.ui import WebDriverWait class SeleniumMiddleware(object): def __init__(self): self.driver = webdriver.PhantomJS() def process_request(self, request, spider): self.driver.get(request.url) tweets = self.driver.find_elements_by_xpath("//li[@data-item-type='tweet']") while len(tweets) < 200: try: self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") WebDriverWait(self.driver, 10).until( lambda driver: new_posts(driver, len(tweets))) tweets = self.driver.find_elements_by_xpath("//li[@data-item-type='tweet']") except TimeoutException: break body = self.driver.page_source return HtmlResponse(self.driver.current_url, body=body, encoding='utf-8', request=request) def new_posts(driver, min_len): return len(driver.find_elements_by_xpath("//li[@data-item-type='tweet']")) > min_len 可以是非默认的可构造的。但FsmDef需要默认可构造。

这是一种将参数传递给State1的方法。

FsmDef

运行演示https://wandbox.org/permlink/ZhhblHFKYWd3ieDK

#include <iostream> #include <boost/msm/back/state_machine.hpp> #include <boost/msm/front/state_machine_def.hpp> #include <boost/msm/front/functor_row.hpp> struct Args { int val; }; class FsmDef : public boost::msm::front::state_machine_def<FsmDef> { private: Args args_; using State = boost::msm::front::state<>; public: FsmDef(Args args) : args_{args} { std::cout << args_.val << std::endl; } struct InitState : boost::msm::front::state<> {}; struct State1 : boost::msm::front::state<> { // states must be default constructible // Args1 args1; // State1(Args1 args1) : args1(args1) // {} }; struct transition_table : boost::mpl::vector< boost::msm::front::Row<InitState, boost::msm::front::none, State1> > { }; using initial_state = InitState; }; using Fsm = boost::msm::back::state_machine<FsmDef>; int main() { Args a {42}; Fsm fsm(a); } Fsm的构造函数与boost::msm::back::state_machine<FsmDef>具有相同的参数。 AFAIK,没有明确记录。

以下是定义构造函数的代码。

https://github.com/boostorg/msm/blob/boost-1.64.0/include/boost/msm/back/state_machine.hpp#L1625