comp.lang.ada
 help / color / mirror / Atom feed
* Initialising stuff
@ 2001-10-16 20:16 chris.danx
  2001-10-16 20:40 ` Ted Dennison
  2001-10-16 22:54 ` Mark Johnson
  0 siblings, 2 replies; 7+ messages in thread
From: chris.danx @ 2001-10-16 20:16 UTC (permalink / raw)


Hi,

I need a variable (global) for a screen package (write to video memory) and
would like it to be initialised once and once only when the package is
"with"ed.

I can think of one way to do this...

package body xxx is
   ...
begin
   initialise screen stuff here;
end xxx;

but I'm unsure if this will work as I don't know the issues involved.  Will
this work bearing in mind the purpose of the package?

Is the following the same or similar?

package xxx is

    ...

    private
        type yyy
            is record
                abc : byte := 0;
            end record;
        a_yyy : yyy;
end xxx;


Any advice?


Thanks,
Chris




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

* Re: Initialising stuff
  2001-10-16 20:16 Initialising stuff chris.danx
@ 2001-10-16 20:40 ` Ted Dennison
  2001-10-16 21:26   ` chris.danx
  2001-10-16 22:54 ` Mark Johnson
  1 sibling, 1 reply; 7+ messages in thread
From: Ted Dennison @ 2001-10-16 20:40 UTC (permalink / raw)


In article <EA0z7.4255$nT1.766306@news6-win.server.ntlworld.com>, chris.danx
says...
>I need a variable (global) for a screen package (write to video memory) and
>would like it to be initialised once and once only when the package is
>"with"ed.

Why not just initialize the variable where it is declared then?

>package body xxx is
>   ...
>begin
>   initialise screen stuff here;
>end xxx;

That'll work too. The only possible "issues" are if you try to reference stuff
in another package here, you have to make sure that package (and everything it
depends on) has already been elaborated. But if its a one-liner, you might as
well just initialize the variable at the declaration and be done with it.

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

No trees were killed in the sending of this message. 
However a large number of electrons were terribly inconvenienced.



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

* Re: Initialising stuff
  2001-10-16 20:40 ` Ted Dennison
@ 2001-10-16 21:26   ` chris.danx
  2001-10-17  2:42     ` Jeffrey Carter
  0 siblings, 1 reply; 7+ messages in thread
From: chris.danx @ 2001-10-16 21:26 UTC (permalink / raw)



"Ted Dennison" <dennison@telepath.com> wrote in message
news:TW0z7.31810$ev2.38911@www.newsranger.com...
> In article <EA0z7.4255$nT1.766306@news6-win.server.ntlworld.com>,
chris.danx
> says...
> >I need a variable (global) for a screen package (write to video memory)
and
> >would like it to be initialised once and once only when the package is
> >"with"ed.
>
> Why not just initialize the variable where it is declared then?

It's private and global inside the screens package, outside it can't be
accessed.  The operations such as put, gotoxy etc manipulate the variable,
and to ensure proper working it requires initialisation.



>
> >package body xxx is
> >   ...
> >begin
> >   initialise screen stuff here;
> >end xxx;
>
> That'll work too. The only possible "issues" are if you try to reference
stuff
> in another package here, you have to make sure that package (and
everything it
> depends on) has already been elaborated. But if its a one-liner, you might
as
> well just initialize the variable at the declaration and be done with it.






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

* Re: Initialising stuff
  2001-10-16 20:16 Initialising stuff chris.danx
  2001-10-16 20:40 ` Ted Dennison
@ 2001-10-16 22:54 ` Mark Johnson
  2001-10-16 23:27   ` chris.danx
  1 sibling, 1 reply; 7+ messages in thread
From: Mark Johnson @ 2001-10-16 22:54 UTC (permalink / raw)


"chris.danx" wrote:

> Hi,
>
> I need a variable (global) for a screen package (write to video memory) and
> would like it to be initialised once and once only when the package is
> "with"ed.

Not sure what you mean by "with"ed. If I with your package 5 times, do you want
the global variable initialized 5 times? Somehow I don't think so.

If you want it initialized just once, the "Ada word" you are looking for is
elaboration. Basically, it is code that runs prior to "execution" to create and
initialize all the variables that are declared in each package. It is a simple
idea but in any complex system has a lot of issues to be addressed.

> I can think of one way to do this...
>
> package body xxx is
>    ...
> begin
>    initialise screen stuff here;
> end xxx;
>
> but I'm unsure if this will work as I don't know the issues involved.  Will
> this work bearing in mind the purpose of the package?

In general, yes. There are cases where a package does not require a body. If
you use GNAT as the Ada compiler, it can warn you of that situation. Not sure
on other vendor products.

> Is the following the same or similar?
>
> package xxx is
>
>     ...
>
>     private
>         type yyy
>             is record
>                 abc : byte := 0;
>             end record;
>         a_yyy : yyy;
> end xxx;

Other than the extra level of declarations - yes. You could just as easily
say....

package xxx is

type Byte is mod 256;
yyy : Byte := 0;
...
end xxx;

and avoid the use of the record structure. If you use gnat as the compiler and
gvd as the debugger, you can step through the elaboration code as it executes,
set break points on variable initialization, and so on. Works really nice to
find the problems in elaboration that you might not be able to find any other
way.

  --Mark




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

* Re: Initialising stuff
  2001-10-16 22:54 ` Mark Johnson
