←back to thread

48 points zigrazor | 1 comments | | HN request time: 0s | source

Hi HN!

I've built [CXXStateTree](https://github.com/ZigRazor/CXXStateTree), a modern C++ header-only library to create hierarchical state machines with clean, intuitive APIs.

It supports: - Deeply nested states - Entry/exit handlers - State transitions with guards and actions - Asynchronous transitions with `co_await` (C++20 coroutines) - Optional runtime type identification for flexibility

It's ideal for complex control logic, embedded systems, games, robotics, and anywhere you'd use a finite state machine.

I’d love feedback, use cases, or contributions from the community!

Repo: https://github.com/ZigRazor/CXXStateTree

Show context
wangii ◴[] No.44527267[source]
how is it better than https://github.com/boost-ext/sml ?

there are about 1 million c++ state machines, and sml happens to be the best, or one of them. how does yours differentiate?

replies(1): >>44527997 #
canyp ◴[] No.44527997[source]
I was about to complain about the use of strings in both libraries, both for the lack of type safety as well as the possible runtime allocation, but then I looked at the assembly for the sml example and there are no strings in the binary other than the obvious "send" one.

What exactly happened there? It looks like make_transition_table() is doing some serious magic. Or are the state transitions evaluated at compile-time given that there is no input in the example, and then the transition table gets compiled out?

Anyway, I think it would help OP's library to have some assembly output in the readme as well.

replies(1): >>44528836 #
aw1621107 ◴[] No.44528836[source]
Looks like SML is using user-defined literals [0, 1] to effectively pre-process the string literal into state/event objects. Looks like the string itself is turned into a template parameter in the process and I believe those shouldn't show up in the compiled code (maybe unless there's some mangling-related thing going on?)

[0]: https://en.cppreference.com/w/cpp/language/user_literal.html

[1]: https://github.com/boost-ext/sml/blob/f232328b49adf708737bef...

replies(1): >>44537298 #
1. canyp ◴[] No.44537298{3}[source]
Thanks, I didn't know that was a thing.