comp.lang.ada
 help / color / mirror / Atom feed
* Re: protected types and functions in Ada95 (Newbie)
  1997-04-24  0:00 protected types and functions in Ada95 (Newbie) Magnus Therning
@ 1997-04-24  0:00 ` Robert A Duff
  1997-04-25  0:00   ` Robert Dewar
  1997-04-24  0:00 ` Jon S Anthony
  1 sibling, 1 reply; 4+ messages in thread
From: Robert A Duff @ 1997-04-24  0:00 UTC (permalink / raw)




In article <335F2DF6.827@und.ida.liu.se>,
Magnus Therning  <d94magth@und.ida.liu.se> wrote:
>I want to seize a semaphore in a function. Is that possible using the
>following protected type semaphore? Functions can only take 'in'
>parameters, but I need 'in out' since Seize changes the semaphore.

You will, no doubt, get flamed by the anti-side-effect police.  ;-)

It's annoying that functions don't take 'in out' parameters.
The best workaround is probably to use an access parameter.
Then declare your objects aliased, and pass 'Access of them.

- Bob




^ permalink raw reply	[flat|nested] 4+ messages in thread

* protected types and functions in Ada95 (Newbie)
@ 1997-04-24  0:00 Magnus Therning
  1997-04-24  0:00 ` Robert A Duff
  1997-04-24  0:00 ` Jon S Anthony
  0 siblings, 2 replies; 4+ messages in thread
From: Magnus Therning @ 1997-04-24  0:00 UTC (permalink / raw)



I want to seize a semaphore in a function. Is that possible using the
following protected type semaphore? Functions can only take 'in'
parameters, but I need 'in out' since Seize changes the semaphore.

package Semaphore_S is

    type Kind(Num : Integer := 1) is limited private;

    procedure Seize(Sem : in out Kind);
    procedure Release(Sem : in out Kind);

private

   protected type Kind(Num : Integer := 1) is
      entry Secure;
      procedure Release; 
   private
      Count : Integer := Num;
   end Kind;

end Semaphore_S;

package body Semaphore_S is

   protected body Kind is
      Entry Secure when Count > 0 is
      begin
	 Count := Count - 1;
      end Secure;
      
      procedure Release is
      begin
	 Count := Count + 1;
      end Release;
   end Kind;

   procedure Seize(Sem : in out Kind) is
   begin  
      Sem.Secure;
   end Seize;

   procedure Release(Sem : in out Kind) is
   begin
      Sem.Release;
   end Release;

end Semaphore_S;
-- 
Magnus Therning ( d94magth@und.ida.liu.se )
http://www-und.ida.liu.se/~d94magth/




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: protected types and functions in Ada95 (Newbie)
  1997-04-24  0:00 protected types and functions in Ada95 (Newbie) Magnus Therning
  1997-04-24  0:00 ` Robert A Duff
@ 1997-04-24  0:00 ` Jon S Anthony
  1 sibling, 0 replies; 4+ messages in thread
From: Jon S Anthony @ 1997-04-24  0:00 UTC (permalink / raw)



In article <335F2DF6.827@und.ida.liu.se> Magnus Therning <d94magth@und.ida.liu.se> writes:

> I want to seize a semaphore in a function. Is that possible using the
> following protected type semaphore? Functions can only take 'in'
> parameters, but I need 'in out' since Seize changes the semaphore.
> 
> package Semaphore_S is
> 
>     type Kind(Num : Integer := 1) is limited private;
> 
>     procedure Seize(Sem : in out Kind);
>     procedure Release(Sem : in out Kind);

I suppose the thing to do is to use access parameters.  How do you
feel about this:

      procedure Seize(Sem : access Kind);

...

somewhere else...

function F ( Sem : access Kind ) return ... is
...
    Seize(Sem);
...
end F;


/Jon

-- 
Jon Anthony
Organon Motives, Inc.
Belmont, MA 02178
617.484.3383
jsa@organon.com





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: protected types and functions in Ada95 (Newbie)
  1997-04-24  0:00 ` Robert A Duff
@ 1997-04-25  0:00   ` Robert Dewar
  0 siblings, 0 replies; 4+ messages in thread
From: Robert Dewar @ 1997-04-25  0:00 UTC (permalink / raw)



Bob says

<<It's annoying that functions don't take 'in out' parameters.
The best workaround is probably to use an access parameter.
Then declare your objects aliased, and pass 'Access of them.>>

I must admit that in the GNAT world, when writing standard libraries
(including the predefined random numbe generator, where we cannot change
to use 'Access because the interface was not designed that way, and in 
the SPITBOL package for example), I am making more and more use of the
GNAT specific approach that allows one to take ;'Unrstricted_Access
of a formal parameter if it is passed by reference.

yes, this is horrible, but it is better to have a horrible kludge in
the implementation of the body, than force kludges on all implementors.
(sorry I mean by implementors here, clients who use the package).

I still find the omission  of in-out parameters in functions to be the
most annoying concession to the viewpoint that the language should
enforce some particular philosophy, even if it means putting in
arbitrary non-orthogonal restrictions.





^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1997-04-25  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-04-24  0:00 protected types and functions in Ada95 (Newbie) Magnus Therning
1997-04-24  0:00 ` Robert A Duff
1997-04-25  0:00   ` Robert Dewar
1997-04-24  0:00 ` Jon S Anthony

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