zaratustra
zaratustra

Reputation: 8738

Implementing event conditions in a C++ state machine

I'm using an hierarchical FSM for an embedded C++ application interface. I'd like to use small functions to determine whether certain inter-state events can be triggered, as well as use them to effect changes in the database: however, making a new class with different event functions for each state is daunting, as well as setting pointers to them as callbacks. (we're talking about 300 functions here.)

If C++ supported lambda functions, I'd use them, but I don't have a lot of weight on the project architecture, so I'm reluctant to use third-party solutions that require heavy library integration (like boost) or the inclusion of extra preprocessor steps.

Any ideas?

Upvotes: 3

Views: 2158

Answers (3)

Jay
Jay

Reputation: 14471

Have you looked at Qt's QStateMachine? It might provide some inspiration.

Upvotes: 0

Judge Maygarden
Judge Maygarden

Reputation: 27583

The new C++0x standard is coming close to finalization and acceptance. It should end up being C++09 when introduced next year. This standard will include Lambda functions directly in the language and obsolete the need for the analogous boost library. The C++ compiler in the technology preview of Visual Studio 2010 already supports Lambda functions per the Visual C++ Team Blog.

Alternatively, you could create Lua bindings for your logic and use tables to handle the FSM as shown in this wiki. This is a very flexible solution that would allow the FSM to be tweaked without recompiling your C++ project. Also, Lambda functions (as well as closures) are native and stable under Lua. Lua coroutines would also be worth investigating to simplify your FSM implementation.

Upvotes: 0

Brian
Brian

Reputation: 3575

Actually, if you use the Boost/TR1 library, there is support for Lambda functions. Details can be found on the Boost web site.

Upvotes: 2

Related Questions