Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating procedure or function in Ada task

I am creating the following task in Ada and I want it to contain a procedure that tells me the count of my buffer. How can I do that?

package body Buffer is

  task body Buffer is

    size: constant := 10000; -- buffer capacity
    buf: array(1.. size) of Types.Item;
    count: integer range 0..size := 0;
    in_index,out_index:integer range 1..size := 1;

  begin
    procedure getCount(currentCount: out Integer) is
    begin   
      currentCount := count;
    end getCount;   

    loop
      select
        when count<size =>
          accept put(item: in Types.Item) do
            buf(in_index) := item;
          end put;
          in_index := in_index mod size+1;
          count := count + 1;
        or
          when count>0 =>
            accept get(item:out Types.Item) do
              item := buf(out_index);
            end get;
            out_index := out_index mod size+1;
            count := count - 1;
        or
          terminate;
      end select;
    end loop;
  end Buffer;

end Buffer;

When I compile this code I get an error that

declarations must come before "begin"

referring to the definition of the getCount procedure.

like image 907
Madu Avatar asked Feb 27 '26 23:02

Madu


2 Answers

A declaration must come before "begin", and your declaration of "getCount" is following the "begin". Relocate it:

procedure getCount(currentCount: out Integer) is
begin   
    currentCount := count;
end getCount;   

begin

But really, pay attention to trashgod's advice.

like image 192
Marc C Avatar answered Mar 02 '26 06:03

Marc C


The immediate problem is that you have specified a subprogram body without having first specified a corresponding subprogram declaration in the handled sequence of statements part of your task body. It should go in the declarative part, as shown here.

The larger problem appears to be creating a bounded buffer, for which a protected type seems more suitable. Examples may be found in §II.9 Protected Types and §9.1 Protected Types. In protected type Bounded_Buffer, you could add a

function Get_Count return Integer;

having a body like this:

function Get_Count return Integer is
begin
   return Count;
end Get_Count;
like image 34
trashgod Avatar answered Mar 02 '26 05:03

trashgod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!