State

Base template class of state derived Commands

This template class provides several convenience handlers and methods. To use this, instantiate this template class with Commands and inherit. In derived classes, the return value of each method of Commands is obtained by transferring the processing to the super class.

  1. template State(Commands, Base = Object, EnterChildHandler = void delegate(Commands)[], ExitChildHandler = EnterChildHandler, EnterHandler = void delegate()[], ExitHandler = EnterHandler)
    template State (
    Commands
    Base = Object
    EnterChildHandler = void delegate(
    Commands
    )[]
    ExitChildHandler = EnterChildHandler
    EnterHandler = void delegate(
    )[]
    ExitHandler = EnterHandler
    ) if (
    is(Commands == interface) &&
    isHandler!EnterChildHandler
    &&
    isHandler!ExitChildHandler
    &&
    isHandler!EnterHandler
    &&
    isHandler!ExitHandler
    ) {}
  2. template State(alias Policy)

Members

Classes

StateBase
class StateBase

Examples

1 interface TestCommand
2 {
3 	TestCommand command(string cmd) @safe;
4 	TestCommand update() @safe;
5 }
6 string msg;
7 
8 TestCommand test1;
9 TestCommand test2;
10 
11 static struct StatePolicy
12 {
13 	alias Commands = TestCommand;
14 	alias ExitChildHandler = void delegate(Commands);
15 }
16 static struct FlowPolicy
17 {
18 	alias Commands = TestCommand;
19 	alias EnterChildHandler = void delegate(Commands, Commands);
20 }
21 test1 = new class State!StatePolicy
22 {
23 	override TestCommand command(string cmd)
24 	{
25 		msg = cmd;
26 		// not transit
27 		return super.command(cmd);
28 	}
29 	override TestCommand update()
30 	{
31 		msg = "";
32 		// transit to child(test2)
33 		setNext(test2);
34 		return super.update();
35 	}
36 };
37 test2 = new class State!TestCommand
38 {
39 	override TestCommand command(string cmd)
40 	{
41 		msg = cmd ~ "!";
42 		// not transit
43 		return super.command(cmd);
44 	}
45 	override TestCommand update()
46 	{
47 		msg = "!";
48 		// transit to parent(test1)
49 		setNext(test1);
50 		return super.update();
51 	}
52 };
53 
54 auto sts = new Flow!FlowPolicy(test1);
55 
56 // not transit
57 sts.command("test");
58 assert(msg == "test");
59 
60 // transit to child(test2)
61 sts.update();
62 assert(msg == "");
63 
64 // not transit
65 sts.command("test");
66 assert(msg == "test!");
67 
68 // transit to parent(test1)
69 sts.update();
70 assert(msg == "!");
71 
72 // not transit
73 sts.command("test");
74 assert(msg == "test");

Meta