1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Structures and services for compatibility with the C language
21 intrude import core
::collection
::array
23 # A thin wrapper around a `NativeCArray` adding length information
24 abstract class CArray[E
]
25 super AbstractArrayRead[E
]
27 # The corresponding C type
28 type NATIVE: NativeCArray
30 # Pointer to the real C array
31 var native_array
: NATIVE is noinit
33 private init(length
: Int) is old_style_init
do self._length
= length
38 assert index
>= 0 and index
< length
39 return native_array
[index
]
42 # Set `val` at `index`.
43 fun []=(index
: Int, val
: E
)
46 assert index
>= 0 and index
< length
47 native_array
[index
] = val
50 # Was this instance destroyed?
52 # See `CArray::destroy`.
55 # Free used memory used by `native_array`.
57 # Also set `destroyed` to true.
60 if destroyed
then return
67 # A native C array, as in a pointer to the first element of the array
68 extern class NativeCArray `{ void * `}
70 # Type of contained elements.
71 type E: nullable Object
73 # Get element at `index
`.
74 fun [](index: Int): E is abstract
76 # Set `val
` at `index
`.
77 fun []=(index: Int, val: E) is abstract
79 # Return pointer to the address to the second element of this array
81 # This is the standard `+` operator on pointers in C
82 fun +(offset: Int): SELF is abstract
85 # Wrapper around an array of `int
` in C (`int
*`) with length and destroy state
88 redef type NATIVE: NativeCIntArray
90 # Initialize a new CIntArray of `size
` elements.
91 init(size: Int) is old_style_init do
92 native_array = new NativeCIntArray(size)
96 # Create from an `SequenceRead[Int]`
97 new from(array: SequenceRead[Int])
99 var carray = new CIntArray(array.length)
100 for i in array.length.times do
107 # An array of `int
` in C (`int
*`)
108 extern class NativeCIntArray `{ int* `}
112 # Initialize a new NativeCIntArray of `size` elements.
113 new(size
: Int) `{ return calloc(size, sizeof(int)); `}
115 redef fun [](index) `{ return self[index]; `}
116 redef fun []=(index
, val
) `{ self[index] = val; `}
118 redef fun +(offset) `{ return self + offset; `}
121 # Wrapper around an array of `unsigned char` in C (`unsigned char*`) with length and destroy state
124 redef type NATIVE: NativeCByteArray
126 # Allocate a new array of `size`
127 init(size
: Int) is old_style_init
do
128 native_array
= new NativeCByteArray(size
)
132 # Create from a `SequenceRead[Byte]`
133 new from
(array
: SequenceRead[Byte])
135 var carray
= new CByteArray(array
.length
)
136 for i
in array
.length
.times
do
142 # Safely move `n` bytes from `dst_offset` to `src_offset`, inside this array
144 # Require: all arguments greater than 0 and ranges within `length`
145 fun move
(dst_offset
, src_offset
, n
: Int)
147 assert dst_offset
>= 0 and src_offset
>= 0 and n
>= 0
148 assert dst_offset
+ n
<= length
149 assert src_offset
+ n
<= length
151 native_array
.move
(dst_offset
, src_offset
, n
)
155 # An array of `unsigned char` in C (`unsigned char*`)
156 extern class NativeCByteArray `{ unsigned char* `}
160 # Allocate a new array of `size
`
161 new(size: Int) `{ return calloc(size, sizeof(unsigned char)); `}
163 redef fun [](index
) `{ return self[index]; `}
164 redef fun []=(index, val) `{ self[index] = val; `}
166 redef fun +(offset
) `{ return self + offset; `}
168 # Move `n
` bytes from `dst_offset
` to `src_offset
`
169 fun move(dst_offset, src_offset, n: Int) `{
170 memmove
(self+dst_offset
, self+src_offset
, n
);
174 # Wrapper around an array of `NativeString` in C (`char
**`) with length and destroy state.
175 class CNativeStringArray
176 super CArray[NativeString]
178 redef type NATIVE: NativeCStringArray
180 # Initialize a new NativeCStringArray of `size
` elements.
181 init(size: Int) is old_style_init do
182 native_array = new NativeCStringArray(size)
186 # Create from an `SequenceRead[NativeString]`
187 new from(array: SequenceRead[NativeString])
189 var carray = new CNativeStringArray(array.length)
190 for i in array.length.times do
197 # An array of `NativeString` in C (`char
**`)
198 extern class NativeCStringArray `{ char** `}
201 redef type E
: NativeString
203 # Initialize a new NativeCStringArray of `size` elements.
204 new(size
: Int) `{ return calloc(size, sizeof(char*)); `}
206 redef fun [](index) `{ return self[index]; `}
207 redef fun []=(index
, val
) `{ self[index] = val; `}
208 redef fun +(offset) `{ return self + offset; `}
211 redef class NativeString
215 redef fun +(offset
) `{ return self + offset; `}