misc/vim: inform the user when no results are found
[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) is old_style_init 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 # Set `val` at `index`.
42 fun []=(index: Int, val: E)
43 do
44 assert not destroyed
45 assert index >= 0 and index < length
46 native_array[index] = val
47 end
48
49 # Was this instance destroyed?
50 #
51 # See `CArray::destroy`.
52 var destroyed = false
53
54 # Free used memory used by `native_array`.
55 #
56 # Also set `destroyed` to true.
57 fun destroy
58 do
59 if destroyed then return
60
61 native_array.free
62 destroyed = true
63 end
64 end
65
66 # A native C array, as in a pointer to the first element of the array
67 extern class NativeCArray `{ void * `}
68
69 # Type of contained elements.
70 type E: nullable Object
71
72 # Get element at `index`.
73 fun [](index: Int): E is abstract
74
75 # Set `val` at `index`.
76 fun []=(index: Int, val: E) is abstract
77
78 # Return pointer to the address to the second element of this array
79 #
80 # This is the standard `+` operator on pointers in C
81 fun +(offset: Int): SELF is abstract
82 end
83
84 # Wrapper around an array of `int` in C (`int*`) with length and destroy state
85 class CIntArray
86 super CArray[Int]
87 redef type NATIVE: NativeCIntArray
88
89 # Initialize a new CIntArray of `size` elements.
90 init(size: Int) is old_style_init do
91 native_array = new NativeCIntArray(size)
92 super size
93 end
94
95 # Build from an `Array[Int]`
96 new from(array: Array[Int])
97 do
98 var carray = new CIntArray(array.length)
99 for i in array.length.times do
100 carray[i] = array[i]
101 end
102 return carray
103 end
104 end
105
106 # An array of `int` in C (`int*`)
107 extern class NativeCIntArray `{ int* `}
108 super NativeCArray
109 redef type E: Int
110
111 # Initialize a new NativeCIntArray of `size` elements.
112 new(size: Int) `{ return calloc(size, sizeof(int)); `}
113
114 redef fun [](index) `{ return recv[index]; `}
115 redef fun []=(index, val) `{ recv[index] = val; `}
116
117 redef fun +(offset) `{ return recv + offset; `}
118 end
119
120 # Wrapper around an array of `unsigned char` in C (`unsigned char*`) with length and destroy state
121 class CByteArray
122 super CArray[Int]
123 redef type NATIVE: NativeCByteArray
124
125 # Allocate a new array of `size`
126 init(size: Int) is old_style_init do
127 native_array = new NativeCByteArray(size)
128 super size
129 end
130
131 # Build from an `Array[Int]`
132 new from(array: Array[Int])
133 do
134 var carray = new CByteArray(array.length)
135 for i in array.length.times do
136 carray[i] = array[i]
137 end
138 return carray
139 end
140 end
141
142 # An array of `unsigned char` in C (`unsigned char*`)
143 extern class NativeCByteArray `{ unsigned char* `}
144 super NativeCArray
145 redef type E: Int
146
147 # Allocate a new array of `size`
148 new(size: Int) `{ return calloc(size, sizeof(unsigned char)); `}
149
150 redef fun [](index) `{ return recv[index]; `}
151 redef fun []=(index, val) `{ recv[index] = val; `}
152
153 redef fun +(offset) `{ return recv + offset; `}
154 end
155
156 redef class NativeString
157 super NativeCArray
158 redef type E: Char
159
160 redef fun +(offset) `{ return recv + offset; `}
161 end