-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto.h
196 lines (170 loc) · 4.46 KB
/
proto.h
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// The MIT License (MIT)
//
// Copyright (c) 2015 Ewerton Assis <[email protected]>
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the MIT license. See LICENSE for details.
#ifndef __proto_library_h__
#define __proto_library_h__
#include <stdbool.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#ifndef T_DECIMAL
#define T_DECIMAL(d) proto_decimal (d)
#endif
#ifndef T_INTEGER
#define T_INTEGER(d) proto_integer (d)
#endif
#ifndef T_STRING
#define T_STRING(d) proto_string (d)
#endif
#ifndef T_OBJECT
#define T_OBJECT(d) proto_object (d)
#endif
#ifndef T_ARRAY
#define T_ARRAY(d) proto_array (d)
#endif
#ifndef T_BOOLEAN
#define T_BOOLEAN(d) proto_boolean (d)
#endif
#ifndef T_FUNCTION
#define T_FUNCTION(d) proto_function (d)
#endif
#ifndef T_POINTER
#define T_POINTER(d) proto_pointer (d)
#endif
#ifndef TYPE_OF
#define TYPE_OF(v) v->type
#endif
#ifndef VALUE
#define VALUE(d) proto_data_value (d)
#endif
#ifndef TYPED_VALUE
#define TYPED_VALUE(d, mode) *(mode *) (void *) proto_data_value (d)
#endif
typedef enum {
decimal_t = 0x00,
integer_t = 0x01,
string_t = 0x02,
object_t = 0x03,
array_t = 0x04,
boolean_t = 0x05,
function_t = 0x06,
pointer_t = 0x07
} proto_type_t;
typedef union {
double decimal;
long integer;
char *string;
void *object;
void *array;
bool boolean;
void *(*function) (void *arguments);
void *pointer;
} proto_typed_data_t;
typedef struct {
proto_type_t type;
proto_typed_data_t data;
} proto_data_t;
typedef struct {
size_t prototype_size;
void **prototype;
void *super;
void (*set_own_property) (void *self, const char *key, const void *value);
const void *(*get_own_property) (const void *self, const char *key);
bool (*has_own_property) (const void *self, const char *key);
const void *(*del_own_property) (void *self, const char *key);
void (*set_chain) (void *self, const char *keys, const void *value);
const void *(*get_chain) (const void *self, const char *keys);
bool (*has_chain) (const void *self, const char *key);
const void *(*execute_property) (void *self, const char *key, const void *arguments);
void (*set_super) (void *self, const void *reference);
void (*merge) (void *self, const void *reference);
} proto_object_t;
typedef struct {
size_t allocated;
size_t length;
void **items;
void (*insert) (void *self, size_t position, const void *element);
bool (*includes) (const void *self, const void *element);
const void *(*at) (const void *self, size_t position);
const void *(*del) (void *self, size_t position);
size_t (*index) (const void *self, const void *element);
void (*push) (void *self, const void *element);
const void *(*pop) (void *self);
void (*unshift) (void *self, const void *element);
const void *(*shift) (void *self);
const void *(*first) (const void *self);
const void *(*last) (const void *self);
void (*concat) (void *self, const void *list);
void *(*reverse) (const void *self);
} proto_array_t;
proto_data_t *
proto_decimal (double data);
proto_data_t *
proto_integer (long data);
proto_data_t *
proto_string (char *data);
proto_data_t *
proto_object (void *data);
proto_data_t *
proto_array (void *data);
proto_data_t *
proto_boolean (bool data);
proto_data_t *
proto_function (void *(*data) (void *arguments));
proto_data_t *
proto_pointer (void *data);
void
proto_del_data (proto_data_t *data);
proto_object_t *
proto_init_object ();
void
proto_del_object (proto_object_t *object);
proto_array_t *
proto_init_array ();
void
proto_del_array (proto_array_t *array);
void *
proto_generic_caller (const char *arguments,
void *(*function) (const void *arguments), ...);
static inline void *
proto_data_value (proto_data_t *data)
{
if (data == NULL)
return NULL;
switch (data->type)
{
case decimal_t:
return &data->data.decimal;
break;
case integer_t:
return &data->data.integer;
break;
case string_t:
return data->data.string;
break;
case object_t:
return data->data.object;
break;
case array_t:
return data->data.array;
break;
case boolean_t:
return &data->data.boolean;
break;
case function_t:
return data->data.function;
break;
case pointer_t:
return data->data.pointer;
break;
}
return NULL;
}
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // __proto_library_h__