a_star: don't crash on deserialization errors and limit static types
[nit.git] / lib / c.nit
index 08c0bf1..7db5945 100644 (file)
--- a/lib/c.nit
+++ b/lib/c.nit
@@ -30,7 +30,7 @@ abstract class CArray[E]
        # Pointer to the real C array
        var native_array: NATIVE is noinit
 
-       private init(length: Int) is old_style_init do self._length = length
+       init(length: Int) is old_style_init do self._length = length
 
        redef fun [](index)
        do
@@ -93,7 +93,7 @@ class CIntArray
                super size
        end
 
-       # Create from an `SequenceRead[Int]`
+       # Create from a `SequenceRead[Int]`
        new from(array: SequenceRead[Int])
        do
                var carray = new CIntArray(array.length)
@@ -118,6 +118,42 @@ extern class NativeCIntArray `{ int* `}
        redef fun +(offset) `{ return self + offset; `}
 end
 
+# Wrapper of a C array of type `uint16_t*` with length and destroy state
+class CUInt16Array
+       super CArray[Int]
+       redef type NATIVE: NativeCUInt16Array
+
+       # Initialize a new CIntArray of `size` elements.
+       init(size: Int) is old_style_init do
+               native_array = new NativeCUInt16Array(size)
+               super size
+       end
+
+       # Create from a `SequenceRead[Int]`
+       new from(array: SequenceRead[Int])
+       do
+               var carray = new CUInt16Array(array.length)
+               for i in array.length.times do
+                       carray[i] = array[i]
+               end
+               return carray
+       end
+end
+
+# An array of `uint16_t` in C
+extern class NativeCUInt16Array `{ uint16_t* `}
+       super NativeCArray
+       redef type E: Int
+
+       # Initialize a new NativeCUInt16Array of `size` elements.
+       new(size: Int) `{ return calloc(size, sizeof(uint16_t)); `}
+
+       redef fun [](index) `{ return self[index]; `}
+       redef fun []=(index, val) `{ self[index] = val; `}
+
+       redef fun +(offset) `{ return self + offset; `}
+end
+
 # Wrapper around an array of `unsigned char` in C (`unsigned char*`) with length and destroy state
 class CByteArray
        super CArray[Byte]
@@ -142,13 +178,13 @@ class CByteArray
        # Safely move `n` bytes from `dst_offset` to `src_offset`, inside this array
        #
        # Require: all arguments greater than 0 and ranges within `length`
-       fun memmove(dst_offset, src_offset, n: Int)
+       fun move(dst_offset, src_offset, n: Int)
        do
                assert dst_offset >= 0 and src_offset >= 0 and n >= 0
                assert dst_offset + n <= length
                assert src_offset + n <= length
 
-               native_array.memmove(dst_offset, src_offset, n)
+               native_array.move(dst_offset, src_offset, n)
        end
 end
 
@@ -166,14 +202,14 @@ extern class NativeCByteArray `{ unsigned char* `}
        redef fun +(offset) `{ return self + offset; `}
 
        # Move `n` bytes from `dst_offset` to `src_offset`
-       fun memmove(dst_offset, src_offset, n: Int) `{
+       fun move(dst_offset, src_offset, n: Int) `{
                memmove(self+dst_offset, self+src_offset, n);
        `}
 end
 
-# Wrapper around an array of `NativeString` in C (`char**`) with length and destroy state.
-class CNativeStringArray
-       super CArray[NativeString]
+# Wrapper around an array of `CString` in C (`char**`) with length and destroy state.
+class CCStringArray
+       super CArray[CString]
 
        redef type NATIVE: NativeCStringArray
 
@@ -183,10 +219,10 @@ class CNativeStringArray
                super size
        end
 
-       # Create from an `SequenceRead[NativeString]`
-       new from(array: SequenceRead[NativeString])
+       # Create from an `SequenceRead[CString]`
+       new from(array: SequenceRead[CString])
        do
-               var carray = new CNativeStringArray(array.length)
+               var carray = new CCStringArray(array.length)
                for i in array.length.times do
                        carray[i] = array[i]
                end
@@ -194,11 +230,11 @@ class CNativeStringArray
        end
 end
 
-# An array of `NativeString` in C (`char**`)
+# An array of `CString` in C (`char**`)
 extern class NativeCStringArray `{ char** `}
        super NativeCArray
 
-       redef type E: NativeString
+       redef type E: CString
 
        # Initialize a new NativeCStringArray of `size` elements.
        new(size: Int) `{ return calloc(size, sizeof(char*)); `}
@@ -208,7 +244,7 @@ extern class NativeCStringArray `{ char** `}
        redef fun +(offset) `{ return self + offset; `}
 end
 
-redef class NativeString
+redef class CString
        super NativeCArray
        redef type E: Char