Ajout des singletons

This commit is contained in:
Antoine Bartuccio 2018-06-06 13:50:11 +02:00
parent 00eebf97f2
commit 8b29c54d76
Signed by: klmp200
GPG Key ID: E7245548C53F904B
5 changed files with 42 additions and 1 deletions

View File

@ -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)
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)

View File

@ -9,6 +9,7 @@
#include <stdlib.h>
#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

18
SharedData/SharedData.c Normal file
View File

@ -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;
}

17
SharedData/SharedData.h Normal file
View File

@ -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

5
main.c
View File

@ -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;
}