CCommand (opaque wrapper around Command for C)

This commit is contained in:
Vinzenz Schroeter 2024-08-29 20:39:42 +02:00
parent d2369446ef
commit 956e5df812
6 changed files with 107 additions and 82 deletions

View file

@ -137,6 +137,11 @@ typedef struct sp_CBitVec sp_CBitVec;
*/
typedef struct sp_CBrightnessGrid sp_CBrightnessGrid;
/**
* Opaque struct needed for correct code generation.
*/
typedef struct sp_CCommand sp_CCommand;
/**
* A C-wrapper for grid containing codepage 437 characters.
*
@ -588,7 +593,7 @@ size_t sp_brightness_grid_width(const struct sp_CBrightnessGrid *this_);
* - the returned `Command` instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_dealloc`.
*/
struct sp_Command *sp_command_bitmap_linear(sp_Offset offset,
struct sp_CCommand *sp_command_bitmap_linear(sp_Offset offset,
struct sp_CBitVec *bit_vec,
sp_CompressionCode compression);
@ -606,7 +611,7 @@ struct sp_Command *sp_command_bitmap_linear(sp_Offset offset,
* - the returned `Command` instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_dealloc`.
*/
struct sp_Command *sp_command_bitmap_linear_and(sp_Offset offset,
struct sp_CCommand *sp_command_bitmap_linear_and(sp_Offset offset,
struct sp_CBitVec *bit_vec,
sp_CompressionCode compression);
@ -624,7 +629,7 @@ struct sp_Command *sp_command_bitmap_linear_and(sp_Offset offset,
* - the returned `Command` instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_dealloc`.
*/
struct sp_Command *sp_command_bitmap_linear_or(sp_Offset offset,
struct sp_CCommand *sp_command_bitmap_linear_or(sp_Offset offset,
struct sp_CBitVec *bit_vec,
sp_CompressionCode compression);
@ -642,7 +647,7 @@ struct sp_Command *sp_command_bitmap_linear_or(sp_Offset offset,
* - the returned `Command` instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_dealloc`.
*/
struct sp_Command *sp_command_bitmap_linear_win(size_t x,
struct sp_CCommand *sp_command_bitmap_linear_win(size_t x,
size_t y,
struct sp_PixelGrid *pixel_grid,
sp_CompressionCode compression_code);
@ -661,7 +666,7 @@ struct sp_Command *sp_command_bitmap_linear_win(size_t x,
* - the returned `Command` instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_dealloc`.
*/
struct sp_Command *sp_command_bitmap_linear_xor(sp_Offset offset,
struct sp_CCommand *sp_command_bitmap_linear_xor(sp_Offset offset,
struct sp_CBitVec *bit_vec,
sp_CompressionCode compression);
@ -680,7 +685,7 @@ struct sp_Command *sp_command_bitmap_linear_xor(sp_Offset offset,
* - the returned `Command` instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_dealloc`.
*/
struct sp_Command *sp_command_brightness(uint8_t brightness);
struct sp_CCommand *sp_command_brightness(uint8_t brightness);
/**
* Allocates a new `Command::CharBrightness` instance.
@ -695,7 +700,7 @@ struct sp_Command *sp_command_brightness(uint8_t brightness);
* - the returned `Command` instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_dealloc`.
*/
struct sp_Command *sp_command_char_brightness(size_t x,
struct sp_CCommand *sp_command_char_brightness(size_t x,
size_t y,
struct sp_CBrightnessGrid *byte_grid);
@ -709,7 +714,7 @@ struct sp_Command *sp_command_char_brightness(size_t x,
* - the returned `Command` instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_dealloc`.
*/
struct sp_Command *sp_command_clear(void);
struct sp_CCommand *sp_command_clear(void);
/**
* Clones a `Command` instance.
@ -723,7 +728,7 @@ struct sp_Command *sp_command_clear(void);
* - the returned `Command` instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_dealloc`.
*/
struct sp_Command *sp_command_clone(const struct sp_Command *original);
struct sp_CCommand *sp_command_clone(const struct sp_CCommand *original);
/**
* Allocates a new `Command::Cp437Data` instance.
@ -738,7 +743,7 @@ struct sp_Command *sp_command_clone(const struct sp_Command *original);
* - the returned `Command` instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_dealloc`.
*/
struct sp_Command *sp_command_cp437_data(size_t x,
struct sp_CCommand *sp_command_cp437_data(size_t x,
size_t y,
struct sp_CCp437Grid *byte_grid);
@ -753,7 +758,7 @@ struct sp_Command *sp_command_cp437_data(size_t x,
* - `this` is not used concurrently or after this call
* - `this` was not passed to another consuming function, e.g. to create a `Packet`
*/
void sp_command_dealloc(struct sp_Command *ptr);
void sp_command_dealloc(struct sp_CCommand *ptr);
/**
* Allocates a new `Command::FadeOut` instance.
@ -765,7 +770,7 @@ void sp_command_dealloc(struct sp_Command *ptr);
* - the returned `Command` instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_dealloc`.
*/
struct sp_Command *sp_command_fade_out(void);
struct sp_CCommand *sp_command_fade_out(void);
/**
* Allocates a new `Command::HardReset` instance.
@ -777,7 +782,7 @@ struct sp_Command *sp_command_fade_out(void);
* - the returned `Command` instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_dealloc`.
*/
struct sp_Command *sp_command_hard_reset(void);
struct sp_CCommand *sp_command_hard_reset(void);
/**
* Tries to turn a `Packet` into a `Command`. The packet is deallocated in the process.
@ -794,7 +799,7 @@ struct sp_Command *sp_command_hard_reset(void);
* - the returned `Command` instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_dealloc`.
*/
struct sp_Command *sp_command_try_from_packet(struct sp_Packet *packet);
struct sp_CCommand *sp_command_try_from_packet(struct sp_Packet *packet);
/**
* Closes and deallocates a `Connection`.

View file

@ -9,7 +9,7 @@ int main(void) {
sp_PixelGrid *pixels = sp_pixel_grid_new(sp_PIXEL_WIDTH, sp_PIXEL_HEIGHT);
sp_pixel_grid_fill(pixels, true);
sp_Command *command = sp_command_bitmap_linear_win(0, 0, pixels, Uncompressed);
sp_CCommand *command = sp_command_bitmap_linear_win(0, 0, pixels, Uncompressed);
sp_Packet *packet = sp_packet_from_command(command);
if (!sp_connection_send(connection, packet))
return 1;

View file

@ -12,6 +12,15 @@ use crate::bit_vec::CBitVec;
use crate::brightness_grid::CBrightnessGrid;
use crate::cp437_grid::CCp437Grid;
/// Opaque struct needed for correct code generation.
pub struct CCommand(pub(crate) Command);
impl Clone for CCommand {
fn clone(&self) -> Self {
CCommand(self.0.clone())
}
}
/// Tries to turn a `Packet` into a `Command`. The packet is deallocated in the process.
///
/// Returns: pointer to new `Command` instance or NULL
@ -28,11 +37,11 @@ use crate::cp437_grid::CCp437Grid;
#[no_mangle]
pub unsafe extern "C" fn sp_command_try_from_packet(
packet: *mut Packet,
) -> *mut Command {
) -> *mut CCommand {
let packet = *Box::from_raw(packet);
match Command::try_from(packet) {
Err(_) => null_mut(),
Ok(command) => Box::into_raw(Box::new(command)),
Ok(command) => Box::into_raw(Box::new(CCommand(command))),
}
}
@ -48,8 +57,8 @@ pub unsafe extern "C" fn sp_command_try_from_packet(
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_clone(
original: *const Command,
) -> *mut Command {
original: *const CCommand,
) -> *mut CCommand {
Box::into_raw(Box::new((*original).clone()))
}
@ -62,8 +71,8 @@ pub unsafe extern "C" fn sp_command_clone(
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_clear() -> *mut Command {
Box::into_raw(Box::new(Command::Clear))
pub unsafe extern "C" fn sp_command_clear() -> *mut CCommand {
Box::into_raw(Box::new(CCommand(Command::Clear)))
}
/// Allocates a new `Command::HardReset` instance.
@ -75,8 +84,8 @@ pub unsafe extern "C" fn sp_command_clear() -> *mut Command {
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_hard_reset() -> *mut Command {
Box::into_raw(Box::new(Command::HardReset))
pub unsafe extern "C" fn sp_command_hard_reset() -> *mut CCommand {
Box::into_raw(Box::new(CCommand(Command::HardReset)))
}
/// Allocates a new `Command::FadeOut` instance.
@ -88,8 +97,8 @@ pub unsafe extern "C" fn sp_command_hard_reset() -> *mut Command {
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_fade_out() -> *mut Command {
Box::into_raw(Box::new(Command::FadeOut))
pub unsafe extern "C" fn sp_command_fade_out() -> *mut CCommand {
Box::into_raw(Box::new(CCommand(Command::FadeOut)))
}
/// Allocates a new `Command::Brightness` instance for setting the brightness of all tiles to the
@ -106,10 +115,12 @@ pub unsafe extern "C" fn sp_command_fade_out() -> *mut Command {
/// - the returned `Command` instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_command_brightness(brightness: u8) -> *mut Command {
pub unsafe extern "C" fn sp_command_brightness(
brightness: u8,
) -> *mut CCommand {
let brightness =
Brightness::try_from(brightness).expect("invalid brightness");
Box::into_raw(Box::new(Command::Brightness(brightness)))
Box::into_raw(Box::new(CCommand(Command::Brightness(brightness))))
}
/// Allocates a new `Command::CharBrightness` instance.
@ -128,12 +139,12 @@ pub unsafe extern "C" fn sp_command_char_brightness(
x: usize,
y: usize,
byte_grid: *mut CBrightnessGrid,
) -> *mut Command {
) -> *mut CCommand {
let byte_grid = *Box::from_raw(byte_grid);
Box::into_raw(Box::new(Command::CharBrightness(
Box::into_raw(Box::new(CCommand(Command::CharBrightness(
Origin::new(x, y),
byte_grid.0,
)))
))))
}
/// Allocates a new `Command::BitmapLinear` instance.
@ -153,13 +164,13 @@ pub unsafe extern "C" fn sp_command_bitmap_linear(
offset: Offset,
bit_vec: *mut CBitVec,
compression: CompressionCode,
) -> *mut Command {
) -> *mut CCommand {
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(Command::BitmapLinear(
Box::into_raw(Box::new(CCommand(Command::BitmapLinear(
offset,
bit_vec.into(),
compression,
)))
))))
}
/// Allocates a new `Command::BitmapLinearAnd` instance.
@ -179,13 +190,13 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_and(
offset: Offset,
bit_vec: *mut CBitVec,
compression: CompressionCode,
) -> *mut Command {
) -> *mut CCommand {
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(Command::BitmapLinearAnd(
Box::into_raw(Box::new(CCommand(Command::BitmapLinearAnd(
offset,
bit_vec.into(),
compression,
)))
))))
}
/// Allocates a new `Command::BitmapLinearOr` instance.
@ -205,13 +216,13 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_or(
offset: Offset,
bit_vec: *mut CBitVec,
compression: CompressionCode,
) -> *mut Command {
) -> *mut CCommand {
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(Command::BitmapLinearOr(
Box::into_raw(Box::new(CCommand(Command::BitmapLinearOr(
offset,
bit_vec.into(),
compression,
)))
))))
}
/// Allocates a new `Command::BitmapLinearXor` instance.
@ -231,13 +242,13 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_xor(
offset: Offset,
bit_vec: *mut CBitVec,
compression: CompressionCode,
) -> *mut Command {
) -> *mut CCommand {
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(Command::BitmapLinearXor(
Box::into_raw(Box::new(CCommand(Command::BitmapLinearXor(
offset,
bit_vec.into(),
compression,
)))
))))
}
/// Allocates a new `Command::Cp437Data` instance.
@ -256,9 +267,12 @@ pub unsafe extern "C" fn sp_command_cp437_data(
x: usize,
y: usize,
byte_grid: *mut CCp437Grid,
) -> *mut Command {
) -> *mut CCommand {
let byte_grid = *Box::from_raw(byte_grid);
Box::into_raw(Box::new(Command::Cp437Data(Origin::new(x, y), byte_grid.0)))
Box::into_raw(Box::new(CCommand(Command::Cp437Data(
Origin::new(x, y),
byte_grid.0,
))))
}
/// Allocates a new `Command::BitmapLinearWin` instance.
@ -279,13 +293,13 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_win(
y: usize,
pixel_grid: *mut PixelGrid,
compression_code: CompressionCode,
) -> *mut Command {
) -> *mut CCommand {
let byte_grid = *Box::from_raw(pixel_grid);
Box::into_raw(Box::new(Command::BitmapLinearWin(
Box::into_raw(Box::new(CCommand(Command::BitmapLinearWin(
Origin::new(x, y),
byte_grid,
compression_code,
)))
))))
}
/// Deallocates a `Command`.
@ -298,6 +312,6 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_win(
/// - `this` is not used concurrently or after this call
/// - `this` was not passed to another consuming function, e.g. to create a `Packet`
#[no_mangle]
pub unsafe extern "C" fn sp_command_dealloc(ptr: *mut Command) {
pub unsafe extern "C" fn sp_command_dealloc(ptr: *mut CCommand) {
_ = Box::from_raw(ptr);
}

View file

@ -4,7 +4,8 @@
use std::ptr::null_mut;
use servicepoint::{Command, Packet};
use crate::command::CCommand;
use servicepoint::Packet;
/// Turns a `Command` into a `Packet`.
/// The `Command` gets consumed.
@ -19,10 +20,10 @@ use servicepoint::{Command, Packet};
/// by explicitly calling `sp_packet_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_packet_from_command(
command: *mut Command,
command: *mut CCommand,
) -> *mut Packet {
let command = *Box::from_raw(command);
let packet = command.into();
let packet = command.0.into();
Box::into_raw(Box::new(packet))
}

View file

@ -561,7 +561,7 @@ namespace ServicePoint.BindGen
/// by explicitly calling `sp_command_dealloc`.
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_command_try_from_packet", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp_command_try_from_packet(Packet* packet);
public static extern CCommand* sp_command_try_from_packet(Packet* packet);
/// <summary>
/// Clones a `Command` instance.
@ -576,7 +576,7 @@ namespace ServicePoint.BindGen
/// by explicitly calling `sp_command_dealloc`.
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_command_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp_command_clone(Command* original);
public static extern CCommand* sp_command_clone(CCommand* original);
/// <summary>
/// Allocates a new `Command::Clear` instance.
@ -589,7 +589,7 @@ namespace ServicePoint.BindGen
/// by explicitly calling `sp_command_dealloc`.
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_command_clear", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp_command_clear();
public static extern CCommand* sp_command_clear();
/// <summary>
/// Allocates a new `Command::HardReset` instance.
@ -602,7 +602,7 @@ namespace ServicePoint.BindGen
/// by explicitly calling `sp_command_dealloc`.
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_command_hard_reset", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp_command_hard_reset();
public static extern CCommand* sp_command_hard_reset();
/// <summary>
/// Allocates a new `Command::FadeOut` instance.
@ -615,7 +615,7 @@ namespace ServicePoint.BindGen
/// by explicitly calling `sp_command_dealloc`.
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_command_fade_out", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp_command_fade_out();
public static extern CCommand* sp_command_fade_out();
/// <summary>
/// Allocates a new `Command::Brightness` instance for setting the brightness of all tiles to the
@ -633,7 +633,7 @@ namespace ServicePoint.BindGen
/// by explicitly calling `sp_command_dealloc`.
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_command_brightness", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp_command_brightness(byte brightness);
public static extern CCommand* sp_command_brightness(byte brightness);
/// <summary>
/// Allocates a new `Command::CharBrightness` instance.
@ -649,7 +649,7 @@ namespace ServicePoint.BindGen
/// by explicitly calling `sp_command_dealloc`.
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_command_char_brightness", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp_command_char_brightness(nuint x, nuint y, CBrightnessGrid* byte_grid);
public static extern CCommand* sp_command_char_brightness(nuint x, nuint y, CBrightnessGrid* byte_grid);
/// <summary>
/// Allocates a new `Command::BitmapLinear` instance.
@ -666,7 +666,7 @@ namespace ServicePoint.BindGen
/// by explicitly calling `sp_command_dealloc`.
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_command_bitmap_linear", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp_command_bitmap_linear(nuint offset, CBitVec* bit_vec, CompressionCode compression);
public static extern CCommand* sp_command_bitmap_linear(nuint offset, CBitVec* bit_vec, CompressionCode compression);
/// <summary>
/// Allocates a new `Command::BitmapLinearAnd` instance.
@ -683,7 +683,7 @@ namespace ServicePoint.BindGen
/// by explicitly calling `sp_command_dealloc`.
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_command_bitmap_linear_and", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp_command_bitmap_linear_and(nuint offset, CBitVec* bit_vec, CompressionCode compression);
public static extern CCommand* sp_command_bitmap_linear_and(nuint offset, CBitVec* bit_vec, CompressionCode compression);
/// <summary>
/// Allocates a new `Command::BitmapLinearOr` instance.
@ -700,7 +700,7 @@ namespace ServicePoint.BindGen
/// by explicitly calling `sp_command_dealloc`.
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_command_bitmap_linear_or", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp_command_bitmap_linear_or(nuint offset, CBitVec* bit_vec, CompressionCode compression);
public static extern CCommand* sp_command_bitmap_linear_or(nuint offset, CBitVec* bit_vec, CompressionCode compression);
/// <summary>
/// Allocates a new `Command::BitmapLinearXor` instance.
@ -717,7 +717,7 @@ namespace ServicePoint.BindGen
/// by explicitly calling `sp_command_dealloc`.
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_command_bitmap_linear_xor", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp_command_bitmap_linear_xor(nuint offset, CBitVec* bit_vec, CompressionCode compression);
public static extern CCommand* sp_command_bitmap_linear_xor(nuint offset, CBitVec* bit_vec, CompressionCode compression);
/// <summary>
/// Allocates a new `Command::Cp437Data` instance.
@ -733,7 +733,7 @@ namespace ServicePoint.BindGen
/// by explicitly calling `sp_command_dealloc`.
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_command_cp437_data", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp_command_cp437_data(nuint x, nuint y, CCp437Grid* byte_grid);
public static extern CCommand* sp_command_cp437_data(nuint x, nuint y, CCp437Grid* byte_grid);
/// <summary>
/// Allocates a new `Command::BitmapLinearWin` instance.
@ -750,7 +750,7 @@ namespace ServicePoint.BindGen
/// by explicitly calling `sp_command_dealloc`.
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_command_bitmap_linear_win", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Command* sp_command_bitmap_linear_win(nuint x, nuint y, PixelGrid* pixel_grid, CompressionCode compression_code);
public static extern CCommand* sp_command_bitmap_linear_win(nuint x, nuint y, PixelGrid* pixel_grid, CompressionCode compression_code);
/// <summary>
/// Deallocates a `Command`.
@ -764,7 +764,7 @@ namespace ServicePoint.BindGen
/// - `this` was not passed to another consuming function, e.g. to create a `Packet`
/// </summary>
[DllImport(__DllName, EntryPoint = "sp_command_dealloc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void sp_command_dealloc(Command* ptr);
public static extern void sp_command_dealloc(CCommand* ptr);
/// <summary>
/// Creates a new instance of `Connection`.
@ -1071,6 +1071,11 @@ namespace ServicePoint.BindGen
{
}
[StructLayout(LayoutKind.Sequential)]
public unsafe partial struct CCommand
{
}
[StructLayout(LayoutKind.Sequential)]
public unsafe partial struct CByteSlice
{

View file

@ -3,7 +3,7 @@ using ServicePoint.BindGen;
namespace ServicePoint;
public sealed class Command : SpNativeInstance<BindGen.Command>
public sealed class Command : SpNativeInstance<BindGen.CCommand>
{
public static bool TryFromPacket(Packet packet, [MaybeNullWhen(false)] out Command command)
{
@ -121,7 +121,7 @@ public sealed class Command : SpNativeInstance<BindGen.Command>
}
}
private unsafe Command(BindGen.Command* instance) : base(instance)
private unsafe Command(BindGen.CCommand* instance) : base(instance)
{
}