comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: Breaking a circularity
Date: Mon, 28 Mar 2011 02:59:35 -0700 (PDT)
Date: 2011-03-28T02:59:35-07:00	[thread overview]
Message-ID: <5aac3e2d-a382-479e-90a3-8acd846ca204@o21g2000prh.googlegroups.com> (raw)
In-Reply-To: dd0c7179-9e48-4366-9b1f-c5586b2c6e3c@e21g2000yqe.googlegroups.com

Gene wrote on comp.lang.ada:
> Need some expert help here with a circularity in the data structures
> for a discrete event simulation.
>
> We need event queues and an arbitrary number of event types.  The
> handlers for events must have access to a simulation state record, so
> used a generic thus:
[...]
> Here's the circularity:  Instantiating an event queue requires the
> simulation state type, but the simulation state must contain an event
> queue (of the instantiated type).
[...]
> Is there a cleaner way to do this?  The painful part is introducing a
> pointer to the event queue in State_Type just to break the
> circularity, when this seems superfluous and means I should make
> State_Type controlled to release the queue.

How about this:

generic
   type State_Type is tagged private;
package States_With_Event_Queues is

   type State_With_Event_Queue_Type is new State_Type with private;

   type Event_Type is abstract tagged limited private;

   procedure Handle(State : in out State_Type;
                    Event : access Event_Type) is abstract;

   type Event_Ptr_Type is access all Event_Type'Class;

   procedure Add(Event_Queue : in out Event_Queue_Type;
                 Time : in Time_Type;
                 Event : access Event_Type'Class);

   -- other methods not shown.
private

   type Event_Type is abstract record
      Id : Positive;
      Time : Time_Type;
   end record;

   function Sooner_Than(A, B : in Event_Ptr_Type) return Boolean;

   -- We are just going to put a thin wrapper around Ada ordered sets
   -- to implement our event queue.
   package Event_Queues is
     new Ada.Containers.Ordered_Sets(Event_Ptr_Type, Sooner_Than,
"=");
   type Event_Queue_Type is new Event_Queues.Set with null record;

   type State_With_Event_Queue_Type is new State_Type with record
      Event_Queue : Event_Queue_Type;
   end record;

end States_With_event_Queues;



Would that work?

BTW, why does Event_Type have to be limited? If it weren't limited,
you would not need Event_Ptr_Type.  Also, why does it have to be
abstract?

--
Ludovic Brenta.



  reply	other threads:[~2011-03-28  9:59 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-28  4:11 Breaking a circularity Gene
2011-03-28  9:59 ` Ludovic Brenta [this message]
2011-03-28 11:37   ` Simon Wright
2011-03-28 22:38     ` Gene
2011-03-29  3:01       ` Randy Brukardt
2011-03-28 19:47   ` Gene
2011-03-28 10:15 ` Simon Wright
2011-03-28 21:31   ` Gene
2011-03-28 11:06 ` Martin
2011-03-28 22:26   ` Gene
2011-03-29 16:28     ` Martin
replies disabled

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