fenbf / cppstories-discussions

4 stars 1 forks source link

2023/finite-state-machines-variant-cpp/ #122

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

Finite State Machine with std::variant - C++ Stories

In this blog post, I’ll show you how to convert a “regular” enum-style finite state machine into a modern version based on std::variant from C++17. This technique allows you to improve design, work with value types and enhance code quality. States   Let’s start with a basic example: we want to track a game player’s health status we’d like to respond to events like “Hit by a monster” or “Healing bonus.

https://www.cppstories.com/2023/finite-state-machines-variant-cpp/

2kaud commented 1 year ago

"Here’s a basic diagram with states and transitions:"

I think a gremlin has got in and eaten it...

2kaud commented 1 year ago

PlayerAlive and PlayerDead both have a member remainingLives_. Couldn't PlayerAlive be derived from PlayerDead and thus only have remainingLives once? The living arising from the dead!

fenbf commented 1 year ago

thanks @2kaud! I've just fixed the state diagram, The states are just flat structures, but we can improve them and derive if needed.

rmerriam commented 1 year ago

Since this is a game performance would be important. The std::variant version is using a number of library functions which the compile may not be able to optimize as well as the enum version.

PiotrNycz commented 1 year ago

Besides one disadvantage you already mentioned (size being max size of all events) - the other disadvantage is the increase of compilation time. For big codebases with 100s of events - it can be a problem - comparing to e.g. classic GoF state pattern with event being represented by base class and all its variants being only forward declared. But maybe with C++20 modules it can be not so much visible.

ninkibah commented 1 year ago

Another plus for the variant implementation, is that it's easy to test the individual onEvent() functions.