Skip to content

Commit

Permalink
add docs to public methods instead of native ones
Browse files Browse the repository at this point in the history
  • Loading branch information
kaesaecracker committed Oct 19, 2024
1 parent 67969d5 commit 1f23bc8
Show file tree
Hide file tree
Showing 8 changed files with 654 additions and 769 deletions.
223 changes: 104 additions & 119 deletions crates/servicepoint_binding_cs/ServicePoint/BitVec.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,89 +14,6 @@ namespace ServicePoint
public unsafe sealed partial class BitVec: IDisposable
{
#nullable enable
public BitVec(nuint size) : this(sp_bitvec_new(size)) {}

public static BitVec Load(byte* data, nuint data_length)
{
return new BitVec(BitVec.sp_bitvec_load(data, data_length));
}

public BitVec Clone()
{
return new BitVec(BitVec.sp_bitvec_clone(Instance));
}

public bool Get(nuint index)
{
return BitVec.sp_bitvec_get(Instance, index);
}

public void Set(nuint index, bool value)
{
BitVec.sp_bitvec_set(Instance, index, value);
}

public void Fill(bool value)
{
BitVec.sp_bitvec_fill(Instance, value);
}

public nuint Len()
{
return BitVec.sp_bitvec_len(Instance);
}

public bool IsEmpty()
{
return BitVec.sp_bitvec_is_empty(Instance);
}

public SPByteSlice UnsafeDataRef()
{
return BitVec.sp_bitvec_unsafe_data_ref(Instance);
}


private SPBitVec* _instance;
internal SPBitVec* Instance
{
get
{
if (_instance == null)
throw new NullReferenceException("instance is null");
return _instance;
}
}

private BitVec(SPBitVec* instance)
{
ArgumentNullException.ThrowIfNull(instance);
_instance = instance;
}

internal SPBitVec* Into()
{
var instance = Instance;
_instance = null;
return instance;
}

private void Free()
{
if (_instance != null)
BitVec.sp_bitvec_free(Into());
}

public void Dispose()
{
Free();
GC.SuppressFinalize(this);
}

~BitVec() => Free();

const string __DllName = "servicepoint_binding_c";
#nullable restore
/// <summary>
/// Creates a new [SPBitVec] instance.
///
Expand All @@ -117,8 +34,7 @@ public void Dispose()
/// - the returned instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_bitvec_free`.
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_bitvec_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern SPBitVec* sp_bitvec_new(nuint size);
public BitVec(nuint size) : this(sp_bitvec_new(size)) {}

/// <summary>
/// Interpret the data as a series of bits and load then into a new [SPBitVec] instance.
Expand All @@ -138,8 +54,10 @@ public void Dispose()
/// - the returned instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_bitvec_free`.
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_bitvec_load", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern SPBitVec* sp_bitvec_load(byte* data, nuint data_length);
public static BitVec Load(byte* data, nuint data_length)
{
return new BitVec(BitVec.sp_bitvec_load(data, data_length));
}

/// <summary>
/// Clones a [SPBitVec].
Expand All @@ -159,26 +77,10 @@ public void Dispose()
/// - the returned instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_bitvec_free`.
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_bitvec_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern SPBitVec* sp_bitvec_clone(SPBitVec* bit_vec);

/// <summary>
/// Deallocates a [SPBitVec].
///
/// # Panics
///
/// - when `but_vec` is NULL
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `bit_vec` points to a valid [SPBitVec]
/// - `bit_vec` is not used concurrently or after this call
/// - `bit_vec` was not passed to another consuming function, e.g. to create a [SPCommand]
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_bitvec_free", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern void sp_bitvec_free(SPBitVec* bit_vec);
public BitVec Clone()
{
return new BitVec(BitVec.sp_bitvec_clone(Instance));
}

/// <summary>
/// Gets the value of a bit from the [SPBitVec].
Expand All @@ -202,9 +104,10 @@ public void Dispose()
/// - `bit_vec` points to a valid [SPBitVec]
/// - `bit_vec` is not written to concurrently
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_bitvec_get", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[return: MarshalAs(UnmanagedType.U1)]
private static extern bool sp_bitvec_get(SPBitVec* bit_vec, nuint index);
public bool Get(nuint index)
{
return BitVec.sp_bitvec_get(Instance, index);
}

/// <summary>
/// Sets the value of a bit in the [SPBitVec].
Expand All @@ -227,8 +130,10 @@ public void Dispose()
/// - `bit_vec` points to a valid [SPBitVec]
/// - `bit_vec` is not written to or read from concurrently
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_bitvec_set", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern void sp_bitvec_set(SPBitVec* bit_vec, nuint index, [MarshalAs(UnmanagedType.U1)] bool value);
public void Set(nuint index, bool value)
{
BitVec.sp_bitvec_set(Instance, index, value);
}

/// <summary>
/// Sets the value of all bits in the [SPBitVec].
Expand All @@ -249,8 +154,10 @@ public void Dispose()
/// - `bit_vec` points to a valid [SPBitVec]
/// - `bit_vec` is not written to or read from concurrently
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_bitvec_fill", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern void sp_bitvec_fill(SPBitVec* bit_vec, [MarshalAs(UnmanagedType.U1)] bool value);
public void Fill(bool value)
{
BitVec.sp_bitvec_fill(Instance, value);
}

/// <summary>
/// Gets the length of the [SPBitVec] in bits.
Expand All @@ -269,8 +176,10 @@ public void Dispose()
///
/// - `bit_vec` points to a valid [SPBitVec]
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_bitvec_len", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern nuint sp_bitvec_len(SPBitVec* bit_vec);
public nuint Len()
{
return BitVec.sp_bitvec_len(Instance);
}

/// <summary>
/// Returns true if length is 0.
Expand All @@ -289,9 +198,10 @@ public void Dispose()
///
/// - `bit_vec` points to a valid [SPBitVec]
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_bitvec_is_empty", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[return: MarshalAs(UnmanagedType.U1)]
private static extern bool sp_bitvec_is_empty(SPBitVec* bit_vec);
public bool IsEmpty()
{
return BitVec.sp_bitvec_is_empty(Instance);
}

/// <summary>
/// Gets an unsafe reference to the data of the [SPBitVec] instance.
Expand All @@ -312,6 +222,81 @@ public void Dispose()
/// - the returned memory range is never accessed after the passed [SPBitVec] has been freed
/// - the returned memory range is never accessed concurrently, either via the [SPBitVec] or directly
/// </summary>
public SPByteSlice UnsafeDataRef()
{
return BitVec.sp_bitvec_unsafe_data_ref(Instance);
}


private SPBitVec* _instance;
internal SPBitVec* Instance
{
get
{
if (_instance == null)
throw new NullReferenceException("instance is null");
return _instance;
}
}

private BitVec(SPBitVec* instance)
{
ArgumentNullException.ThrowIfNull(instance);
_instance = instance;
}

internal SPBitVec* Into()
{
var instance = Instance;
_instance = null;
return instance;
}

private void Free()
{
if (_instance != null)
BitVec.sp_bitvec_free(Into());
}

public void Dispose()
{
Free();
GC.SuppressFinalize(this);
}

~BitVec() => Free();

const string __DllName = "servicepoint_binding_c";
#nullable restore
[DllImport(__DllName, EntryPoint = "sp_bitvec_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern SPBitVec* sp_bitvec_new(nuint size);

[DllImport(__DllName, EntryPoint = "sp_bitvec_load", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern SPBitVec* sp_bitvec_load(byte* data, nuint data_length);

[DllImport(__DllName, EntryPoint = "sp_bitvec_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern SPBitVec* sp_bitvec_clone(SPBitVec* bit_vec);

[DllImport(__DllName, EntryPoint = "sp_bitvec_free", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern void sp_bitvec_free(SPBitVec* bit_vec);

[DllImport(__DllName, EntryPoint = "sp_bitvec_get", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[return: MarshalAs(UnmanagedType.U1)]
private static extern bool sp_bitvec_get(SPBitVec* bit_vec, nuint index);

[DllImport(__DllName, EntryPoint = "sp_bitvec_set", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern void sp_bitvec_set(SPBitVec* bit_vec, nuint index, [MarshalAs(UnmanagedType.U1)] bool value);

[DllImport(__DllName, EntryPoint = "sp_bitvec_fill", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern void sp_bitvec_fill(SPBitVec* bit_vec, [MarshalAs(UnmanagedType.U1)] bool value);

[DllImport(__DllName, EntryPoint = "sp_bitvec_len", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern nuint sp_bitvec_len(SPBitVec* bit_vec);

[DllImport(__DllName, EntryPoint = "sp_bitvec_is_empty", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[return: MarshalAs(UnmanagedType.U1)]
private static extern bool sp_bitvec_is_empty(SPBitVec* bit_vec);

[DllImport(__DllName, EntryPoint = "sp_bitvec_unsafe_data_ref", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern SPByteSlice sp_bitvec_unsafe_data_ref(SPBitVec* bit_vec);

Expand Down
Loading

0 comments on commit 1f23bc8

Please sign in to comment.