comp.lang.ada
 help / color / mirror / Atom feed
* Simplest way to protect a variable ?
@ 2001-09-07  6:50 Reinert Korsnes
  2001-09-07  8:21 ` Peter Amey
                   ` (3 more replies)
  0 siblings, 4 replies; 11+ messages in thread
From: Reinert Korsnes @ 2001-09-07  6:50 UTC (permalink / raw)


Hi,

Let's say I initiate a "global" variable, A (for example an array), by
calling a procedure (which may read data from a file).  And I want to be
sure that the content (value) of this variable is not changed by
another procedure.  The variable is "global" in the sense that it is 
available in many many other routines.   

I may have several co-programmers I do not trust :-)

What is the simplest way to do this (in Ada) ?

reinert

-- 
http://home.chello.no/~rkorsnes



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

* Re: Simplest way to protect a variable ?
  2001-09-07  6:50 Simplest way to protect a variable ? Reinert Korsnes
@ 2001-09-07  8:21 ` Peter Amey
  2001-09-07  8:45 ` Wilhelm Spickermann
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 11+ messages in thread
From: Peter Amey @ 2001-09-07  8:21 UTC (permalink / raw)




Reinert Korsnes wrote:
> 
> Hi,
> 
> Let's say I initiate a "global" variable, A (for example an array), by
> calling a procedure (which may read data from a file).  And I want to be
> sure that the content (value) of this variable is not changed by
> another procedure.  The variable is "global" in the sense that it is
> available in many many other routines.
> 
> I may have several co-programmers I do not trust :-)
> 
> What is the simplest way to do this (in Ada) ?
> 

I don't know if it is the easiest way, but you could have a Boolean
"Already_Initialised" flag in the package that owns the array.  This
would be set to False during elaboration of the package body.  Your
Initialise procedure would refuse to do anything if Already_Initialised
is set to True and would set it to True after performing the first and
only update of the array.  The array would, of course, also have to be
in the package body (allowing read access via exported subprograms) in
order to prevent any direct updating of it.

Peter



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

* RE: Simplest way to protect a variable ?
  2001-09-07  6:50 Simplest way to protect a variable ? Reinert Korsnes
  2001-09-07  8:21 ` Peter Amey
@ 2001-09-07  8:45 ` Wilhelm Spickermann
  2001-09-07  9:13   ` Reinert Korsnes
  2001-09-07 11:35   ` Reinert Korsnes
  2001-09-07 13:03 ` DuckE
  2001-09-07 13:21 ` Ted Dennison
  3 siblings, 2 replies; 11+ messages in thread
From: Wilhelm Spickermann @ 2001-09-07  8:45 UTC (permalink / raw)
  To: comp.lang.ada


On 07-Sep-01 Reinert Korsnes wrote:
> Hi,
> 
> Let's say I initiate a "global" variable, A (for example an array), by
> calling a procedure (which may read data from a file).  And I want to be
> sure that the content (value) of this variable is not changed by
> another procedure.  The variable is "global" in the sense that it is 
> available in many many other routines.   
> 
> I may have several co-programmers I do not trust :-)
> 
> What is the simplest way to do this (in Ada) ?
> 

A : constant array ( ... ) of ... := file_reading_function (...);

Wilhelm




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

* RE: Simplest way to protect a variable ?
  2001-09-07  8:45 ` Wilhelm Spickermann
@ 2001-09-07  9:13   ` Reinert Korsnes
  2001-09-07  9:16     ` Reinert Korsnes
  2001-09-07 13:15     ` Wilhelm Spickermann
  2001-09-07 11:35   ` Reinert Korsnes
  1 sibling, 2 replies; 11+ messages in thread
From: Reinert Korsnes @ 2001-09-07  9:13 UTC (permalink / raw)


Wilhelm Spickermann wrote:

> 
> On 07-Sep-01 Reinert Korsnes wrote:
>> Hi,
>> 
>> Let's say I initiate a "global" variable, A (for example an array), by
>> calling a procedure (which may read data from a file).  And I want to be
>> sure that the content (value) of this variable is not changed by
>> another procedure.  The variable is "global" in the sense that it is
>> available in many many other routines.
>> 
>> I may have several co-programmers I do not trust :-)
>> 
>> What is the simplest way to do this (in Ada) ?
>> 
> 
> A : constant array ( ... ) of ... := file_reading_function (...);
> 
> Wilhelm
> 
> 

Yes, I got this idea but the file reading function reads mode than 
a simple array.  I could collect the variables in a record maybe...

reinert


-- 
http://home.chello.no/~rkorsnes



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

* RE: Simplest way to protect a variable ?
  2001-09-07  9:13   ` Reinert Korsnes
