Skip to content

Commit 536a194

Browse files
committed
complete state machine boiler plate
1 parent 9dbb263 commit 536a194

File tree

10 files changed

+161
-136
lines changed

10 files changed

+161
-136
lines changed

CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ find_package(libelf REQUIRED)
1212
# Find package dependancies
1313
list(APPEND CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/build/Debug/generators")
1414

15-
#Linking Libraries
16-
add_executable(${PROJECT_NAME} src/main.cpp src/elf_parser.cpp src/gcc_parse.cpp)
15+
#Creating Executable Target
16+
add_executable(${PROJECT_NAME} src/main.cpp
17+
src/elf_parser.cpp
18+
src/gcc_parse.cpp
19+
src/state_machine.cpp
20+
src/concrete_states.cpp)
1721

1822
#Linking Libraries
1923
target_link_libraries(${PROJECT_NAME} PUBLIC ctre::ctre libelf::libelf)

include/concrete_states.hpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,49 @@
44

55
class UserInputState : public State {
66
public:
7-
void enter() override;
8-
void handle() override;
9-
void exit() override;
7+
void enter(StateContext& context) override;
8+
void handle(StateContext& context) override;
9+
State* exit(StateContext& context) override;
1010
};
1111

1212
class ElfParserState : public State {
1313
public:
14-
void enter() override;
15-
void handle() override;
16-
void exit() override;
14+
void enter(StateContext& context) override;
15+
void handle(StateContext& context) override;
16+
State* exit(StateContext& context) override;
1717
};
1818

1919
class CallgraphState : public State {
2020
public:
21-
void enter() override;
22-
void handle() override;
23-
void exit() override;
21+
void enter(StateContext& context) override;
22+
void handle(StateContext& context) override;
23+
State* exit(StateContext& context) override;
2424
};
2525

2626
class AbiParserState : public State {
2727
public:
28-
void enter() override;
29-
void handle() override;
30-
void exit() override;
28+
void enter(StateContext& context) override;
29+
void handle(StateContext& context) override;
30+
State* exit(StateContext& context) override;
3131
};
3232

3333
class ValidatorState : public State {
3434
public:
35-
void enter() override;
36-
void handle() override;
37-
void exit() override;
35+
void enter(StateContext& context) override;
36+
void handle(StateContext& context) override;
37+
State* exit(StateContext& context) override;
3838
};
3939

4040
class OutputState : public State {
4141
public:
42-
void enter() override;
43-
void handle() override;
44-
void exit() override;
42+
void enter(StateContext& context) override;
43+
void handle(StateContext& context) override;
44+
State* exit(StateContext& context) override;
4545
};
4646

4747
class ErrorState : public State {
4848
public:
49-
void enter() override;
50-
void handle() override;
51-
void exit() override;
49+
void enter(StateContext& context) override;
50+
void handle(StateContext& context) override;
51+
State* exit(StateContext& context) override;
5252
};

include/state.hpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
#pragma once
2+
#include "./state_machine.hpp"
23
#include "./state_context.hpp"
34

4-
class StateContext;
5-
65
class State {
7-
protected:
8-
StateContext *context;
96
public:
10-
virtual void enter() = 0;
11-
virtual void handle() = 0;
12-
virtual void exit() = 0;
13-
14-
void set_context(StateContext *context);
7+
virtual void enter(StateContext& context) = 0;
8+
virtual void handle(StateContext& context) = 0;
9+
virtual State* exit(StateContext& context) = 0;
1510

16-
virtual ~State() = 0;
11+
virtual ~State() = default;
1712
};

include/state_context.hpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
#pragma once
2-
#include "./state.hpp"
3-
4-
class State;
52

3+
// class or struct idk
64
class StateContext {
7-
public:
8-
StateContext(State *state);
9-
~StateContext();
10-
State* get_current_state();
11-
void run_state();
12-
void transition_state(State* new_state);
13-
private:
14-
State* current_state;
15-
int total_state;
16-
};
17-
5+
public:
6+
void inc_data() {data++;}
7+
int get_data() {return data;}
8+
private:
9+
int data = 0;
10+
};

include/state_machine.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
#include "./state.hpp"
3+
#include "./state_context.hpp"
4+
5+
class State;
6+
7+
class StateMachine {
8+
public:
9+
StateMachine();
10+
~StateMachine();
11+
State* get_current_state();
12+
void run_state();
13+
void transition_state(State* new_state);
14+
private:
15+
State *current_state;
16+
StateContext context;
17+
};
18+

src/concrete_states.cpp

Lines changed: 72 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,122 @@
1-
#include "../include/concrete_state.hpp"
1+
#include "../include/concrete_states.hpp"
22
#include "../include/state.hpp"
3-
#include "../include/state_context.hpp"
3+
#include "../include/state_machine.hpp"
44

