readme: add information section
[nit.git] / lib / c.nit
index ae1b875..076ea65 100644 (file)
--- a/lib/c.nit
+++ b/lib/c.nit
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# Utilities and performant structure for the FFI with C
+# Structures and services for compatibility with the C language
 module c
-import standard
-intrude import standard::collection::array
+
+import core
+intrude import core::collection::array
 
 # A thin wrapper around a `NativeCArray` adding length information
 abstract class CArray[E]
@@ -92,8 +93,8 @@ class CIntArray
                super size
        end
 
-       # Build from an `Array[Int]`
-       new from(array: Array[Int])
+       # Create from a `SequenceRead[Int]`
+       new from(array: SequenceRead[Int])
        do
                var carray = new CIntArray(array.length)
                for i in array.length.times do
@@ -111,15 +112,51 @@ extern class NativeCIntArray `{ int* `}
        # Initialize a new NativeCIntArray of `size` elements.
        new(size: Int) `{ return calloc(size, sizeof(int)); `}
 
-       redef fun [](index) `{ return recv[index]; `}
-       redef fun []=(index, val) `{ recv[index] = val; `}
+       redef fun [](index) `{ return self[index]; `}
+       redef fun []=(index, val) `{ self[index] = val; `}
+
+       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 recv + offset; `}
+       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[Int]
+       super CArray[Byte]
        redef type NATIVE: NativeCByteArray
 
        # Allocate a new array of `size`
@@ -128,8 +165,8 @@ class CByteArray
                super size
        end
 
-       # Build from an `Array[Int]`
-       new from(array: Array[Int])
+       # Create from a `SequenceRead[Byte]`
+       new from(array: SequenceRead[Byte])
        do
                var carray = new CByteArray(array.length)
                for i in array.length.times do
@@ -137,25 +174,79 @@ class CByteArray
                end
                return carray
        end
+
+       # Safely move `n` bytes from `dst_offset` to `src_offset`, inside this array
+       #
+       # Require: all arguments greater than 0 and ranges within `length`
+       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.move(dst_offset, src_offset, n)
+       end
 end
 
 # An array of `unsigned char` in C (`unsigned char*`)
 extern class NativeCByteArray `{ unsigned char* `}
        super NativeCArray
-       redef type E: Int
+       redef type E: Byte
 
        # Allocate a new array of `size`
        new(size: Int) `{ return calloc(size, sizeof(unsigned char)); `}
 
-       redef fun [](index) `{ return recv[index]; `}
-       redef fun []=(index, val) `{ recv[index] = val; `}
+       redef fun [](index) `{ return self[index]; `}
+       redef fun []=(index, val) `{ self[index] = val; `}
+
+       redef fun +(offset) `{ return self + offset; `}
+
+       # Move `n` bytes from `dst_offset` to `src_offset`
+       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]
+
+       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
+
+       # Create from an `SequenceRead[NativeString]`
+       new from(array: SequenceRead[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 +(offset) `{ return recv + offset; `}
+       redef fun [](index) `{ return self[index]; `}
+       redef fun []=(index, val) `{ self[index] = val; `}
+       redef fun +(offset) `{ return self + offset; `}
 end
 
 redef class NativeString
        super NativeCArray
        redef type E: Char
 
-       redef fun +(offset) `{ return recv + offset; `}
+       redef fun +(offset) `{ return self + offset; `}
 end