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