online_ide: imports nitc (symlinked from contrib)
[nit.git] / lib / c.nit
index 3732085..a9b04e1 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]
@@ -29,7 +30,7 @@ abstract class CArray[E]
        # Pointer to the real C array
        var native_array: NATIVE is noinit
 
-       private init(length: Int) do self._length = length
+       private init(length: Int) is old_style_init do self._length = length
 
        redef fun [](index)
        do
@@ -38,6 +39,7 @@ abstract class CArray[E]
                return native_array[index]
        end
 
+       # Set `val` at `index`.
        fun []=(index: Int, val: E)
        do
                assert not destroyed
@@ -45,7 +47,14 @@ abstract class CArray[E]
                native_array[index] = val
        end
 
+       # Was this instance destroyed?
+       #
+       # See `CArray::destroy`.
        var destroyed = false
+
+       # Free used memory used by `native_array`.
+       #
+       # Also set `destroyed` to true.
        fun destroy
        do
                if destroyed then return
@@ -57,10 +66,15 @@ end
 
 # A native C array, as in a pointer to the first element of the array
 extern class NativeCArray `{ void * `}
+
+       # Type of contained elements.
        type E: nullable Object
 
-       fun [](index: E): E is abstract
-       fun []=(index: E, val: E) is abstract
+       # Get element at `index`.
+       fun [](index: Int): E is abstract
+
+       # Set `val` at `index`.
+       fun []=(index: Int, val: E) is abstract
 
        # Return pointer to the address to the second element of this array
        #
@@ -73,14 +87,14 @@ class CIntArray
        super CArray[Int]
        redef type NATIVE: NativeCIntArray
 
-       init(size: Int)
-       do
+       # Initialize a new CIntArray of `size` elements.
+       init(size: Int) is old_style_init do
                native_array = new NativeCIntArray(size)
                super size
        end
 
-       # Build from an `Array[Int]`
-       new from(array: Array[Int])
+       # Create from an `SequenceRead[Int]`
+       new from(array: SequenceRead[Int])
        do
                var carray = new CIntArray(array.length)
                for i in array.length.times do
@@ -95,27 +109,28 @@ extern class NativeCIntArray `{ int* `}
        super NativeCArray
        redef type E: 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 +(offset) `{ return recv + offset; `}
+       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[Int]
+       super CArray[Byte]
        redef type NATIVE: NativeCByteArray
 
        # Allocate a new array of `size`
-       init(size: Int)
-       do
+       init(size: Int) is old_style_init do
                native_array = new NativeCByteArray(size)
                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
@@ -128,20 +143,57 @@ 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; `}
+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