From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,269df2c167555fd6 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-02-23 21:44:05 PST Path: supernews.google.com!sn-xit-02!sn-xit-04!supernews.com!europa.netcrusader.net!204.71.34.3!newsfeed.cwix.com!wn2feed!worldnet.att.net!135.173.83.71!wnfilter1!worldnet-localpost!bgtnsc05-news.ops.worldnet.att.net.POSTED!not-for-mail Message-ID: <3A974A99.469AE413@worldnet.att.net> From: James Rogers X-Mailer: Mozilla 4.76 [en] (Win98; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Ada 0x: Re: Representation clause References: <3a95c52f@post.usenet.com> <3A95E095.75B2A248@worldnet.att.net> <3a9621eb$1@pull.gecm.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Sat, 24 Feb 2001 05:44:04 GMT NNTP-Posting-Host: 12.74.130.206 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc05-news.ops.worldnet.att.net 982993444 12.74.130.206 (Sat, 24 Feb 2001 05:44:04 GMT) NNTP-Posting-Date: Sat, 24 Feb 2001 05:44:04 GMT Organization: AT&T Worldnet Xref: supernews.google.com comp.lang.ada:5502 Date: 2001-02-24T05:44:04+00:00 List-Id: I just sent the following article to AdaPower.com. It is my humble attempt to provide a simple solution to the problems mentioned in this thread without involving, or waiting for, a language revision effort. The following article is intended for the section titled "Source Code Packages for Reuse". I place it in the public domain. I was inspired to provide this solution because of a discussion concerning how to use the internal representation of an enumerated type with a representation clause. Several contributors to the discussion expressed dissatisfaction with the fact that the language makes the use of the internal value so obtuse. I offer the following article as one possible solution for the problem. Jim Rogers Colorado Springs, Colorado jimmaureenrogers@worldnet.att.net ------------------------------------------------------------------- Ada allows the definition of an Enumerated type to include a representation clause. This clause is used to define the internal representation of the values of the Enumerated type. A difficulty arises when one wants to view, or convert to, that internal representation. The 'POS attribute of an Enumerated type only corresponds to the internal representation if the programmer has not explicitly used a representation clause. Ada provides no attribute to convert from an enumeration value to its internal representation. The only available course of action is to use Ada.Unchecked_Conversion on the enumeration value. This is not difficult, or particularly error prone. Errors can easily be encountered when trying to convert from what is expected to be an internal representation to the corresponding enumeration value. The following example may illustrate the problem. type Colors is (red, orange, yellow, green); for Colors use ( red => 1, orange => 10, yellow => 100, green => 1000); Converting to the internal representation results in no errors. It is possible that an attempt may be made to convert an integer, not in the set of internal values, to one of the enumeration values. If the following is attempted, Program_Error will be raised at run-time. function Convert is new Ada.Unchecked_Conversion(Source => Integer, Target => Colors); Enum_Value : Colors := Convert(2); The following generic package is designed to solve these problems for all possible enumerated types. ----------------------------------------------------------------------- -- This package provides a generic encapsulation of enumerated types -- to allow simple conversion between the implementation value of an -- enumerated type, and the corresponding enumeration value. ----------------------------------------------------------------------- generic type Enum is (<>); package Convert_Internal is function Internal_Representation(Item : enum) return Integer; function Value_Representation(Item : Integer) return Enum; Out_Of_Range_Error : Exception; end Convert_Internal; ----------------------------------------------------------------------- -- This package provides a generic encapsulation of enumerated types -- to allow simple conversion between the implementation value of an -- enumerated type, and the corresponding enumeration value. ----------------------------------------------------------------------- with Ada.Unchecked_Conversion; package body Convert_Internal is function Internal_Representation(Item : enum) return Integer is function convert is new Ada.Unchecked_Conversion(Source => Enum, Target => Integer); begin return convert(Item); end Internal_Representation; function Value_Representation(Item : Integer) return Enum is function convert is new Ada.Unchecked_Conversion(Source => Integer, Target => Enum); Is_Valid : Boolean := False; begin for enum_val in Enum'Range loop Is_Valid := Internal_Representation(enum_val) = Item; exit when Is_Valid; end loop; if Is_Valid then return convert(Item); else raise Out_Of_Range_Error; end if; end Value_Representation; end Convert_Internal;