-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop_interface.c
More file actions
149 lines (128 loc) · 3.52 KB
/
oop_interface.c
File metadata and controls
149 lines (128 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
/* communication interface */
typedef struct{
struct com_func_table *func_table;
}communication;
/* virtual table (a table of function pointers)*/
typedef struct com_func_table{
void(*receive)(communication * obj);
void(*send)(communication * obj);
void(*destroy)(communication * obj);
}com_func_table;
/*UART class*/
/*attributes*/
typedef struct{
communication super;
char *payload;
}uart;
/* virtual table function uart*/
typedef struct{
com_func_table super;
void(*receive_new)(communication * obj, char *buffer);
}uart_func_table;
/*methods*/
void uart_receive(communication * obj){
uart *uart_obj = (uart *)obj;
printf("receive uart data : %s",uart_obj->payload );
}
void uart_receive_new(communication * obj, char * buffer){
uart *uart_obj = (uart *)obj;
uart_obj->payload = buffer;
}
void uart_send(communication * obj){
uart *uart_obj = (uart *)obj;
printf("send uart data : %s",uart_obj->payload );
}
void uart_destroy(communication * obj){
free(obj);
}
uart_func_table uart_func = {
{
uart_receive,
uart_send,
uart_destroy
},
uart_receive_new
};
communication *uart_new(){
uart * obj = (uart *) malloc(sizeof(uart));
obj->super.func_table =(struct com_func_table *)&uart_func;
obj->payload = malloc(sizeof(char)*50);
return (communication *)obj;
}
/*I2C class*/
/*attributes*/
typedef struct{
communication super;
char *payload;
int addr;
}i2c;
/* virtual table function i2c*/
typedef struct{
com_func_table super;
void(*receive_new)(communication * obj, char *buffer);
}i2c_func_table;
/*methods*/
void i2c_receive(communication * obj){
i2c *i2c_obj = (i2c *)obj;
printf("receive i2c data : %s",i2c_obj->payload );
}
void i2c_receive_new(communication * obj, char * buffer){
i2c *i2c_obj = (i2c *)obj;
i2c_obj->payload = buffer;
}
void i2c_send(communication * obj){
i2c *i2c_obj = (i2c *)obj;
printf("send i2c data : %s",i2c_obj->payload );
}
void i2c_destroy(communication * obj){
free(obj);
}
i2c_func_table i2c_func = {
{
i2c_receive,
i2c_send,
i2c_destroy
},
i2c_receive_new
};
communication *i2c_new(int addr){
i2c * obj = (i2c *) malloc(sizeof(i2c));
obj->super.func_table =(struct com_func_table *)&i2c_func;
obj->payload = malloc(sizeof(char)*50);
obj->addr = addr;
return (communication *)obj;
}
/*virtual call (late binding)*/
#define COM_RECEIVE(obj) ((( communication *) ( obj )) -> func_table -> receive (obj))
#define COM_SEND(obj) ((( communication *) ( obj )) -> func_table -> send (obj))
#define UART_RECEIVE_NEW(obj, b) ((uart_func_table *)(( communication *) ( obj )) -> func_table) -> receive_new (obj, b)
#define I2C_RECEIVE_NEW(obj, b) ((i2c_func_table *)(( communication *) ( obj )) -> func_table) -> receive_new (obj, b)
#define COM_DESTROY(obj) ((( communication *) ( obj )) -> func_table -> receive (obj))
int main () {
communication *uart_comm;
communication *i2c_comm;
uart_comm = uart_new();
i2c_comm = i2c_new(0x12);
UART_RECEIVE_NEW(uart_comm, "uart payload\n");
COM_RECEIVE(uart_comm);
uart* uart_data = (uart*)uart_comm;
i2c* i2c_data = (i2c*)i2c_comm;
i2c_data->payload = uart_data->payload;
COM_SEND((communication *)i2c_data);
I2C_RECEIVE_NEW(i2c_comm , "i2c payload\n");
COM_RECEIVE(i2c_comm );
uart_data->payload = i2c_data->payload;
COM_SEND((communication *)uart_data);
uart_destroy(uart_comm);
i2c_destroy(i2c_comm);
}
/*
Result:
receive uart data : uart payload
send i2c data : uart payload
receive i2c data : i2c payload
send uart data : i2c payload
*/