6c50cf8b9bf416989cfcc348f39fa7c626d906cd
[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 # Utilities and performant structure for the FFI with C
18 module c
19 import standard
20 intrude import standard::collection::array
21
22 # A thin wrapper around a `NativeCArray` adding length information
23 abstract class CArray[E]
24 super AbstractArrayRead[E]
25
26 type NATIVE: NativeCArray
27 var native_array: NATIVE is noinit
28
29 private init(length: Int) do self._length = length
30
31 redef fun [](index)
32 do
33 assert not destroyed
34 assert index >= 0 and index < length
35 return native_array[index]
36 end
37
38 fun []=(index: Int, val: E)
39 do
40 assert not destroyed
41 assert index >= 0 and index < length
42 native_array[index] = val
43 end
44
45 var destroyed = false
46 fun destroy
47 do
48 if destroyed then return
49
50 native_array.free
51 destroyed = true
52 end
53 end
54
55 # A native C array, as in a pointer to the first element of the array
56 extern class NativeCArray `{ void * `}
57 type E: nullable Object
58
59 fun [](index: E): E is abstract
60 fun []=(index: E, val: E) is abstract
61
62 # Return pointer to the address to the second element of this array
63 #
64 # This is the standard `+` operator on pointers in C
65 fun +(offset: Int): SELF is abstract
66 end
67
68 # Wrapper around an array of `int` in C (`int*`) with length and destroy state
69 class CIntArray
70 super CArray[Int]
71 redef type NATIVE: NativeCIntArray
72
73 init(size: Int)
74 do
75 native_array = new NativeCIntArray(size)
76 super size
77 end
78
79 # Build from an `Array[Int]`
80 new from(array: Array[Int])
81 do
82 var carray = new CIntArray(array.length)
83 for i in array.length.times do
84 carray[i] = array[i]
85 end
86 return carray
87 end
88 end
89
90 # An array of `int` in C (`int*`)
91 extern class NativeCIntArray `{ int* `}
92 super NativeCArray
93 redef type E: Int
94
95 new(size: Int) `{ return calloc(size, sizeof(int)); `}
96 redef fun [](index) `{ return recv[index]; `}
97 redef fun []=(index, val) `{ recv[index] = val; `}
98
99 redef fun +(offset) `{ return recv + offset; `}
100 end
101
102 # Wrapper around an array of `unsigned char` in C (`unsigned char*`) with length and destroy state
103 class CByteArray
104 super CArray[Int]
105 redef type NATIVE: NativeCByteArray
106
107 # Allocate a new array of `size`
108 init(size: Int)
109 do
110 native_array = new NativeCByteArray(size)
111 super size
112 end
113
114 # Build from an `Array[Int]`
115 new from(array: Array[Int])
116 do
117 var carray = new CByteArray(array.length)
118 for i in array.length.times do
119 carray[i] = array[i]
120 end
121 return carray
122 end
123 end
124
125 # An array of `unsigned char` in C (`unsigned char*`)
126 extern class NativeCByteArray `{ unsigned char* `}
127 super NativeCArray
128 redef type E: Int
129
130 # Allocate a new array of `size`
131 new(size: Int) `{ return calloc(size, sizeof(unsigned char)); `}
132
133 redef fun [](index) `{ return recv[index]; `}
134 redef fun []=(index, val) `{ recv[index] = val; `}
135
136 redef fun +(offset) `{ return recv + offset; `}
137 end
138
139 redef class NativeString
140 super NativeCArray
141 redef type E: Char
142
143 redef fun +(offset) `{ return recv + offset; `}
144 end