简体   繁体   中英

Ada left hand of assignment must not be limited type

I am at loss after spending hours on this.

Protected type declaration:

   protected type AdditionMachine is
      procedure setTask (Item : in WorkerTask);
      procedure getTask (Item : out WorkerTask);
      procedure calculate;
   private
      machineType : String (1 .. 1) := "A";
      job : workerTask;
   end AdditionMachine;

Task:

   task body SimpleTask1 is 
     Available     : Natural := ADDITION_MACHINES;
      Elements      : array (1 .. ADDITION_MACHINES) of AdditionMachine;
   begin
     loop
        select
           when Available > 0 =>
              accept getMachine (machine : out AdditionMachine) do
                  machine := Elements(Available);
                  Available := Available - 1;
              end getMachine;
        or
            accept addMachine (machine : in AdditionMachine) do
                 Available := Available + 1;
                 Elements(Available) := machine;
              end addMachine;
        end select;
     end loop;
   end  SimpleTask1;

At line Elements(Available) := machine; and machine := Elements(Available); I get "left hand of assingment must not be limited type"

I have no idea how to fix this, I've found nothing when I googled, can anyone help?

edit: I now know that protected types are limited, but how do I accomplish a pool of protected objects without something like above?

From ARM 7.5 , Limited Types,

A limited type is (a view of) a type for which copying (such as for an assignment_statement) is not allowed. A nonlimited type is a (view of a) type for which copying is allowed.

However, you are allowed to copy accesses to limited objects.

So, if you have declared

protected type AdditionMachine is
   ...
end AdditionMachine;
type AdditionMachine_Access is access AdditionMachine;

you can write something like this (I haven't tested it):

task body SimpleTask1 is 
   Available : Natural := ADDITION_MACHINES;
   Elements  : array (1 .. ADDITION_MACHINES) of AdditionMachine_Access
     := (others => new AdditionMachine);
begin

You'll need to take care of deallocating the allocated objects when you've done with them (if the program needs to run indefinitely, anyway). I don't think there'll be a problem with protected objects (but what happens if a task is waiting on a PO that you deallocate?), and recent GNATs are OK with deallocating tasks provided the tasks have open terminate alternatives, ARM 9.7.1 , or have already terminated - perhaps by being aborted).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM