Small Project Organization
Command Dispatch
An enum and a switch make command handling explicit.
enum
Use an enum when a value must be one of a small named set.
dispatch
A switch turns the command into the project action that should happen next.
Command Dispatch
command_dispatch.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
enum class Command {
Start,
Stop,
Pause
};
Command decode(int code) {
if (code == 1) {
return Command::Start;
}
if (code == 2) {
return Command::Stop;
}
return Command::Pause;
}
int main() {
int code = 2;
Command command = decode(code);
int action = 0;
switch (command) {
case Command::Start:
action = 10;
break;
case Command::Stop:
action = 20;
break;
case Command::Pause:
action = 30;
break;
}
std::cout << "code=" << code << std::endl;
std::cout << "action=" << action << std::endl;
return 0;
}
#include <iostream>
enum class Command {
Start,
Stop,
Pause
};
Command decode(int code) {
if (code == 1) {
return Command::Start;
}
if (code == 2) {
return Command::Stop;
}
return Command::Pause;
}
int main() {
int code = 1;
Command command = decode(code);
int action = 0;
switch (command) {
case Command::Start:
action = 10;
break;
case Command::Stop:
action = 20;
break;
case Command::Pause:
action = 30;
break;
}
std::cout << "code=" << code << std::endl;
std::cout << "action=" << action << std::endl;
return 0;
}
#include <iostream>
enum class Command {
Start,
Stop,
Pause
};
Command decode(int code) {
if (code == 1) {
return Command::Start;
}
if (code == 2) {
return Command::Stop;
}
return Command::Pause;
}
int main() {
int code = 3;
Command command = decode(code);
int action = 0;
switch (command) {
case Command::Start:
action = 10;
break;
case Command::Stop:
action = 20;
break;
case Command::Pause:
action = 30;
break;
}
std::cout << "code=" << code << std::endl;
std::cout << "action=" << action << std::endl;
return 0;
}
code ← 2
19int main() {20 int code→ 2 = 2; //@code=1, 32122 Command command = decode(code2);23 int action = 0;Command decode(int code)
9Command decode(int code2) {10 if (code == 1) {if (code == 2)
12}13if (code2 == 2) {14 return Command::Stop;15}command ← 1, action ← 0
22Command command→ 1 = decode(code2);23int action→ 0 = 0;24switch (command) {switch (command)
23int action = 0;24switch (command1) {25 case Command::Start:action ← 20
27 break;28case Command::Stop:29 action→ 20 = 20;30 break;action = 20;
28case Command::Stop:29 action = 20;30 break;31case Command::Pause:std::cout << "code=" << code << std::endl;
36 std::cout << "code=" << code2 << std::endl;37 std::cout << "action=" << action20 << std::endl;38 return 0;39}outputcode=2 action=20
code ← 1
19int main() {20 int code→ 1 = 1;2122 Command command = decode(code1);23 int action = 0;Command decode(int code)
9Command decode(int code1) {10 if (code == 1) {if (code == 1)
9Command decode(int code) {10 if (code1 == 1) {11 return Command::Start;12 }command ← 0, action ← 0
22Command command→ 0 = decode(code1);23int action→ 0 = 0;24switch (command) {switch (command)
23int action = 0;24switch (command0) {25 case Command::Start:action ← 10
24switch (command) {25 case Command::Start:26 action→ 10 = 10;27 break;action = 10;
25case Command::Start:26 action = 10;27 break;28case Command::Stop:std::cout << "code=" << code << std::endl;
36 std::cout << "code=" << code1 << std::endl;37 std::cout << "action=" << action10 << std::endl;38 return 0;39}outputcode=1 action=10
code ← 3
19int main() {20 int code→ 3 = 3;2122 Command command = decode(code3);23 int action = 0;Command decode(int code)
9Command decode(int code3) {10 if (code == 1) {11 return Command::Start;12 }13 if (code == 2) {14 return Command::Stop;15 }16 return Command::Pause;17}command ← 2, action ← 0
22Command command→ 2 = decode(code3);23int action→ 0 = 0;24switch (command) {switch (command)
23int action = 0;24switch (command2) {25 case Command::Start:action ← 30
30 break;31case Command::Pause:32 action→ 30 = 30;33 break;action = 30;
31 case Command::Pause:32 action = 30;33 break;34}std::cout << "code=" << code << std::endl;
36 std::cout << "code=" << code3 << std::endl;37 std::cout << "action=" << action30 << std::endl;38 return 0;39}outputcode=3 action=30