views:

1036

answers:

2

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?

+1  A: 

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

Brian
I'm reluctant to use Boost, as it seems to be a heavy library that requires a lot of integrating into the system.
zaratustra
A: 

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