lib/standard: remove the only `attr-in-refinement` warning
[nit.git] / lib / c.nit
index d94ae8e..32bd967 100644 (file)
--- a/lib/c.nit
+++ b/lib/c.nit
@@ -70,10 +70,10 @@ extern class NativeCArray `{ void * `}
        type E: nullable Object
 
        # Get element at `index`.
-       fun [](index: E): E is abstract
+       fun [](index: Int): E is abstract
 
        # Set `val` at `index`.
-       fun []=(index: E, val: E) is abstract
+       fun []=(index: Int, val: E) is abstract
 
        # Return pointer to the address to the second element of this array
        #
@@ -153,6 +153,43 @@ extern class NativeCByteArray `{ unsigned char* `}
        redef fun +(offset) `{ return recv + offset; `}
 end
 
+# Wrapper around an array of `NativeString` in C (`char**`) with length and destroy state.
+class CNativeStringArray
+       super CArray[NativeString]
+
+       redef type NATIVE: NativeCStringArray
+
+       # Initialize a new NativeCStringArray of `size` elements.
+       init(size: Int) is old_style_init do
+               native_array = new NativeCStringArray(size)
+               super size
+       end
+
+       # Build from an `Array[NativeString]`
+       new from(array: Array[NativeString])
+       do
+               var carray = new CNativeStringArray(array.length)
+               for i in array.length.times do
+                       carray[i] = array[i]
+               end
+               return carray
+       end
+end
+
+# An array of `NativeString` in C (`char**`)
+extern class NativeCStringArray `{ char** `}
+       super NativeCArray
+
+       redef type E: NativeString
+
+       # Initialize a new NativeCStringArray of `size` elements.
+       new(size: Int) `{ return calloc(size, sizeof(char*)); `}
+
+       redef fun [](index) `{ return recv[index]; `}
+       redef fun []=(index, val) `{ recv[index] = val; `}
+       redef fun +(offset) `{ return recv + offset; `}
+end
+
 redef class NativeString
        super NativeCArray
        redef type E: Char