examples: annotate examples
[nit.git] / lib / c.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Structures and services for compatibility with the C language
18 module c
19
20 import core
21 intrude import core::collection::array
22
23 # A thin wrapper around a `NativeCArray` adding length information
24 abstract class CArray[E]
25 super AbstractArrayRead[E]
26
27 # The corresponding C type
28 type NATIVE: NativeCArray
29
30 # Pointer to the real C array
31 var native_array: NATIVE is noinit
32
33 init(length: Int) is old_style_init do self._length = length
34
35 redef fun [](index)
36 do
37 assert not destroyed
38 assert index >= 0 and index < length
39 return native_array[index]
40 end
41
42 # Set `val` at `index`.
43 fun []=(index: Int, val: E)
44 do
45 assert not destroyed
46 assert index >= 0 and index < length
47 native_array[index] = val
48 end
49
50 # Was this instance destroyed?
51 #
52 # See `CArray::destroy`.
53 var destroyed = false
54
55 # Free used memory used by `native_array`.
56 #
57 # Also set `destroyed` to true.
58 fun destroy
59 do
60 if destroyed then return
61
62 native_array.free
63 destroyed = true
64 end
65 end
66
67 # A native C array, as in a pointer to the first element of the array
68 extern class NativeCArray `{ void * `}
69
70 # Type of contained elements.
71 type E: nullable Object
72
73 # Get element at `index`.
74 fun [](index: Int): E is abstract
75
76 # Set `val` at `index`.
77 fun []=(index: Int, val: E) is abstract
78
79 # Return pointer to the address to the second element of this array
80 #
81 # This is the standard `+` operator on pointers in C
82 fun +(offset: Int): SELF is abstract
83 end
84
85 # Wrapper around an array of `int` in C (`int*`) with length and destroy state
86 class CIntArray
87 super CArray[Int]
88 redef type NATIVE: NativeCIntArray
89
90 # Initialize a new CIntArray of `size` elements.
91 init(size: Int) is old_style_init do
92 native_array = new NativeCIntArray(size)
93 super size
94 end
95
96 # Create from a `SequenceRead[Int]`
97 new from(array: SequenceRead[Int])
98 do
99 var carray = new CIntArray(array.length)
100 for i in array.length.times do
101 carray[i] = array[i]
102 end
103 return carray
104 end
105 end
106
107 # An array of `int` in C (`int*`)
108 extern class NativeCIntArray `{ int* `}
109 super NativeCArray
110 redef type E: Int
111
112 # Initialize a new NativeCIntArray of `size` elements.
113 new(size: Int) `{ return calloc(size, sizeof(int)); `}
114
115 redef fun [](index) `{ return self[index]; `}
116 redef fun []=(index, val) `{ self[index] = val; `}
117
118 redef fun +(offset) `{ return self + offset; `}
119 end
120
121 # Wrapper of a C array of type `uint16_t*` with length and destroy state
122 class CUInt16Array
123 super CArray[Int]
124 redef type NATIVE: NativeCUInt16Array
125
126 # Initialize a new CIntArray of `size` elements.
127 init(size: Int) is old_style_init do
128 native_array = new NativeCUInt16Array(size)
129 super size
130 end
131
132 # Create from a `SequenceRead[Int]`
133 new from(array: SequenceRead[Int])
134 do
135 var carray = new CUInt16Array(array.length)
136 for i in array.length.times do
137 carray[i] = array[i]
138 end
139 return carray
140 end
141 end
142
143 # An array of `uint16_t` in C
144 extern class NativeCUInt16Array `{ uint16_t* `}
145 super NativeCArray
146 redef type E: Int
147
148 # Initialize a new NativeCUInt16Array of `size` elements.
149 new(size: Int) `{ return calloc(size, sizeof(uint16_t)); `}
150
151 redef fun [](index) `{ return self[index]; `}
152 redef fun []=(index, val) `{ self[index] = val; `}
153
154 redef fun +(offset) `{ return self + offset; `}
155 end
156
157 # Wrapper around an array of `unsigned char` in C (`unsigned char*`) with length and destroy state
158 class CByteArray
159 super CArray[Byte]
160 redef type NATIVE: NativeCByteArray
161
162 # Allocate a new array of `size`
163 init(size: Int) is old_style_init do
164 native_array = new NativeCByteArray(size)
165 super size
166 end
167
168 # Create from a `SequenceRead[Byte]`
169 new from(array: SequenceRead[Byte])
170 do
171 var carray = new CByteArray(array.length)
172 for i in array.length.times do
173 carray[i] = array[i]
174 end
175 return carray
176 end
177
178 # Safely move `n` bytes from `dst_offset` to `src_offset`, inside this array
179 #
180 # Require: all arguments greater than 0 and ranges within `length`
181 fun move(dst_offset, src_offset, n: Int)
182 do
183 assert dst_offset >= 0 and src_offset >= 0 and n >= 0
184 assert dst_offset + n <= length
185 assert src_offset + n <= length
186
187 native_array.move(dst_offset, src_offset, n)
188 end
189 end
190
191 # An array of `unsigned char` in C (`unsigned char*`)
192 extern class NativeCByteArray `{ unsigned char* `}
193 super NativeCArray
194 redef type E: Byte
195
196 # Allocate a new array of `size`
197 new(size: Int) `{ return calloc(size, sizeof(unsigned char)); `}
198
199 redef fun [](index) `{ return self[index]; `}
200 redef fun []=(index, val) `{ self[index] = val; `}
201
202 redef fun +(offset) `{ return self + offset; `}
203
204 # Move `n` bytes from `dst_offset` to `src_offset`
205 fun move(dst_offset, src_offset, n: Int) `{
206 memmove(self+dst_offset, self+src_offset, n);
207 `}
208 end
209
210 # Wrapper around an array of `CString` in C (`char**`) with length and destroy state.
211 class CCStringArray
212 super CArray[CString]
213
214 redef type NATIVE: NativeCStringArray
215
216 # Initialize a new NativeCStringArray of `size` elements.
217 init(size: Int) is old_style_init do
218 native_array = new NativeCStringArray(size)
219 super size
220 end
221
222 # Create from an `SequenceRead[CString]`
223 new from(array: SequenceRead[CString])
224 do
225 var carray = new CCStringArray(array.length)
226 for i in array.length.times do
227 carray[i] = array[i]
228 end
229 return carray
230 end
231 end
232
233 # An array of `CString` in C (`char**`)
234 extern class NativeCStringArray `{ char** `}
235 super NativeCArray
236
237 redef type E: CString
238
239 # Initialize a new NativeCStringArray of `size` elements.
240 new(size: Int) `{ return calloc(size, sizeof(char*)); `}
241
242 redef fun [](index) `{ return self[index]; `}
243 redef fun []=(index, val) `{ self[index] = val; `}
244 redef fun +(offset) `{ return self + offset; `}
245 end
246
247 redef class CString
248 super NativeCArray
249 redef type E: Char
250
251 redef fun +(offset) `{ return self + offset; `}
252 end