如何重置提升::sml::sm 实例
How to reset boost::sml::sm instance
似乎boost::sml::sm
没有任何特定的重置方法,并且缺少移动分配运算符,因此我无法执行诸如分配新实例之类的操作(例如machine = boost::sml::sm<MyMachine>{}
(。我目前的方法是一个丑陋的技巧,将 dtor 和 ctor 调用到位:
machine.~sm();
new (&machine) std::decay_t<decltype(machine)>();
有没有更好的方法?
就像你往常做的那样(即从不做你实际写的东西(:使用包装器来管理生命周期,例如使用optional
或unique_ptr
.
例:
住在科里鲁
//
// Copyright (c) 2016-2018 Kris Jusiak (kris at jusiak dot net)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <boost/sml.hpp>
#include <cassert>
namespace sml = boost::sml;
namespace {
// events
struct release {};
struct ack {};
struct fin {};
struct timeout {};
// guards
const auto is_ack_valid = [](const ack&) { return true; };
const auto is_fin_valid = [](const fin&) { return true; };
// actions
const auto send_fin = [] {};
const auto send_ack = [] {};
struct hello_world {
auto operator()() const {
using namespace sml;
// clang-format off
return make_transition_table(
*"established"_s + event<release> / send_fin = "fin wait 1"_s,
"fin wait 1"_s + event<ack> [ is_ack_valid ] = "fin wait 2"_s,
"fin wait 2"_s + event<fin> [ is_fin_valid ] / send_ack = "timed wait"_s,
"timed wait"_s + event<timeout> / send_ack = X
);
// clang-format on
}
};
}
#include <boost/optional.hpp>
#include <iostream>
int main() {
using namespace sml;
boost::optional<sm<hello_world> > m;
static_assert(2 == sizeof(m), "sizeof(m) != 2b");
auto run = [&] {
assert(m->is("established"_s));
m->process_event(release{});
assert(m->is("fin wait 1"_s));
m->process_event(ack{});
assert(m->is("fin wait 2"_s));
m->process_event(fin{});
assert(m->is("timed wait"_s));
m->process_event(timeout{});
assert(m->is(X)); // released
};
for (auto i : {1,2,3}) {
std::cout << "Run " << i << "n";
m.emplace(); // default constructs a sm in-place
run();
}
}
指纹
Run 1
Run 2
Run 3
相关文章:
- 从C++实例化QML
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 设计一个只能由特定类实例化的类(如果可能的话,通过make_unique)
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 我收到以下错误:抛出'std::bad_alloc'实例后终止调用
- 建议在运行时将带有类实例的列表从c++导入qml
- 约束和显式模板实例化
- 通过实例理解std::move及其目的
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 正在生成未知类实例
- 从DLL中删除类的实例
- 在std::vector上存储带有模板的类实例
- 如何重置提升::sml::sm 实例