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: "Matthew Heaney" Subject: Re: Controlled types for resource locking Date: 1999/09/09 Message-ID: <37d7cee7@news1.prserv.net>#1/1 X-Deja-AN: 522982025 Content-transfer-encoding: 7bit References: <936824686.950.0.nnrp-07.c2de848f@news.demon.co.uk> Content-Type: text/plain; charset="US-ASCII" X-Complaints-To: abuse@prserv.net X-Trace: 9 Sep 1999 15:14:47 GMT, 129.37.62.108 Organization: Global Network Services - Remote Access Mail & News Services Mime-version: 1.0 Newsgroups: comp.lang.ada Date: 1999-09-09T00:00:00+00:00 List-Id: 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. I'll be discussing this technique in my Design Patterns tutorial at this year's SIGAda conference. > package body Locking_Manager is > > > procedure Initialize( This : in out Managed_Resource ) is > begin > Lockable.Lock( Lockable.Thing'Class(This.Resource.all) ); > end Initialize; procedure Init (...) is begin Lock (This); end; > procedure Finalize( This : in out Managed_Resource ) is > begin > Lockable.Unlock( Lockable.Thing'Class(This.Resource.all) ); > end Finalize; > > > end Locking_Manager;