comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: Ada-Singleton-Why does it work like this?
Date: Wed, 25 Mar 2009 05:07:41 -0700 (PDT)
Date: 2009-03-25T05:07:41-07:00	[thread overview]
Message-ID: <13a049ee-c1f5-48da-b2c3-ab0a965190a8@z15g2000yqm.googlegroups.com> (raw)
In-Reply-To: 14672a25-24e6-4b04-8556-c58690d8f3d3@w35g2000yqm.googlegroups.com

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
> ´s 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 := null;
end Pool_Of_Instances;

package body Pool_Of_Instances is
   type T is record ... end record;
   N : constant := 10;
  Instances : array (1 .. N) of aliased T;
  Available : array (1 .. N) of Boolean := (others => True);

  function Next_Available return Handle is
  begin
      for J in Available'Range loop
         if Available (J) then
            Available (J) := False;
            return Instances (J)'Access;
         end if;
      end loop;
      raise Pool_Exhausted;
  end Next_Available;

   procedure Return (H : in out Handle) is
   begin
      H := 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.



  reply	other threads:[~2009-03-25 12:07 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-24 19:01 Ada-Singleton-Why does it work like this? patrick.gunia
2009-03-24 19:10 ` Pascal Obry
2009-03-24 20:47 ` Jeffrey R. Carter
2009-03-25  0:10   ` Martin
2009-03-25  0:41     ` Jeffrey R. Carter
2009-03-25  9:30     ` Dmitry A. Kazakov
2009-03-26  8:55       ` Martin
2009-03-26  9:28         ` Dmitry A. Kazakov
2009-03-26 13:39           ` Maciej Sobczak
2009-03-26 14:07             ` Georg Bauhaus
2009-03-26 14:33               ` Dmitry A. Kazakov
2009-03-26 15:22                 ` Georg Bauhaus
2009-03-26 16:31                   ` Dmitry A. Kazakov
2009-03-26 14:28             ` Dmitry A. Kazakov
2009-03-26 22:00               ` Maciej Sobczak
2009-03-27 10:02                 ` Dmitry A. Kazakov
2009-03-25 22:29   ` sjw
2009-03-24 20:52 ` Ludovic Brenta
2009-03-25  9:59   ` patrick.gunia
2009-03-25 10:29     ` Jean-Pierre Rosen
2009-03-25 11:26     ` Georg Bauhaus
2009-03-25 11:49       ` patrick.gunia
2009-03-29  7:29     ` Jacob Sparre Andersen
2009-03-24 21:21 ` Dmitry A. Kazakov
2009-03-25 10:07   ` patrick.gunia
2009-03-25 10:57     ` patrick.gunia
2009-03-25 11:40       ` Georg Bauhaus
2009-03-25 11:46       ` Ludovic Brenta
2009-03-25 11:55         ` patrick.gunia
2009-03-25 14:10         ` patrick.gunia
2009-03-25 14:40           ` Ludovic Brenta
2009-03-25 15:16             ` Adam Beneschan
2009-03-25 15:19             ` patrick.gunia
2009-03-25 16:52               ` Georg Bauhaus
2009-03-25 11:10     ` Dmitry A. Kazakov
2009-03-25 11:37       ` patrick.gunia
2009-03-25 12:07         ` Ludovic Brenta [this message]
2009-03-25 15:00         ` Robert A Duff
2009-03-25 11:17     ` Jean-Pierre Rosen
2009-03-26  9:04       ` Martin
2009-03-25 11:38     ` Ludovic Brenta
replies disabled

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