2012-04-28 6 views
2

Adaで次のタスクを作成していますが、私のバッファのカウントを教えてくれるプロシージャが含まれています。どうやってやるの?Adaタスクのプロシージャまたはファンクションを作成する

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; 

私はこのコードをコンパイルするとき、私は

宣言はgetCountプロシージャの定義を参照

を「開始」の前に来なければならないというエラーが発生します。

答えて

5

宣言は、「開始」の前に来なければならない、と「同様にgetCount」のあなたの宣言は、以下のある「始まります"それを再配置してください:

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

begin 

本当に、trashgodのアドバイスに注意してください。

+0

+1よく目に付きます。 @Keith Thompsonの改良された書式設定が、これをもっと簡単に見えるのは魅力的です。 – trashgod

6

当面の問題は、あなたが最初の文あなたtask bodyの一部の取り扱うシーケンスに対応するsubprogram declarationを指定せずにsubprogram body が指定されていることです。これは、 hereのように、 宣言部に入る必要があります。

より大きな問題は、protected typeがより適していると思われる有界バッファを作成するように見えます。例は、§II.9 Protected Typesおよび§9.1 Protected Typesに見つけることができます。 protected type Bounded_Bufferでは、このような本体を有する

function Get_Count return Integer; 

を追加することができます。

function Get_Count return Integer is 
begin 
    return Count; 
end Get_Count;