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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,28c043d47104f5d9 X-Google-Attributes: gid103376,public From: "Pat Rogers" Subject: Re: Controlled types for resource locking Date: 1999/09/09 Message-ID: #1/1 X-Deja-AN: 523019722 References: <936824686.950.0.nnrp-07.c2de848f@news.demon.co.uk> <37d7cee7@news1.prserv.net> X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 X-Complaints-To: newsabuse@supernews.com Organization: Software Arts & Sciences X-MSMail-Priority: Normal Newsgroups: comp.lang.ada Date: 1999-09-09T00:00:00+00:00 List-Id: Matthew Heaney wrote in message news:37d7cee7@news1.prserv.net... > In article , "Pat Rogers" > 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