6 ms·
If you're not willing to develop all the boilerplate code to encode your states, transitions, etc. Boost has two libraries to offer : Boost Statechart and Meta
by lgeorget 8y ago
If you're not willing to develop all the boilerplate code to encode your states, transitions, etc. Boost has two libraries to offer : Boost Statechart and Meta State Machine. One of my favorite question on Stack Overflow compares both: https://stackoverflow.com/questions/4275602/boost-statechart-vs-meta-state-machine https://stackoverflow.com/questions/4275602/boost-statechart....
As with most Boost libraries, you're in for some serious headache if you decide to use them (or is it just me?) but it's very rewarding in the end.
- nly 8y agoBoost.SML (which is experimental) has garnered a lot of attention lately as a modern take on C++ state machines that can replace both of the options you raised. https://boost-experimental.github.io/sml/ https://boost-experimental.github.io/sml/ Unfortunately I haven't seen any compelling real world usage of any of these libraries, and the documentation for SML leaves a lot to be desired.
- indiosmo 8y agoI have been using SML in production for a little over a year now without issues, mostly for protocol machines. I agree the documentation is not great though, you kind of have to go over the example code and try to figure things out on your own. Using embedded data for keeping state (e.g. counters, etc; see https://boost-experimental.github.io/sml/examples/index.html#data https://boost-experimental.github.io/sml/examples/index.html...) along with injecting std::functions to use as actions is pretty powerful. Here's a basic example I wrote some months ago showing a hierarchical machine (https://gist.github.com/indiosmo/08ab24181770125d5a2448d27f6ae99f https://gist.github.com/indiosmo/08ab24181770125d5a2448d27f6...). Also please note my usage is pretty basic as I usually have the same machine ported to C# and Python so I tend to use the smallest subset of functionality supported by all the libraries in each language.
- ephimetheus 8y agoI didn’t find a good way to handle unexpected events with SML. I know there is supposed to be a special catch all event which can do that, but that’s only for the case the event is not used in the entire FSM, which makes it pretty useless. Thinking of rolling my own based on std::variant now.
- analognoise 8y agoSeeing those examples made me realize how much I hate Modern C++, or just C++ in general. This is so belaboured; there's an abundance of what looks like line noise to me. It lacks any sense of grace whatsoever.
- lgeorget 8y agoThank you, indeed I was not aware of that one. I'll have a look.
- hermitdev 8y agoLast I looked at Boost's offerings for state machines, my team and I found the offerings lacking. I forget exactly what it was missing (it's been 6-7 years and 4 jobs ago). May have been needing to perform actions on leaving a state, but not sure. Edit: Boost is my first stop for non-standard C++ libs, but this time the offering was lacking. Previously also had issues with the date/time libs. Like they got 90% of the way there, and then didn't quite finish. Example: time zone handling. Has all of the infrastructure to support it, but no facility to really provide the necessary data. Also, their CSV of timezones was not historical, so broke down trying to do historical work for something like an energy contract in finance, where it was necessary to know if there were 23, 24 or 25 hours in a calendar day (or other value!) in a given year. Also, the interfaces for posix_time and local_date_time are a little clunky. I ended up with a facade that exposed an API more like Python's datetime lib. We also had a timezone loader to handle Olson TZ databases for the historical TZ info (and future updates) for both Linux and Windows.