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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,a5da83ba8982e1cd X-Google-Attributes: gid103376,public From: stt@henning.camb.inmet.com (Tucker Taft) Subject: Re: Q: How to declare object with known tag? Date: 1996/06/05 Message-ID: #1/1 X-Deja-AN: 158527412 sender: news@inmet.camb.inmet.com (USENET news) x-nntp-posting-host: henning.camb.inmet.com references: organization: Intermetrics, Inc. newsgroups: comp.lang.ada Date: 1996-06-05T00:00:00+00:00 List-Id: John Woodruff (woodruff@tanana.llnl.gov) wrote: : I hope one of the Ada experts will advise me how to write the following : code fragment. : I would like to declare an object of a particular type, where the type : is a tagged record, and I can specify which tag at run-time. Here is : the fragment I would like to build: : declare : Trial_Tag : Ada.Tags.Tag ; : type Trial_Type is access all Some_Tag.Base'Class ; : Trial : Trial_Type ; : begin : Trial_Tag := Ada.Tags.Internal_Tag ("SOME_TAG.EXTEND21") ; : -- Let user input specify the string argument to Internal_Tag -- : Trial := new SOMETHING -- I dont know what ; : -- Trial.all is to be an instance of some_tag.extend21 : end ; : The goal: : I will declare a "heterogenous list" composed of various sub-types of : the type declared in some_tag. I wish to append a newly-created member : onto the list. : I want to read a user's input to decide which subtype is to be : appended. If I have my way, I'll be able to write the procedure for : recognizing the user's selection *before* all the subtypes have been : declared, because I want to defer the definition of "the last" legitimate : subtype until after the code to accept user selection has been written and : tested. Of course if the argument to Tags.Internal_Tag is wrong, I'll : remonstrate with my user. : I recognize that the user will need to create the object of (the new : extension) of some_tag.extend21. But to my thinking, after I have : successfully declared Trial.all in the example, THEN I can dispatch to : the newly extended type to receive the user's input. : Is this a legitimate wish? Yes, and you can do this approximately by using some_tag.base'Class'Input. This will read an external tag from a stream, convert it to the internal tag, and then dispatch to the appropriate some_tag.extendXX'Read to read the remainder of the data. It will raise Constraint_Error if the external tag is not recognized. You can override 'Read for the various type extensions if you want to recognize a special input format, rather than the normal input format used by 'Write/'Read. See "Stream-Oriented Attributes" in RM95 13.13. : John Woodruff N I F \ ^ / : Lawrence Livermore National Lab =====---- < 0 > : 510 422 4661 / v \ -- -Tucker Taft stt@inmet.com http://www.inmet.com/~stt/ Intermetrics, Inc. Cambridge, MA USA