comp.lang.ada
 help / color / mirror / Atom feed
From: "Robert I. Eachus" <rieachus@comcast.net>
Subject: Re: Type detection
Date: Fri, 30 Apr 2004 20:28:54 -0400
Date: 2004-04-30T20:28:54-04:00	[thread overview]
Message-ID: <QoydncxqQenacg_d4p2dnA@comcast.com> (raw)
In-Reply-To: <rUskc.57669$mU6.235826@newsb.telia.net>

Bj�rn Persson wrote:

> For the special case of distinguishing integer types from enumeration 
> types, I've been thinking of using something like this...

I extended the idea a little further, but there is still some work to 
do.  A character type is an enumeration type where at least one of the 
enumeration values is a character literal.  So the test for enumeration 
types could be extended with a loop.

Since a boolean type is either Boolean or a type derived from it, I 
guess checking that 'Size = 1 and that the literals are TRUE and FALSE 
should be close enough.

-------------------------------------------------------------------------

with Ada.Text_IO;
with Ada.Characters.Handling;
procedure Datatype is

   generic
     type Unknown is (<>);
   function Find_Datatype return String;

   function Find_Datatype return String is
   begin
     if Ada.Characters.Handling.Is_Letter
             (Unknown'Image(Unknown'First)(1))
     then return "Enumeration";
     elsif Unknown'Pos( Unknown'Base'First) < 0 then return "Integer";
     elsif Unknown'Pos(Unknown'Base'First) = 0 then
       begin
         if Unknown'Succ(Unknown'Last) = Unknown'First
         then return "Unsigned";
         else return "Enumeration?";
         end if;
       exception
         when others => return "Enumeration too";
         -- It's an enumeration.
       end;
     else return "Unknown Type";
     end if;
   end Find_Datatype;

   type Unsigned_16 is mod 2**16;
   type Weekday is (Monday, Tuesday, Wednesday, Thursday, Friday);

   function Find_Boolean is new Find_Datatype(Boolean);
   function Find_Integer is new Find_Datatype(Integer);
   function Find_Character is new Find_Datatype(Character);
   function Find_Unsigned is new Find_Datatype(Unsigned_16);
   function Find_Enumeration is new Find_Datatype(Weekday);

begin

   Ada.Text_IO.Put_Line(" Find_Boolean returned " & Find_Boolean);
   Ada.Text_IO.Put_Line(" Find_Integer returned " & Find_Integer);
   Ada.Text_IO.Put_Line(" Find_Character returned " & Find_Character);
   Ada.Text_IO.Put_Line(" Find_Unsigned returned " & Find_Unsigned);
   Ada.Text_IO.Put_Line(" Find_Enumeration returned " & Find_Enumeration);

end Datatype;


-- 

                                           Robert I. Eachus

"The terrorist enemy holds no territory, defends no population, is 
unconstrained by rules of warfare, and respects no law of morality. Such 
an enemy cannot be deterred, contained, appeased or negotiated with. It 
can only be destroyed--and that, ladies and gentlemen, is the business 
at hand."  -- Dick Cheney




  parent reply	other threads:[~2004-05-01  0:28 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-04-30 12:17 Newbie Ada Axel Druesnes
2004-04-30 12:22 ` Preben Randhol
2004-04-30 13:20 ` Hyman Rosen
2004-04-30 13:54 ` Type detection Björn Persson
2004-04-30 15:04   ` Marius Amado Alves
2004-05-01  0:28   ` Robert I. Eachus [this message]
2004-05-01  5:01     ` James Rogers
2004-05-01  7:12       ` Robert I. Eachus
2004-05-01 12:43       ` Björn Persson
2004-05-01 12:42     ` Björn Persson
2004-04-30 14:49 ` Newbie Ada Lutz Donnerhacke
2004-05-02 13:33 ` Stephen Leake
replies disabled

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