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,b88383a5d9c51aa0 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!z15g2000yqm.googlegroups.com!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Ada-Singleton-Why does it work like this? Date: Wed, 25 Mar 2009 05:07:41 -0700 (PDT) Organization: http://groups.google.com Message-ID: <13a049ee-c1f5-48da-b2c3-ab0a965190a8@z15g2000yqm.googlegroups.com> References: <5a7a870c-40e2-4803-8753-0f9cfd2b800f@k2g2000yql.googlegroups.com> <13su65cm8b5ov$.1198qla32cc3i$.dlg@40tude.net> <1bf21w38d0xy6.1jeyl0h376pe7.dlg@40tude.net> <14672a25-24e6-4b04-8556-c58690d8f3d3@w35g2000yqm.googlegroups.com> 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 1237982861 6032 127.0.0.1 (25 Mar 2009 12:07:41 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 25 Mar 2009 12:07:41 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: z15g2000yqm.googlegroups.com; posting-host=153.98.68.197; posting-account=pcLQNgkAAAD9TrXkhkIgiY6-MDtJjIlC User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:4297 Date: 2009-03-25T05:07:41-07:00 List-Id: Patrick Gunia wrote on comp.lang.ada: > Dmitry A. Kazakov wrote: > > Sorry, but a singleton with more than one instance is not a singleton. > > This is arguable. Gamma et al. tell explictily in their work, that it > =B4s one advantage of the Singleton-pattern that the number of creatable > instances can be fixed later on without major code changes. Thus I > think of the pattern more like a possibility to gain control over the > number of instantiations of a concrete class, not necessarily > restricted to one, though limited. Then do not call this a Singleton; call it a Bounded_Pool_Of_Instances. And in Ada, you still do not need tagged types or public access types to implement this pattern: package Pool_Of_Instances is type Handle (<>) is private; Null_Handle : constant Handle; function Next_Available return Handle; procedure Operate (On : in Handle); procedure Return (H : in out Handle); Pool_Exhausted : exception; private type T; type Handle is access all T; -- note: the access type is private Null_Handle : constant Handle :=3D null; end Pool_Of_Instances; package body Pool_Of_Instances is type T is record ... end record; N : constant :=3D 10; Instances : array (1 .. N) of aliased T; Available : array (1 .. N) of Boolean :=3D (others =3D> True); function Next_Available return Handle is begin for J in Available'Range loop if Available (J) then Available (J) :=3D False; return Instances (J)'Access; end if; end loop; raise Pool_Exhausted; end Next_Available; procedure Return (H : in out Handle) is begin H :=3D Null_Handle; end Return; end Pool_Of_Instances; The above pattern can be extended to do reference counting and deallocate unused instances, thereby making some slots available again. For that you'd need to make Handle controlled: package Pool_Of_Instances is -- as above private type T; type Access_T is access all T; type Handle is new Ada.Finalization.Controlled with record Instance : Access_T; end record; overriding procedure Initialize (H : in out Handle); overriding procedure Adjust (H : in out Handle); overriding procedure Finalize (H : in out Handle); end Pool_Of_Instances; The Instance would contain the reference count; Initialize, Adjust and Finalize would manage it. See http://www.adaic.com/learn/tech/safe_ptr.html for inspiration. -- Ludovic Brenta.