@ 2001-09-07  9:16     ` Reinert Korsnes
  2001-09-07 13:15     ` Wilhelm Spickermann
  1 sibling, 0 replies; 11+ messages in thread
From: Reinert Korsnes @ 2001-09-07  9:16 UTC (permalink / raw)


Reinert Korsnes wrote:

> Wilhelm Spickermann wrote:
> 
>> 
>> On 07-Sep-01 Reinert Korsnes wrote:
>>> Hi,
>>> 
>>> Let's say I initiate a "global" variable, A (for example an array), by
>>> calling a procedure (which may read data from a file).  And I want to be
>>> sure that the content (value) of this variable is not changed by
>>> another procedure.  The variable is "global" in the sense that it is
>>> available in many many other routines.
>>> 
>>> I may have several co-programmers I do not trust :-)
>>> 
>>> What is the simplest way to do this (in Ada) ?
>>> 
>> 
>> A : constant array ( ... ) of ... := file_reading_function (...);
>> 
>> Wilhelm
>> 
>> 
> 
> Yes, I got this idea but the file reading function reads mode than

"*more* than a ..."  :-)

> a simple array.  I could collect the variables in a record maybe...
> 
> reinert
> 
> 

-- 
http://home.chello.no/~rkorsnes



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

* RE: Simplest way to protect a variable ?
  2001-09-07  8:45 ` Wilhelm Spickermann
  2001-09-07  9:13   ` Reinert Korsnes
@ 2001-09-07 11:35   ` Reinert Korsnes
  2001-09-07 13:26     ` Ted Dennison
  1 sibling, 1 reply; 11+ messages in thread
From: Reinert Korsnes @ 2001-09-07 11:35 UTC (permalink / raw)


Wilhelm Spickermann wrote:

> 
> On 07-Sep-01 Reinert Korsnes wrote:
>> Hi,
>> 
>> Let's say I initiate a "global" variable, A (for example an array), by
>> calling a procedure (which may read data from a file).  And I want to be
>> sure that the content (value) of this variable is not changed by
>> another procedure.  The variable is "global" in the sense that it is
>> available in many many other routines.
>> 
>> I may have several co-programmers I do not trust :-)
>> 
>> What is the simplest way to do this (in Ada) ?
>> 
> 
> A : constant array ( ... ) of ... := file_reading_function (...);


Obs, in this case I get compilation error message:

"left hand side of assignment must be a variable"

reinert



> 
> Wilhelm
> 
> 

-- 
http://home.chello.no/~rkorsnes



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

* Re: Simplest way to protect a variable ?
  2001-09-07  6:50 Simplest way to protect a variable ? Reinert Korsnes
  2001-09-07  8:21 ` Peter Amey
  2001-09-07  8:45 ` Wilhelm Spickermann
@ 2001-09-07 13:03 ` DuckE
  2001-09-07 13:21 ` Ted Dennison
  3 siblings, 0 replies; 11+ messages in thread
From: DuckE @ 2001-09-07 13:03 UTC (permalink / raw)


Have you considered using a function instead of an array?
That is:
  Define a function in the package spec (use pragma Inline if performance is
an issue) then initialize
the array in the package body and have the function return an eleement.

SteveD

"Reinert Korsnes" <Reinert.Korsnes@ffi.no> wrote in message
news:9n9r4c$srt$1@snipp.uninett.no...
> Hi,
>
> Let's say I initiate a "global" variable, A (for example an array), by
> calling a procedure (which may read data from a file).  And I want to be
> sure that the content (value) of this variable is not changed by
> another procedure.  The variable is "global" in the sense that it is
> available in many many other routines.
>
> I may have several co-programmers I do not trust :-)
>
> What is the simplest way to do this (in Ada) ?
>
> reinert
>
> --
> http://home.chello.no/~rkorsnes





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

* RE: Simplest way to protect a variable ?
  2001-09-07  9:13   ` Reinert Korsnes
  2001-09-07  9:16     ` Reinert Korsnes
