Re: initialiser un objet protégé

Ludovic Brenta <[email protected]>
Newsgroups gmane.comp.lang.ada.france
Message-ID <[email protected]>
Thomas De Contes <[email protected]> writes:
> est il possible d'initialiser un objet protégé ?
>
> je veux dire,
>
> - pour des variables internes pour lesquelles le calcul est un peu
> trop compliqué pour être mis directement dans une affectation (par
> exemple lecture dans un fichier)
>
> - autrement qu'avec une procédure d'initialisation qu'on devrais
> appeler manuellement à chaque fois, avec risque d'oubli

Je ne vois guère que déclarer un type privé avec des discriminants
inconnus pour forcer le client à appeler la fonction d'initialisation:

package P is

   type T (<>) is limited private;

   function Construct return T;

   procedure Foo (Object : in out T);
   function Bar (Object : in T) return Boolean;

private
   protected type T is
      procedure Foo;
      function Bar return Boolean;
      procedure Initialize;
   private
      Must_Be_Initialized : Boolean;
   end T;
end P;

package body P is

   function Construct return T is
   begin
      return Result : T do
         Result.Initialize;
      end return;
   end Construct;

   procedure Foo (Object : in out T) is
   begin
      Object.Foo;
   end Foo;

   function Bar (Object : in T) return Boolean is
   begin
      return Object.Bar;
   end Bar;

   protected body T is
      procedure Foo is begin null; end Foo;
      function Bar return Boolean is begin return False; end Bar;
      procedure Initialize is begin null; end Initialize;
   end T;

end P;

-- 
Ludovic Brenta.
_______________________________________________
Site WWW de l'association Ada-France: http://www.ada-france.org/
[email protected]
http://www.ada-france.org/mailman/listinfo/ada-france
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.