comp.lang.ada
 help / color / mirror / Atom feed
* How to include shared code
@ 2013-09-19  0:17 Emanuel Berg
  2013-09-19  5:31 ` Peter Brooks
  2013-09-19  7:20 ` Dmitry A. Kazakov
  0 siblings, 2 replies; 4+ messages in thread
From: Emanuel Berg @ 2013-09-19  0:17 UTC (permalink / raw)


If I have two files:

protected_buffer.adb
bounded_buffer.adb

and those compile and run fine.

But, in both those, I have this piece of code:

function Random_Integer return integer is
   type Span is range 1..9;
   package Random_Integer is new Ada.Numerics.Discrete_Random(Span);
   Seed    : Random_Integer.Generator;
   Outcome : Span;
begin
   Random_Integer.Reset(seed);
   Outcome := Random_Integer.Random(seed);
   return INTEGER(Outcome);
end Random_Integer;

function Random_Period return Time_Span is
begin
   return Milliseconds(Random_Integer*100);
end Random_Period;

Now, how can I extract that piece of code from both files, and put
it into another file, say, random_period.adb, and then "include"
it into each of the above files (that operate in isolation)?

-- 
Emanuel Berg - programmer (hire me! CV below)
computer projects: http://user.it.uu.se/~embe8573
internet activity: http://home.student.uu.se/embe8573

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

* Re: How to include shared code
  2013-09-19  0:17 How to include shared code Emanuel Berg
@ 2013-09-19  5:31 ` Peter Brooks
  2013-09-19  6:24   ` Simon Wright
  2013-09-19  7:20 ` Dmitry A. Kazakov
  1 sibling, 1 reply; 4+ messages in thread
From: Peter Brooks @ 2013-09-19  5:31 UTC (permalink / raw)


On Thursday, 19 September 2013 02:17:52 UTC+2, Emanuel Berg  wrote:
> 
> Now, how can I extract that piece of code from both files, and put
> it into another file, say, random_period.adb, and then "include"
> it into each of the above files (that operate in isolation)?
> 
That's what .ads files are for - you specify your function (and everything else you need shared) in a package in a .ads file. Then the gnat make will find and incorporate it:

http://blog.projectpolymath.org/ada-2012-tutorial_01/

gives an example of what a .ads file looks like at the bottom.

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

* Re: How to include shared code
  2013-09-19  5:31 ` Peter Brooks
@ 2013-09-19  6:24   ` Simon Wright
  0 siblings, 0 replies; 4+ messages in thread
From: Simon Wright @ 2013-09-19  6:24 UTC (permalink / raw)


Peter Brooks <peter.h.m.brooks@gmail.com> writes:

> On Thursday, 19 September 2013 02:17:52 UTC+2, Emanuel Berg  wrote:
>> 
>> Now, how can I extract that piece of code from both files, and put
>> it into another file, say, random_period.adb, and then "include"
>> it into each of the above files (that operate in isolation)?
>> 
> That's what .ads files are for - you specify your function (and
> everything else you need shared) in a package in a .ads file. Then the
> gnat make will find and incorporate it:
>
> http://blog.projectpolymath.org/ada-2012-tutorial_01/
>
> gives an example of what a .ads file looks like at the bottom.

The relevant chapter in "Ada 95: The Craft of Object-Oriented
Programming" is http://archive.adaic.com/docs/craft/html/ch04.htm#4.7
(NB, external references in this document won't work)


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

* Re: How to include shared code
  2013-09-19  0:17 How to include shared code Emanuel Berg
  2013-09-19  5:31 ` Peter Brooks
@ 2013-09-19  7:20 ` Dmitry A. Kazakov
  1 sibling, 0 replies; 4+ messages in thread
From: Dmitry A. Kazakov @ 2013-09-19  7:20 UTC (permalink / raw)


On Thu, 19 Sep 2013 02:17:52 +0200, Emanuel Berg wrote:

> If I have two files:
> 
> protected_buffer.adb
> bounded_buffer.adb
> 
> and those compile and run fine.
> 
> But, in both those, I have this piece of code:
> 
> function Random_Integer return integer is
>    type Span is range 1..9;
>    package Random_Integer is new Ada.Numerics.Discrete_Random(Span);
>    Seed    : Random_Integer.Generator;
> begin
>    Random_Integer.Reset(seed);
>    Outcome := Random_Integer.Random(seed);

You should keep the generator object (and instantiation of its type)
outside the function that calls it. You also would not reset it each time
you call it. You do this just once after the generator object creation and
only if you want the sequence generated to vary with each program start
(see ARM A.5.2(28).

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


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

end of thread, other threads:[~2013-09-19  7:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-19  0:17 How to include shared code Emanuel Berg
2013-09-19  5:31 ` Peter Brooks
2013-09-19  6:24   ` Simon Wright
2013-09-19  7:20 ` Dmitry A. Kazakov

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