mirror of
https://gitlab.com/klmp200/LO41.git
synced 2024-11-22 08:43:22 +00:00
Ajout des singletons
This commit is contained in:
parent
00eebf97f2
commit
8b29c54d76
@ -4,4 +4,4 @@ project(LO41 C)
|
|||||||
set(CMAKE_C_STANDARD 11)
|
set(CMAKE_C_STANDARD 11)
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
|
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)
|
@ -9,6 +9,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define NEW(type, ...) _init_##type(__VA_ARGS__)
|
#define NEW(type, ...) _init_##type(__VA_ARGS__)
|
||||||
|
#define GET_INSTANCE(type, ...) _get_instance_##type(__VA_ARGS__)
|
||||||
#define DELETE(obj) obj->_free_(obj)
|
#define DELETE(obj) obj->_free_(obj)
|
||||||
#define DESTRUCTOR(type) void (*_free_)(struct o_##type *this)
|
#define DESTRUCTOR(type) void (*_free_)(struct o_##type *this)
|
||||||
#define THIS(type) type *this
|
#define THIS(type) type *this
|
||||||
|
18
SharedData/SharedData.c
Normal file
18
SharedData/SharedData.c
Normal 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
17
SharedData/SharedData.h
Normal 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
5
main.c
@ -2,11 +2,15 @@
|
|||||||
#include "Objects.h"
|
#include "Objects.h"
|
||||||
#include "List/List.h"
|
#include "List/List.h"
|
||||||
#include "Building/Building.h"
|
#include "Building/Building.h"
|
||||||
|
#include "SharedData/SharedData.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
List *l = NEW(List);
|
List *l = NEW(List);
|
||||||
char text1[30] = "Patate";
|
char text1[30] = "Patate";
|
||||||
char text2[30] = "Patator";
|
char text2[30] = "Patator";
|
||||||
|
|
||||||
|
GET_INSTANCE(SharedData);
|
||||||
|
|
||||||
printf("La taille est de %d\n", l->get_size(l));
|
printf("La taille est de %d\n", l->get_size(l));
|
||||||
l->insert_tail(l, text1, sizeof(char) * 30);
|
l->insert_tail(l, text1, sizeof(char) * 30);
|
||||||
l->insert_head(l, text2, sizeof(char) * 30);
|
l->insert_head(l, text2, sizeof(char) * 30);
|
||||||
@ -28,5 +32,6 @@ int main() {
|
|||||||
DELETE(main_building);
|
DELETE(main_building);
|
||||||
|
|
||||||
printf("Hello, World!\n");
|
printf("Hello, World!\n");
|
||||||
|
DELETE(GET_INSTANCE(SharedData));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user