3

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?

zaratustra
  • 8,148
  • 8
  • 36
  • 42

3 Answers3

2

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

Brian
  • 3,457
  • 4
  • 31
  • 41
  • I'm reluctant to use Boost, as it seems to be a heavy library that requires a lot of integrating into the system. – zaratustra Dec 02 '08 at 18:28
0

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.

Judge Maygarden
  • 26,961
  • 9
  • 82
  • 99
0

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

Jay
  • 13,803
  • 4
  • 42
  • 69