@ 2001-09-07 13:15     ` Wilhelm Spickermann
  1 sibling, 0 replies; 11+ messages in thread
From: Wilhelm Spickermann @ 2001-09-07 13:15 UTC (permalink / raw)
  To: comp.lang.ada


On 07-Sep-01 Reinert Korsnes wrote:
> Wilhelm Spickermann wrote:
> 
>> 
>> On 07-Sep-01 Reinert Korsnes wrote:
>>> Hi,
>>> 
>>> Let's say I initiate a "global" variable, A (for example an array), by
>>> calling a procedure (which may read data from a file).  And I want to be
>>> sure that the content (value) of this variable is not changed by
>>> another procedure.  The variable is "global" in the sense that it is
>>> available in many many other routines.
>>> 
>>> I may have several co-programmers I do not trust :-)
>>> 
>>> What is the simplest way to do this (in Ada) ?
>>> 
>> 
>> A : constant array ( ... ) of ... := file_reading_function (...);
>> 
>> Wilhelm
>> 
>> 
> 
> Yes, I got this idea but the file reading function reads mode than 
> a simple array.  I could collect the variables in a record maybe...

Another possibility is to put all these things into a package and use read-only
pointers ("access constant"). In the initialization of the package body
the file is read and all the variables (only one in the example code) are
filled:

package A_Package is
   pragma Elaborate_Body;
   type A_Type is array (1 .. 3) of Integer;
   type A_Read_Only_Access is access constant A_Type;
   Read_Only_A : constant A_Read_Only_Access;
private
   A : aliased A_Type;
   Read_Only_A : constant A_Read_Only_Access := A'ACCESS;
end A_Package;

package body A_Package is
begin
   --  read the file and fill Variable A
   ...
end A_Package;

Wilhelm




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

* Re: Simplest way to protect a variable ?
  2001-09-07  6:50 Simplest way to protect a variable ? Reinert Korsnes
                   ` (2 preceding siblings ...)
  2001-09-07 13:03 ` DuckE
@ 2001-09-07 13:21 ` Ted Dennison
  3 siblings, 0 replies; 11+ messages in thread
From: Ted Dennison @ 2001-09-07 13:21 UTC (permalink / raw)


In article <9n9r4c$srt$1@snipp.uninett.no>, Reinert Korsnes says...
>Let's say I initiate a "global" variable, A (for example an array), by
>calling a procedure (which may read data from a file).  And I want to be
>sure that the content (value) of this variable is not changed by
>another procedure.  The variable is "global" in the sense that it is 
>available in many many other routines.   

If I take the literal interpretation of what you are saying, you want one
routine to be able to set it once, and everyone else only having read-only
access.

The easiest way to do that is to declare it as a constant in its package spec.
If you need a function to initialize it, put the function in a package that
doesn't depend on your package's body, and do a pramga elaborate_all on its
package when you "with" it.

A more flexible way to do it is to put the global variable inside the package
body of the setter. For all the read-only clients, provide a function in the
package spec that returns the current value of the variable. Code in the package
body can now muck with your global at will, but no-one else can (unless you
provide them a routine to do so). Among other benifits, this allows you to
change the implementation of your "global" without affecting your read-only
clients.

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

* Re: RE: Simplest way to protect a variable ?
  2001-09-07 11:35   ` Reinert Korsnes
@ 2001-09-07 13:26     ` Ted Dennison
  2001-09-09 18:03       ` martin.m.dowie
  0 siblings, 1 reply; 11+ messages in thread
From: Ted Dennison @ 2001-09-07 13:26 UTC (permalink / raw)


In article <9nabri$3i4$1@snipp.uninett.no>, Reinert Korsnes says...
>
>> A : constant array ( ... ) of ... := file_reading_function (...);
>
>
>Obs, in this case I get compilation error message:
>
>"left hand side of assignment must be a variable"

Try declaring the type first. I don't think you can actually use an anynomous
array type in a constant declaration like this.

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

* Re: RE: Simplest way to protect a variable ?
  2001-09-07 13:26     ` Ted Dennison
@ 2001-09-09 18:03       ` martin.m.dowie
  0 siblings, 0 replies; 11+ messages in thread
From: martin.m.dowie @ 2001-09-09 18:03 UTC (permalink / raw)


> >> A : constant array ( ... ) of ... := file_reading_function (...);
> >
> Try declaring the type first. I don't think you can actually use an
anynomous
> array type in a constant declaration like this.

can't be annonymous as what type of object would the function return?






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

end of thread, other threads:[~2001-09-09 18:03 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-09-07  6:50 Simplest way to protect a variable ? Reinert Korsnes
2001-09-07  8:21 ` Peter Amey
2001-09-07  8:45 ` Wilhelm Spickermann
2001-09-07  9:13   ` Reinert Korsnes
2001-09-07  9:16     ` Reinert Korsnes
2001-09-07 13:15     ` Wilhelm Spickermann
2001-09-07 11:35   ` Reinert Korsnes
2001-09-07 13:26     ` Ted Dennison
2001-09-09 18:03       ` martin.m.dowie
2001-09-07 13:03 ` DuckE
2001-09-07 13:21 ` Ted Dennison

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