comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Runtime type selection
Date: Sun, 11 Oct 2009 20:02:48 +0300
Date: 2009-10-11T20:02:48+03:00	[thread overview]
Message-ID: <4ad20fd5$0$26308$4f793bc4@news.tdc.fi> (raw)
In-Reply-To: <2c15c8a6-b555-4d08-8fe6-f77cb207e7a6@k33g2000yqa.googlegroups.com>

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).

Keep a list or array of (accesses to) a factory function for each 
possible (derived) type. When you derive a new type from the base 
graph-element type, add its factory function to this list (this is often 
called "registering" the new type/function). When you want to make a 
graph element of a "random" type, pick a random index into the list or 
array of factory functions and call the chosen function, which creates 
the new element and returns an access to it (type access all 
Base_Graph_Element_Type'Class).

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .



  reply	other threads:[~2009-10-11 17:02 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-11 16:41 Runtime type selection xorque
2009-10-11 17:02 ` Niklas Holsti [this message]
2009-10-11 17:15   ` xorque
2009-10-11 19:54     ` Niklas Holsti
2009-10-12  9:26       ` Georg Bauhaus
2009-10-12 12:02         ` xorque
2009-10-12 23:42       ` Randy Brukardt
2009-10-13  6:50         ` Niklas Holsti
2009-10-13  0:59       ` Adam Beneschan
2009-10-13  7:01         ` Niklas Holsti
2009-10-11 17:15 ` mockturtle
2009-10-11 20:06 ` Dmitry A. Kazakov
2009-10-13 10:45   ` Colin Paul Gloster
2009-10-13 10:17     ` Dmitry A. Kazakov
2009-10-13 15:11 ` xorque
2009-10-13 15:50   ` Dmitry A. Kazakov
2009-10-13 16:02     ` xorque
replies disabled

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