diff --git a/CMakeLists.txt b/CMakeLists.txt index f358df6..9c3a1e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,4 +4,4 @@ project(LO41 C) set(CMAKE_C_STANDARD 11) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall") -add_executable(LO41 main.c List/List.h List/List.c Objects.h List/Element.c List/Element.h Objects.c Building/Building.c Building/Building.h Elevator/Elevator.c Elevator/Elevator.h CommunicationBox/CommunicationBox.c CommunicationBox/CommunicationBox.h) \ No newline at end of file +add_executable(LO41 main.c List/List.h List/List.c Objects.h List/Element.c List/Element.h Objects.c Building/Building.c Building/Building.h Elevator/Elevator.c Elevator/Elevator.h CommunicationBox/CommunicationBox.c CommunicationBox/CommunicationBox.h SharedData/SharedData.c SharedData/SharedData.h) \ No newline at end of file diff --git a/Objects.h b/Objects.h index c9dee21..671bd9e 100644 --- a/Objects.h +++ b/Objects.h @@ -9,6 +9,7 @@ #include #define NEW(type, ...) _init_##type(__VA_ARGS__) +#define GET_INSTANCE(type, ...) _get_instance_##type(__VA_ARGS__) #define DELETE(obj) obj->_free_(obj) #define DESTRUCTOR(type) void (*_free_)(struct o_##type *this) #define THIS(type) type *this diff --git a/SharedData/SharedData.c b/SharedData/SharedData.c new file mode 100644 index 0000000..83bd97d --- /dev/null +++ b/SharedData/SharedData.c @@ -0,0 +1,18 @@ +// +// Created by Antoine Bartuccio on 06/06/2018. +// + +#include "SharedData.h" + +void _free__SharedData(THIS(SharedData)){ + free(this); +} + +SharedData *_get_instance_SharedData(){ + static SharedData * new_shared_data = NULL; + if (new_shared_data == NULL){ + new_shared_data = malloc_or_die(sizeof(SharedData)); + LINK(SharedData, new_shared_data, _free_); + } + return new_shared_data; +} diff --git a/SharedData/SharedData.h b/SharedData/SharedData.h new file mode 100644 index 0000000..b987897 --- /dev/null +++ b/SharedData/SharedData.h @@ -0,0 +1,17 @@ +// +// Created by Antoine Bartuccio on 06/06/2018. +// + +#ifndef LO41_SHAREDDATA_H +#define LO41_SHAREDDATA_H + +#include "../Objects.h" + +typedef struct o_SharedData { + + DESTRUCTOR(SharedData); +} SharedData; + +SharedData *_get_instance_SharedData(); + +#endif //LO41_SHAREDDATA_H diff --git a/main.c b/main.c index 5d322aa..fb2d757 100644 --- a/main.c +++ b/main.c @@ -2,11 +2,15 @@ #include "Objects.h" #include "List/List.h" #include "Building/Building.h" +#include "SharedData/SharedData.h" int main() { List *l = NEW(List); char text1[30] = "Patate"; char text2[30] = "Patator"; + + GET_INSTANCE(SharedData); + printf("La taille est de %d\n", l->get_size(l)); l->insert_tail(l, text1, sizeof(char) * 30); l->insert_head(l, text2, sizeof(char) * 30); @@ -28,5 +32,6 @@ int main() { DELETE(main_building); printf("Hello, World!\n"); + DELETE(GET_INSTANCE(SharedData)); return 0; } \ No newline at end of file