-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathREADME
More file actions
226 lines (158 loc) · 5.19 KB
/
README
File metadata and controls
226 lines (158 loc) · 5.19 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# What's libooc
libooc is library with object oriented written in c.
# How to install
1. Download this repository.
$ git clone https://github.com/xsoameix/libooc -b develop
2. Compile.
$ cd libooc
$ ./autogen.sh
$ make
3. Install
$ sudo make install
# Define your class
## Example: Circle
* Interface: `main.c`
#include <stdio.h>
#include <libooc/object_type.h>
#include "circle.h"
int
main(void) {
o circle = new(Circle, 2);
int area = Circle_area(circle);
printf("circle area: %d\n", area);
delete(circle);
return 0;
}
Compile:
(make sure your have installed libooc)
$ cc -o main main.c circle.c -I/usr/local/include/libooc-0.0.1 -L/usr/local/lib -looc-0.0.1
Run:
$ ./main
circle area: 4
* Implementation: `circle.c`
#include <stdlib.h>
// struct definition of Circle
#include "circle.struct.h"
// inherit form Object
O_DEF_CLASS(Circle, Object)
// constructor
override
def(ctor, void : va_list * @args) {
self->radius = va_arg(* args, int);
}
// destructor
override
def(dtor, void) {
free(self);
}
// public method
def(area, int) {
// call private method
return my_pow(self, self->radius, 2);
}
// private method
private
def(my_pow, int : int @base . int @exponent) {
int result = 1;
while(exponent > 0) {
result *= base;
exponent -= 1;
}
return result;
}
* Struct definition: `circle.struct.h`
#ifndef CIRCLE_STRCUT_H
#define CIRCLE_STRCUT_H
// get the parent's struct definition
#include <libooc/object.struct.h>
// declare the methods
#include "circle.h"
struct Circle {
struct Object super;
int radius;
};
// declare virtual table(like C++) of Circle
O_DEF_CLASS_STRUCT()
#endif
* Public methods declaration: `circle.h`
(I create a script for vim, the script can auto create/update this file according to `circle.c`)
#ifndef O_CIRCLE_H
#define O_CIRCLE_H
#include <libooc/object.h>
// Please put included files here.
// Please put macros/type declarations here.
#undef O_CLASS
#undef O_PARENT
#define O_CLASS Circle
#define O_PARENT Object
#define O_Circle_OVERRIDE_METHODS_LEN 2
#define O_Circle_PUBLIC_METHODS_LEN 1
#define O_Circle_PRIVATE_METHODS_LEN 1
#define O_Circle_OVERRIDE_METHOD_0 ctor, void, (va_list * args, args)
#define O_Circle_OVERRIDE_METHOD_1 dtor, void
#define O_Circle_PUBLIC_METHOD_0 area, int
#define O_Circle_PRIVATE_METHOD_0 my_pow, int, (int base, base), (int exponent, exponent)
#define O_Circle_ctor O_Circle_OVERRIDE_METHOD_0
#define O_Circle_dtor O_Circle_OVERRIDE_METHOD_1
#define O_Circle_area O_Circle_PUBLIC_METHOD_0
#define O_Circle_my_pow O_Circle_PRIVATE_METHOD_0
O_DEF_GLOBAL_METHODS()
#endif
# Available Classes in libooc
* Object
o obj = new(Object);
Object_class(obj); // => Object
Object_is_a(obj, Object); // => true
Object_class_name(obj); // => "Object"
String_strip(obj); // stderr => "WrongMethodError: calling String#strip on #<Object:0xdeadbeaf>"
delete(obj);
* String
o a = new(String, "a");
Object_class(a); // => String
Object_is_a(a, Object); // => true
Object_class_name(a); // => "String"
delete(a);
* Static String
Value a;
Value_type(&a, StaticString);
Value_set_str(&a, "a");
Value_get_str(&a);
* Hash
o a = new(String, "a");
o b = new(String, "b");
o hash = new(Hash);
H_SET(hash, a, b);
H_GET(hash, a); // => b
// nested function (gcc extension)
H_ITOR(print) {
printf("key: %s, value: %s\n", KEY, VALUE);
}
H_EACH(hash, print);
delete(hash);
If you have name conflict problem:
o_obj hash = new(Hash);
Hash_set(hash, a, b);
Hash_get(hash, a);
void print(o_obj key, o_obj value) {
printf("key: %s, value: %s\n", key, value);
}
Hash_each(hash, print);
* Array
o ary = new(Array);
PUSH(ary, a);
PUSH(ary, b);
ITOR(print) {
printf("%zu: %s\n", INDEX, Object_inspect(ITEM));
}
EACH(ary, print);
delete(ary);
If you have name conflict problem:
o_obj ary = new(Array);
Array_push(ary, a);
Array_push(ary, b);
void print(o_obj item, o_uint index) {
printf("%zu: %s\n", index, Object_inspect(item));
}
Array_each(ary, print);
# Copying
See the file `COPYING`.