@ 2001-10-16 23:27   ` chris.danx
  0 siblings, 0 replies; 7+ messages in thread
From: chris.danx @ 2001-10-16 23:27 UTC (permalink / raw)



"Mark Johnson" <Mark_H_Johnson@Raytheon.com> wrote in message
news:3BCCBA9F.544FC58B@Raytheon.com...
> "chris.danx" wrote:
>
> > Hi,
> >
> > I need a variable (global) for a screen package (write to video memory)
and
> > would like it to be initialised once and once only when the package is
> > "with"ed.
>
> Not sure what you mean by "with"ed. If I with your package 5 times, do you
want
> the global variable initialized 5 times? Somehow I don't think so.

You are correct, only once.


> If you want it initialized just once, the "Ada word" you are looking for
is
> elaboration. Basically, it is code that runs prior to "execution" to
create and
> initialize all the variables that are declared in each package. It is a
simple
> idea but in any complex system has a lot of issues to be addressed.

Now I get the "elaboration" idea, thanks.  I'd read about it but couldn't
quite get my head around the idea (part of the confusion was that i'd seen a
pragma preelaborate in a similar package by Serge Robyns, and not knowing
what it really meant I didn't use it.  The difference between that package
and my own is that I use a screen_info record to hold the position and
current global attribute and initialise them inside the begin while Serges'
package uses two natural subtypes in the body (and initialises them upon
declaration).

I can do that too, it's a lot simpler and nicer.


> > Is the following the same or similar?
> >
> > package xxx is
> >
> >     ...
> >
> >     private
> >         type yyy
> >             is record
> >                 abc : byte := 0;
> >             end record;
> >         a_yyy : yyy;
> > end xxx;
>
> Other than the extra level of declarations - yes. You could just as easily
> say....
>
> package xxx is
>
> type Byte is mod 256;
> yyy : Byte := 0;
> ...
> end xxx;

That was just an example, sorry (forgot the ... in the record, sorry).


> and avoid the use of the record structure. If you use gnat as the compiler
and
> gvd as the debugger, you can step through the elaboration code as it
executes,
> set break points on variable initialization, and so on. Works really nice
to
> find the problems in elaboration that you might not be able to find any
other
> way.

I'll try to remember that in future, it's good advice.



Thanks,
Chris




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

* Re: Initialising stuff
  2001-10-16 21:26   ` chris.danx
@ 2001-10-17  2:42     ` Jeffrey Carter
  2001-10-18  8:18       ` chris.danx
  0 siblings, 1 reply; 7+ messages in thread
From: Jeffrey Carter @ 2001-10-17  2:42 UTC (permalink / raw)


"chris.danx" wrote:
> 
> It's private and global inside the screens package, outside it can't be
> accessed.  The operations such as put, gotoxy etc manipulate the variable,
> and to ensure proper working it requires initialisation.

That's not global, that's package state data. Whether you initialize it
where it's declared, or provide default initial values for all the
components of a record, or put a bunch of elaborate initialization code
in the package body, it's initialized during elaboration of the package.

-- 
Jeff Carter
"You couldn't catch clap in a brothel, silly English K...niggets."
Monty Python & the Holy Grail



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

* Re: Initialising stuff
  2001-10-17  2:42     ` Jeffrey Carter
@ 2001-10-18  8:18       ` chris.danx
  0 siblings, 0 replies; 7+ messages in thread
From: chris.danx @ 2001-10-18  8:18 UTC (permalink / raw)



"Jeffrey Carter" <jrcarter@acm.org> wrote in message
news:3BCCF006.B3A57E6F@acm.org...
> "chris.danx" wrote:
> >
> > It's private and global inside the screens package, outside it can't be
> > accessed.  The operations such as put, gotoxy etc manipulate the
variable,
> > and to ensure proper working it requires initialisation.
>
> That's not global, that's package state data.

Ok, my jargon isn't great but now I know, I'll call it that.

> Whether you initialize it
> where it's declared, or provide default initial values for all the
> components of a record, or put a bunch of elaborate initialization code
> in the package body, it's initialized during elaboration of the package.

Not much difference other than convienance then? <rh>  I'll just default
initial values for record components, it's simpler(in the sense that it
doesn't have an explicit initialisation block in the package).


Chris




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

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

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-10-16 20:16 Initialising stuff chris.danx
2001-10-16 20:40 ` Ted Dennison
2001-10-16 21:26   ` chris.danx
2001-10-17  2:42     ` Jeffrey Carter
2001-10-18  8:18       ` chris.danx
2001-10-16 22:54 ` Mark Johnson
2001-10-16 23:27   ` chris.danx

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