comp.lang.ada
 help / color / mirror / Atom feed
From: "Pat Rogers" <progers@NOclasswideSPAM.com>
Subject: Re: Controlled types for resource locking
Date: 1999/09/09
Date: 1999-09-09T00:00:00+00:00	[thread overview]
Message-ID: <rtfpr6pv1iv73@corp.supernews.com> (raw)
In-Reply-To: 37d7cee7@news1.prserv.net

Matthew Heaney <matthew_heaney@acm.org> wrote in message
news:37d7cee7@news1.prserv.net...
> In article <rtff5h311iv34@corp.supernews.com> , "Pat Rogers"
> <progers@NOclasswideSPAM.com> wrote:
>
> > with Ada.Finalization;
> > with Lockable;
> >
> > generic
> >   type Unmanaged_Resource is new Lockable.Thing with private;
> > package Locking_Manager is
> >
> >   type Managed_Resource( Resource : access Unmanaged_Resource ) is
> >     tagged limited private;
> >
> ...
> > end Locking_Manager;
>
> Realize that there's no reason to import a formal tagged derived type.
A
> simpler approach is to just import a private type and some operations:
>
> generic
>   type Resource_Type (<>) is limited private;
>   with procedure Lock (Resource : in out Resource_Type) is <>;
>   with procedure Unlock (Resource : in out Resource_Type) is <>;
> package Locking_Manager is ...;
>
> This will work for resources that are tagged or untagged.
>
> If you want to dispatch lock and unlock, that's possible too.  Just
import a
> class-wide op that calls the primitive op:
>
> package P is
>
>   type Root_Resource_Type is abstract tagged limited private;
>
>   procedure Lock (Resource : in out ...);
>   procedure Unlock (Resource : ...);
>
> ...
> end P;
>
> Just write two small class-wide ops:
>
> package P.Dispatching_Utils is
>
>   procedure Call_Lock (Resource : in out Resource_Type'Class);
>
>   procedure Call_Unclock (Resource : in out Resource_Type'Class);
>
> end;
>
> package body P.Dispatching_Utils is
>
>   procedure Call_Lock (Resource : in out Resource_Type'Class) is
>   begin
>     Lock (Resource);
>   end;
>
>   ...
> end;
>
> Now just supply the class-wide type and the util ops to the
instantiation:
>
> with P.Dispatching_Utils, Locking_Manager;
>
> package Lock_Resources is
>   new Locking_Manager (Resource_Type'Class, Call_Lock, Call_Unlock);
>
>
> Et viola!  The calls to Lock and Unlock dispatch.
>
> In general, I prefer not to import a type hierarchy unless I really
have to.


"Simpler is in the eye of the beholder".  :-)  I find it simpler to do
it the other way, but not greatly so.  Too, I had originally done it as
a mix-in, so that the exported type was in the class of the imported
type, but I don't like having to do view conversions to get the parent's
primitive operations when using the child type.


> I'll be discussing this technique in my Design Patterns tutorial at
this
> year's SIGAda conference.


When is the book coming out?


--
Pat Rogers                            Training and Consulting in:
http://www.classwide.com      Deadline Schedulability Analysis
progers@classwide.com       Software Fault Tolerance
(281)648-3165                        Real-Time/OO Languages






  reply	other threads:[~1999-09-09  0:00 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-09-08  0:00 Controlled types for resource locking Steve Folly
1999-09-08  0:00 ` tmoran
1999-09-09  0:00   ` Pat Rogers
1999-09-09  0:00     ` Matthew Heaney
1999-09-09  0:00       ` Pat Rogers [this message]
1999-09-09  0:00         ` Matthew Heaney
1999-09-09  0:00           ` Pat Rogers
1999-09-09  0:00   ` Ted Dennison
1999-09-09  0:00     ` Tucker Taft
1999-09-08  0:00 ` Matthew Heaney
1999-09-08  0:00 ` David C. Hoos, Sr.
replies disabled

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