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-Thread: 103376,2fa4069595cb9ef7 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!o21g2000prh.googlegroups.com!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Breaking a circularity Date: Mon, 28 Mar 2011 02:59:35 -0700 (PDT) Organization: http://groups.google.com Message-ID: <5aac3e2d-a382-479e-90a3-8acd846ca204@o21g2000prh.googlegroups.com> References: NNTP-Posting-Host: 153.98.68.197 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1301306376 31931 127.0.0.1 (28 Mar 2011 09:59:36 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 28 Mar 2011 09:59:36 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: o21g2000prh.googlegroups.com; posting-host=153.98.68.197; posting-account=pcLQNgkAAAD9TrXkhkIgiY6-MDtJjIlC User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.6) Gecko/2009012111 Red Hat/3.0.6-1.el5 Firefox/3.0.6,gzip(gfe) Xref: g2news1.google.com comp.lang.ada:18534 Date: 2011-03-28T02:59:35-07:00 List-Id: 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. =A0The > handlers for events must have access to a simulation state record, so > used a generic thus: [...] > Here's the circularity: =A0Instantiating 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? =A0The 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, "=3D"); 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.