comp.lang.ada
 help / color / mirror / Atom feed
* Protected Objects: Scoping problem.
@ 1998-06-15  0:00 Robert Worne
  1998-06-15  0:00 ` Matthew Heaney
  1998-06-17  0:00 ` Stephen Leake
  0 siblings, 2 replies; 4+ messages in thread
From: Robert Worne @ 1998-06-15  0:00 UTC (permalink / raw)



I am having a problem with scoping on protected objects.

I am working on a package that has a protected object that
communicates to other packages via a "read".  A "write" in that object 
should be visible only as an internal subprogram, and not visible to the
outside world.  A simplified version from the Ada documents on-line is:

package Protected_Variable is

type T is new integer;
    
   protected Variable is
   function Read return T;
   procedure Write(Value: T);  -- I do NOT want this accessible to outside
                               -- packages!
private
   Data: T;
end Variable;
end Protected_Variable;

package body Protected_Variable is
protected body Variable is
   function Read return T is
   begin
      return Data;
   end Read; 

   procedure Write(Value: T) is
   begin
      Data := Value;
end Write is separate;
end Variable; 
end Protected_Variable;

What I want is for the procedure "Write" to not be externally visible,
as if it were commented out of the spec file.  However, that makes it not
visible to the package body (at least during elaboration).

How can I accomplish this seemingly simple task?  Am I going about it in a
totally incorrect fashion? I need the mutual exclusion a protected object
will provide, but in a read-only fashion to the outside.

If anyone needs to know the compiler I'm using, it's ObjectAda.  Although
I tend to believe it's my problem and not the compiler's.




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

* Re: Protected Objects: Scoping problem.
  1998-06-15  0:00 Protected Objects: Scoping problem Robert Worne
@ 1998-06-15  0:00 ` Matthew Heaney
  1998-06-17  0:00 ` Stephen Leake
  1 sibling, 0 replies; 4+ messages in thread
From: Matthew Heaney @ 1998-06-15  0:00 UTC (permalink / raw)



Robert Worne <rworne@primenet.com> writes:

> I am working on a package that has a protected object that
> communicates to other packages via a "read".  A "write" in that object 
> should be visible only as an internal subprogram, and not visible to the
> outside world.  A simplified version from the Ada documents on-line is:
> 
> package Protected_Variable is
> 
> type T is new integer;
>     
>    protected Variable is
>    function Read return T;
>    procedure Write(Value: T);  -- I do NOT want this accessible to outside
>                                -- packages!
> private
>    Data: T;
> end Variable;
> end Protected_Variable;
>
> package body Protected_Variable is
> protected body Variable is
>    function Read return T is
>    begin
>       return Data;
>    end Read; 
> 
>    procedure Write(Value: T) is
>    begin
>       Data := Value;
> end Write is separate;
> end Variable; 
> end Protected_Variable;
>

I'm not sure what you're trying to do here.

If you have only one object, then why not hide the protected object in
the body?

package Protected_Variable is
 
   function Get_Value return Integer;

end;

package body Protected_Variable is

   protected Variable is 
      function Read return Integer;
      procedure Write (V : in Integer);
   private
      V : Integer;
   end;

   protected body Variable is

      function Read return Integer is
      begin
        return V;
      end;

      procedure Write (V : Integer) is
      begin
         Variable.V := V;
      end;

   end Variable;

end Protected_Variable;

You could declare the protected object in the private part of the
spec, if you wish.

You could even declare a "protected variable" ADT (limited private, of
course), and implement the type as a protected type.

I don't know what you're trying to do.

> What I want is for the procedure "Write" to not be externally visible,
> as if it were commented out of the spec file.  However, that makes it not
> visible to the package body (at least during elaboration).

Well, if it's not visible, then who calls it???

> How can I accomplish this seemingly simple task?  Am I going about it in a
> totally incorrect fashion? I need the mutual exclusion a protected object
> will provide, but in a read-only fashion to the outside.

Then just export a selector in the spec, and declare the protected
object in the body.  What's the problem?
 




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

* Re: Protected Objects: Scoping problem.
  1998-06-17  0:00 ` Stephen Leake
@ 1998-06-17  0:00   ` Robert A Duff
  0 siblings, 0 replies; 4+ messages in thread
From: Robert A Duff @ 1998-06-17  0:00 UTC (permalink / raw)



Stephen Leake <Stephen.Leake@gsfc.nasa.gov> writes:

> package Protected_Variable is
>    
>    type T is new integer;
>    
>    protected Variable is
>       function Read return T;
> 
>    private
>       procedure Write(Value: T);  -- NOT accessible to outside packages

True, but I think the original poster wanted Write to be accessible to
*this* package (in the body), but not accessible to outside packages.
The above doesn't do that.

Another post showed how to wrap the call to the protected Read in a
normal procedure, and put the whole protected type in the package body.
That works, but if the exported thing was an entry, then it wouldn't --
outsiders wouldn't be able to do entry-ish things to it, such as requeue
to it, do a timed call to it, &c.

-- 
Change robert to bob to get my real email address.  Sorry.
-- 
Change robert to bob to get my real email address.  Sorry.




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

* Re: Protected Objects: Scoping problem.
  1998-06-15  0:00 Protected Objects: Scoping problem Robert Worne
  1998-06-15  0:00 ` Matthew Heaney
@ 1998-06-17  0:00 ` Stephen Leake
  1998-06-17  0:00   ` Robert A Duff
  1 sibling, 1 reply; 4+ messages in thread
From: Stephen Leake @ 1998-06-17  0:00 UTC (permalink / raw)



Robert Worne <rworne@primenet.com> writes:

> I am having a problem with scoping on protected objects.
> 
> I am working on a package that has a protected object that
> communicates to other packages via a "read".  A "write" in that object 
> should be visible only as an internal subprogram, and not visible to the
> outside world.  A simplified version from the Ada documents on-line is:
> 
> package Protected_Variable is
> 
> type T is new integer;
>     
>    protected Variable is
>    function Read return T;
>    procedure Write(Value: T);  -- I do NOT want this accessible to outside
>                                -- packages!
> private
>    Data: T;
> end Variable;
> end Protected_Variable;

That's precisely what the private part of the protected type is for.
Just declare Write in the private part:

package Protected_Variable is
   
   type T is new integer;
   
   protected Variable is
      function Read return T;

   private
      procedure Write(Value: T);  -- NOT accessible to outside packages
      Data: T;
   end Variable;
end Protected_Variable;

<snip> 

-- Stephe




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

end of thread, other threads:[~1998-06-17  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-06-15  0:00 Protected Objects: Scoping problem Robert Worne
1998-06-15  0:00 ` Matthew Heaney
1998-06-17  0:00 ` Stephen Leake
1998-06-17  0:00   ` Robert A Duff

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