关注、星标公众号,直达精彩内容
来源:技术让梦想更伟大
作者:李肖遥
开发中经常遇到api如何使用,或者省略时间可以直接使用一些开源的代码,programcreek很好的解决这个问题,这个网站可以提供最简单的demo,快速获知一个API的使用,开发中让我们事半功倍,大家可以用用。
网站地址:https://www.programcreek.com/
首页界面:
我们看看c++ code Examples
我们搜索一下state machine,看到找到了4 个与“状态机”相关的 C++ 代码示例,看看代码写的怎么样 很多例子写的很好,甚至可以直接使用。
void ReplicationThread::CallbacksStateMachine::ConnEventCB(
bufferevent *bev, int16_t events, void *state_machine_ptr) {
if (events & BEV_EVENT_CONNECTED) {
// call write_cb when connected
bufferevent_data_cb write_cb;
bufferevent_getcb(bev, nullptr, &write_cb, nullptr, nullptr);
if (write_cb) write_cb(bev, state_machine_ptr);
return;
}
if (events & (BEV_EVENT_ERROR | BEV_EVENT_EOF)) {
LOG(ERROR) << "[replication] connection error/eof, reconnect the master";
// Wait a bit and reconnect
auto state_m = static_cast(state_machine_ptr);
state_m->repl_->repl_state_ = kReplConnecting;
std::this_thread::sleep_for(std::chrono::seconds(1));
state_m->Stop();
state_m->Start();
}
}
再看看C++ open source Projects
我们搜索一下state machine,看到有4个相关项目,还有分层有限状态机框架HFSM2和高性能分层有限状态机框架HFSM2。
我们点开HSFM
Basic Usage // 1. Include HFSM header: #include// 2. Define interface class between the FSM and its owner // (also ok to use the owner object itself): struct Context { }; // 3. (Optional) Typedef hfsm::Machine for convenience: using M = hfsm::Machine ; // 4. Define states: struct MyState1 : M::Bare { // 5. Override some of the following state functions: void enter(Context& _); void update(Context& _); void transition(Control& c, Context& _); void leave(Context& _); }; struct MyState2 : M::Bare { }; struct MySubState1 : M::Bare { }; struct MySubState2 : M::Bare { }; struct MyState3 : M::Bare { }; struct MyOrthogonalState1 : M::Bare { }; struct MyOrthogonalState2 : M::Bare { }; // 6. Declare state machine structure: using MyFSM = M::PeerRoot< MyState1, M::Composite , M::Orthogonal >; int main() { // 7. Create context and state machine instances: Context context; MyFSM fsm(context); // 8. Kick off periodic updates: bool running = true; while (running) fsm.update(); return 0; }
总的来说,这个programcreek很不错,发现一些好的工具或者网站可以使我们开发效率提高,这也是我们站在巨人的肩膀上的意义。
‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧ END ‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧
关注我的微信公众号,回复“加群”按规则加入技术交流群。
欢迎关注我的视频号:
点击“阅读原文”查看更多分享,欢迎点分享、收藏、点赞、在看。