55
//User Input
6-
void UserInputState::enter() {
7-
print("Enter User Input State\n");
6+
void UserInputState::enter(StateContext& context) {
7+
std::print("Enter User Input State\n");
8+
std::print("Previous Number: {}\n", context.get_data());
89
}
910

10-
void UserInputState::handle() {
11-
print("Handle User Input State\n");
11+
void UserInputState::handle(StateContext& context) {
12+
std::print("Handle User Input State\n");
13+
context.inc_data();
1214
}
1315

14-
void UserInputState::exit() {
15-
print("Exit User Input State\n-------------\n");
16-
this->contect->transition_state(new ElfParserState);
16+
State* UserInputState::exit(StateContext& context) {
17+
std::print("Exit User Input State\n-------------\n");
18+
(void) context;
19+
return new ElfParserState;
1720
}
1821

1922
//Elf Parser
20-
void ElfParserState::enter() {
21-
print("Enter Elf Parser State\n");
23+
void ElfParserState::enter(StateContext& context) {
24+
std::print("Enter Elf Parser State\n");
25+
std::print("Previous Number: {}\n", context.get_data());
2226
}
2327

24-
void ElfParserState::handle() {
25-
print("Handle Elf Parser State\n");
28+
void ElfParserState::handle(StateContext& context) {
29+
std::print("Handle Elf Parser State\n");
30+
context.inc_data();
2631
}
2732

28-
void ElfParserState::exit() {
29-
print("Exit Elf Parser State\n-------------\n");
30-
this->context->transition_state(new CallgraphState);
33+
State* ElfParserState::exit(StateContext& context) {
34+
std::print("Exit Elf Parser State\n-------------\n");
35+
(void) context;
36+
return new CallgraphState;
3137
}
3238

3339
//Callgraph
34-
void CallgraphState::enter() {
35-
print("Enter Callgraph State\n");
40+
void CallgraphState::enter(StateContext& context) {
41+
std::print("Enter Callgraph State\n");
42+
std::print("Previous Number: {}\n", context.get_data());
3643
}
3744

38-
void CallgraphState::handle() {
39-
print("Handle Callgraph State\n");
45+
void CallgraphState::handle(StateContext& context) {
46+
context.inc_data();
47+
std::print("Handle Callgraph State\n");
4048
}
4149

42-
void CallgraphState::exit() {
43-
print("Exit Callgraph State\n-------------\n");
44-
this->context->transition_state(new AbiParserState);
50+
State* CallgraphState::exit(StateContext& context) {
51+
std::print("Exit Callgraph State\n-------------\n");
52+
(void) context;
53+
return new AbiParserState;
4554
}
4655

4756
//Abi Parser
48-
void AbiParserState::enter() {
49-
print("Enter ABI Parser State\n");
57+
void AbiParserState::enter(StateContext& context) {
58+
std::print("Enter ABI Parser State\n");
59+
std::print("Previous Number: {}\n", context.get_data());
5060
}
5161

52-
void AbiParserState::handle() {
53-
print("Handle ABI Parser State\n");
62+
void AbiParserState::handle(StateContext& context) {
63+
std::print("Handle ABI Parser State\n");
64+
context.inc_data();
5465
}
5566

56-
void AbiParserState::exit() {
57-
print("Exit ABI Parser State\n-------------\n");
58-
this->context->transition_state(new ValidatorState);
67+
State* AbiParserState::exit(StateContext& context) {
68+
std::print("Exit ABI Parser State\n-------------\n");
69+
(void) context;
70+
return new ValidatorState;
5971
}
6072

6173
//Validator
62-
void ValidatorState::enter() {
63-
print("Enter Validator State\n")
74+
void ValidatorState::enter(StateContext& context) {
75+
std::print("Enter Validator State\n");
76+
std::print("Previous Number: {}\n", context.get_data());
6477
}
6578

66-
void ValidatorState::handle() {
67-
print("Handle Validator State\n");
79+
void ValidatorState::handle(StateContext& context) {
80+
std::print("Handle Validator State\n");
81+
context.inc_data();
6882
}
6983

70-
void ValidatorState::exit() {
71-
print("Exit Validator State\n-------------\n");
72-
this->context->transition_state(new OutputState);
84+
State* ValidatorState::exit(StateContext& context) {
85+
std::print("Exit Validator State\n-------------\n");
86+
(void) context;
87+
return new OutputState;
7388
}
7489

7590
//Output
76-
void OutputState::enter() {
77-
print("Enter Output State\n");
91+
void OutputState::enter(StateContext& context) {
92+
std::print("Enter Output State\n");
93+
std::print("Previous Number: {}\n", context.get_data());
7894
}
7995

80-
void OutputState::handle() {
81-
print("Handle Output State\n");
96+
void OutputState::handle(StateContext& context) {
97+
std::print("Handle Output State\n");
98+
context.inc_data();
8299
}
83100

84-
void OutputState::exit() {
85-
print("Exit Output State\n-------------\n");
86-
this->context->transition_state(new OutputState);
101+
State* OutputState::exit(StateContext& context) {
102+
std::print("Exit Output State\n-------------\n");
103+
(void) context;
104+
return nullptr;
87105
}
88106

89107
//Error
90-
void ErrorState::enter() {
91-
print("Enter Error State\n")
108+
void ErrorState::enter(StateContext& context) {
109+
std::print("Enter Error State\n");
110+
(void) context;
92111
}
93112

94-
void ErrorState::handle() {
95-
print("Handle Error State\n");
113+
void ErrorState::handle(StateContext& context) {
114+
std::print("Handle Error State\n");
115+
(void) context;
96116
}
97117

98-
void ErrorState::exit() {
99-
print("Exit Error State\n-------------\n")
118+
State* ErrorState::exit(StateContext& context) {
119+
std::print("Exit Error State\n-------------\n");
120+
(void) context;
121+
return nullptr;
100122
}

src/main.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,15 @@ int main(int argc, char** argv)
2020
#include "state_context.hpp"
2121
#include "concrete_states.hpp"
2222

23-
void state_init() {
24-
UserInputState inital_state;
25-
StateContext *context = new StateContext(&inital_state);
26-
context->run_state();
27-
delete context;
28-
}
29-
3023
int main()
3124
{
3225
std::println("yeet: {}", __cplusplus);
3326

34-
state_init();
27+
StateMachine sm;
28+
29+
while(sm.get_current_state() != nullptr){
30+
sm.run_state();
31+
}
3532

3633
return 0;
3734
}

src/state.cpp

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/state_context.cpp

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)