From: Lucretia <laguest9000@googlemail.com>
Subject: Is this actually possible?
Date: Wed, 11 Dec 2019 08:43:08 -0800 (PST)
Date: 2019-12-11T08:43:08-08:00 [thread overview]
Message-ID: <36d45c82-2a6b-4c60-baeb-1a4aef5189c7@googlegroups.com> (raw)
Hi,
I was thinking about extensible records recently (not tagged types), and thought to try to export a tagged type to C, not C++. It compiles, but the results aren't quite right, so wondering if it's possible or not.
The idea is to export an array of tagged types as a C array to either a function or a variable / struct element. i.e.
struct Blah {
My_Array *Array;
};
or in this case (Array below):
#include <stdio.h>
typedef struct {
int One;
float Two;
} Packet;
void Dump (int First, int Last, Packet *Array) {
printf ("Dump (%d .. %d)\n", First, Last);
printf ("Array => %p\n", Array);
for (int I = First; I < Last + 1; I++) {
printf ("\tOne => %d\n", Array[I].One);
printf ("\tTwo => %f\n", Array[I].Two);
}
}
So, I tried a few things, including Holders, which I never knew existed, but this is where I am currently:
with Interfaces.C;
-- with Ada.Containers.Bounded_Holders;
with System;
package Datums is
package C renames Interfaces.C;
-- Force a size, we kind of want a variant like array of records, but with unknown element types, but always of
-- the same number and size of elements.
type Root_Packet is abstract tagged null record with
Size => C.int'Size + C.C_float'Size;
-- package Root_Holders is new Ada.Containers.Bounded_Holders (Element_Type => Root_Packet'Class);
type Storage_Element is mod 2 ** System.Storage_Unit with
Convention => C;
type Storage_Array is array (Positive range <>) of Storage_Element with
Convention => C;
type Root_Holder is
record
Data : Storage_Array (1 .. Root_Packet'Max_Size_In_Storage_Elements);
end record with
Convention => C;
type Packet_Array is array (C.size_t range <>) of aliased Root_Holder with -- Root_Holders.Holder with
Convention => C;
type Packet_Array_Ptr is access all Packet_Array with
Convention => C;
type Packet_1 is new Root_Packet with
record
One : C.int;
Two : C.C_float;
end record with
Convention => C;
type Packet_2 is new Root_Packet with
record
Banana : C.int;
Mango : C.C_float;
end record with
Convention => C;
end Datums;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Unchecked_Conversion;
with System.Address_Image;
with Datums; use Datums;
procedure Testing is
-- use Root_Holders;
function To_Holder is new Ada.Unchecked_Conversion (Source => Packet_1, Target => Root_Holder);
function To_Holder is new Ada.Unchecked_Conversion (Source => Packet_2, Target => Root_Holder);
A : aliased Packet_Array := (1 => To_Holder (Packet_1'(One => 10, Two => 3.14)),
2 => To_Holder (Packet_2'(Banana => 50, Mango => 4.5)));
procedure Dump (First, Last : in C.int; Data : in Packet_Array_Ptr) with
Convention => C,
Import => True,
External_Name => "Dump";
begin
Put_Line ("A'Address => " & System.Address_Image (A'Address));
Dump (C.int (A'First), C.int (A'Last), A'Unchecked_Access);
end Testing;
project T is
for Source_Dirs use (".");
for Languages use ("C", "Ada");
for Main use ("testing.adb");
package Compiler is
for Default_Switches ("Ada") use ("-g");
for Default_Switches ("C") use ("-g");
end Compiler;
end T;
next reply other threads:[~2019-12-11 16:43 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-11 16:43 Lucretia [this message]
2019-12-11 17:38 ` Is this actually possible? Dmitry A. Kazakov
2019-12-11 17:54 ` Lucretia
2019-12-11 19:55 ` Randy Brukardt
2019-12-11 19:58 ` Dmitry A. Kazakov
2019-12-11 21:12 ` Lucretia
2019-12-11 21:34 ` Dmitry A. Kazakov
2019-12-12 2:00 ` Randy Brukardt
2019-12-12 9:26 ` Niklas Holsti
2020-04-08 16:10 ` Alejandro R. Mosteo
2019-12-12 10:17 ` Lucretia
2019-12-12 14:32 ` Dmitry A. Kazakov
2019-12-12 15:14 ` Lucretia
2019-12-12 15:15 ` Lucretia
2019-12-12 18:24 ` Dmitry A. Kazakov
2019-12-12 18:30 ` Lucretia
2019-12-12 19:09 ` Dmitry A. Kazakov
2019-12-12 20:54 ` Lucretia
2019-12-12 21:12 ` Dmitry A. Kazakov
2019-12-13 11:11 ` Lucretia
2019-12-11 19:59 ` Randy Brukardt
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox