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=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,c7ff90b15f15636c X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!postnews.google.com!a32g2000yqm.googlegroups.com!not-for-mail From: mockturtle Newsgroups: comp.lang.ada Subject: Re: Runtime type selection Date: Sun, 11 Oct 2009 10:15:37 -0700 (PDT) Organization: http://groups.google.com Message-ID: <9b80fe88-6814-48e2-9fa6-0c2ab7d99521@a32g2000yqm.googlegroups.com> References: <2c15c8a6-b555-4d08-8fe6-f77cb207e7a6@k33g2000yqa.googlegroups.com> NNTP-Posting-Host: 93.37.248.253 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1255281338 16528 127.0.0.1 (11 Oct 2009 17:15:38 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 11 Oct 2009 17:15:38 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: a32g2000yqm.googlegroups.com; posting-host=93.37.248.253; posting-account=9fwclgkAAAD6oQ5usUYhee1l39geVY99 User-Agent: G2/1.0 X-HTTP-UserAgent: Opera/9.64 (X11; Linux i686; U; en) Presto/2.1.1,gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:8669 Date: 2009-10-11T10:15:37-07:00 List-Id: On Oct 11, 6:41=A0pm, xorque wrote: > Hello. > > I'd like to develop a program that processes a graph. The contents of > the graph are a random selection of objects of different types (all > elements may be of the same type or may all be different - assuming > there are enough types defined). By 'type' in this case, I am > referring > to Ada types. > > The basic problem is that I want to create an object of a type picked > at > random, at runtime. I'm not entirely sure what my options are here. > > I could create a base tagged type and derive a set of types from that > base, selecting at random. I could use variant records. It seems that > in > either case, I'd need to create an enumeration type with a 1:1 mapping > between enumeration values and derived types. > > Both of these solutions seem a little unwieldly. They also seem like > they'd end up making the program difficult to extend (I'd ideally just > like to derive a new type and have it automatically available to be > selected at random for the graph). > > Any ideas would be appreciated. I appreciate the problem is a little > abstract... I am making this suggestion as I write, so maybe it could prove unfeasible, but maybe it will inspire you. I would create a base tagged type, so that all your types are descendent of a common ancestor. Then I would define a "constructor callback", that is a type that represents an access to a function that return a class-wide access type to a newly created object. Finally, I would create a container (a vector?) of constructor callbacks. More or less, something like this type Root is tagged null record; type Root_Class_Access is access Root'Class; type Callback is access function return Root_Class_Access; package Callback_Vectors is new Containers.Vectors(Natural, Callback); Callback_Table : Callback_Vectors.Vector; [Please note: I did not check the code above with an actual Ada compiler, so maybe it is full of errors (beside the missing "with"), but I hope it clarifies what I mean] When I want to add a new type to my system, I simply "register" the constructor of the new type by inserting it into Callback_Table. When I want to generate an object at random I simply choose one callback from the container and call it. As I said, I made this in "real time" while writing, so I hope this makes some sense... ;-) Cheers