|
Boost : |
From: Aleksey Gurtovoy (alexy_at_[hidden])
Date: 2002-01-29 16:40:28
David Abrahams wrote:
> I recently discovered that nontype template parameters other
> than integers are much better supported across compilers
> than I had previously expected, so now I'm on the lookout
> for uses.
And there are plenty of them - pointers to (member) functions as non-type
template parameters are indeed unexpectedly cool and useful :). For example,
here is a simple application of a compile-time state machine generator we've
wrote recently:
class player
: state_machine<player>
{
private:
typedef player self_t;
// state invariants
void stopped_state_invariant();
void playing_state_invariant();
void paused_state_invariant();
// states
typedef state<0, &self_t::stopped_state_invariant> stopped;
typedef state<1, &self_t::playing_state_invariant> playing;
typedef state<2, &self_t::paused_state_invariant> paused;
private:
// events
struct start_event;
struct stop_event;
struct pause_event;
struct resume_event;
// transition functions
bool do_start(start_event const&);
bool do_stop(stop_event const&);
bool do_pause(pause_event const&);
bool do_resume(resume_event const&);
// transitions, in the following format:
// | current state | event | next state | transition function |
friend class state_machine<player>;
typedef boost::mpl::list<
transition<stopped, start_event, playing, &self_t::do_start>
, transition<playing, stop_event, stopped, &self_t::do_stop>
, transition<playing, pause_event, paused, &self_t::do_pause>
, transition<paused, resume_event, paused, &self_t::do_resume>
>::type transition_table;
};
// event definitions
struct player::start_event
: player::event
{
};
// ...
int main()
{
player.process_event(player::start_event());
return 0;
}
IMO, a programming language that allows you to do something like this is
cool by definition :).
Aleksey
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk