comp.lang.ada
 help / color / mirror / Atom feed
* pragma Convention C on discriminated record type
@ 1999-04-08  0:00 okellogg
  0 siblings, 0 replies; only message in thread
From: okellogg @ 1999-04-08  0:00 UTC (permalink / raw)


Hi,

The LRM doesn't say anything specific about the effect of pragma
Convention (C) when applied to a record type with a discriminant.

Is it safe to assume that the appended example will work?
(It does with GNAT 3.11p and Rational Apex 3.0.0b,
but I'm not sure about others.)

Thanks
-- oliver.kellogg@vs.dasa.de


/* file: uni.h */

typedef enum { one, two } discriminant;

typedef struct {
  discriminant _d;
  union {
    unsigned a;
    float b;
  } u;
} uni;

extern void set_a_union (uni *);
extern void set_b_union (uni *);

/* file: uni.c */

#include "uni.h"

void set_a_union (uni *var)
{
  var->_d = one;
  var->u.a = 1;
}

void set_b_union (uni *var)
{
  var->_d = two;
  var->u.b = 2.0;
}


-- file: ada_uni.adb

with Interfaces.C, Ada.Text_IO, Ada.Float_Text_IO;
use  Interfaces.C;

procedure Ada_Uni is

   type Discriminant is (one, two);
   pragma Convention (C, Discriminant);

   type Uni_Type (D : Discriminant := one) is record
      case D is
         when one =>
            A : Unsigned;
         when two =>
            B : C_Float;
      end case;
   end record;
   pragma Convention (C, Uni_Type);

   procedure Set_A_Union (Var : access Uni_Type);
   pragma Import (C, Set_A_Union);

   procedure Set_B_Union (Var : access Uni_Type);
   pragma Import (C, Set_B_Union);

   Var : aliased Uni_Type;

begin

   Set_A_Union (Var'access);
   Ada.Text_IO.Put ("after Set_A_Union: ");
   case Var.D is
      when one =>
         Ada.Text_IO.Put_Line ("A is " & Unsigned'Image (Var.A));
      when others =>
         Ada.Text_IO.Put_Line ("Whoa! error");
   end case;

   Set_B_Union (Var'access);
   Ada.Text_IO.Put ("after Set_B_Union: ");
   case Var.D is
      when two =>
         Ada.Text_IO.Put ("B is ");
         Ada.Float_Text_IO.Put (Float (Var.B));
         Ada.Text_IO.New_Line;
      when others =>
         Ada.Text_IO.Put_Line ("Whoa! error");
   end case;

end Ada_Uni;


-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1999-04-08  0:00 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-04-08  0:00 pragma Convention C on discriminated record type okellogg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox