From c6c59383ddb97c0b3ecf733174b31a2b28fd1ebd Mon Sep 17 00:00:00 2001 From: Phillip Johnston Date: Tue, 4 Aug 2020 10:57:58 -0700 Subject: [PATCH] Add a refactoring example --- examples/c/meson.build | 4 + examples/c/refactoring_global_data.c | 132 +++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 examples/c/refactoring_global_data.c diff --git a/examples/c/meson.build b/examples/c/meson.build index 347087f..9cb6800 100644 --- a/examples/c/meson.build +++ b/examples/c/meson.build @@ -4,6 +4,10 @@ static_library('interrupt_latency', 'interrupt_latency.c' ) +executable('refactoring_global_data', + 'refactoring_global_data.c' +) + executable('circular_buffer', [ 'circular_buffer_test.c', diff --git a/examples/c/refactoring_global_data.c b/examples/c/refactoring_global_data.c new file mode 100644 index 0000000..9a77a4f --- /dev/null +++ b/examples/c/refactoring_global_data.c @@ -0,0 +1,132 @@ +//#define USE_EXAMPLE_1 // Legacy code example +//#define USE_EXAMPLE_2 // Intermediate refactoring example +#define USE_EXAMPLE_3 // Full refactoring + +#ifdef USE_EXAMPLE_1 +#include + +int data_0 = 1; +int data_1 = 2; +int data_2 = 3; +int data_3 = 4; + +int func0(void) +{ + return data_0 + data_1; +} + +int func1(void) +{ + return data_2 + data_3; +} + +int main(void) +{ + int a = func0(); + int b = func1(); + int calculated = a + b; + printf("Calculated value: %d\n", calculated); + + return 0; +} +#elif defined(USE_EXAMPLE_2) +#include + +typedef struct { + int data_0; + int data_1; + int data_2; + int data_3; +} global_data_t; + +global_data_t instance_1 = { + .data_0 = 1, + .data_1 = 2, + .data_2 = 3, + .data_3 = 4 +}; + +global_data_t* g_data_ptr; +#define data_0 (g_data_ptr->data_0) +#define data_1 (g_data_ptr->data_1) +#define data_2 (g_data_ptr->data_2) +#define data_3 (g_data_ptr->data_3) + +int func0(void) +{ + return data_0 + data_1; +} + +int func1(void) +{ + return data_2 + data_3; +} + +int main(void) +{ + g_data_ptr = &instance_1; + + int a = func0(); + int b = func1(); + int calculated = a + b; + printf("Calculated value: %d\n", calculated); + + return 0; +} +#elif defined(USE_EXAMPLE_3) +#include + +typedef struct { + int data_0; + int data_1; + int data_2; + int data_3; +} global_data_t; + +global_data_t instance_1 = { + .data_0 = 1, + .data_1 = 2, + .data_2 = 3, + .data_3 = 4 +}; + +global_data_t instance_2 = { + .data_0 = 5, + .data_1 = 6, + .data_2 = 7, + .data_3 = 8 +}; + +global_data_t* g_data_ptr; +#define data_0 (g_data_ptr->data_0) +#define data_1 (g_data_ptr->data_1) +#define data_2 (g_data_ptr->data_2) +#define data_3 (g_data_ptr->data_3) + +int func0(global_data_t* g_data_ptr) +{ + return data_0 + data_1; +} + +int func1(global_data_t* g_data_ptr) +{ + return data_2 + data_3; +} + +int main(void) +{ + int a = func0(&instance_1); + int b = func1(&instance_1); + int calculated = a + b; + printf("Calculated value for instance_1: %d\n", calculated); + + a = func0(&instance_2); + b = func1(&instance_2); + calculated = a + b; + printf("Calculated value for instance_2: %d\n", calculated); + + return 0; +} +#else +#error Please enable an example using one of the macros shown above. +#endif