1

I just started programming on Ada and I was wondering how to code getters and setters to work with classes attributes.

In fact I am interested in obtaining getters and setters of attributes deadline, period and computingTime of the following package:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Real_Time; use Ada.Real_Time;

package body pkg_tasks is
    task body task_a is
        deadline : Time_Span := To_Time_Span(25.0);
        period : Time_Span := To_Time_Span(25.0);
        computingTime : Time_Span := To_Time_Span(10.0);
        startingTime : Time;
    begin
        entry start do
            startingTime := Clock;

            while (Clock - startingTime) < computingTime loop

            end loop;

            New_line;
            Put_Line("End of task A");
        end start;
    end task_a;
end pkg_tasks;
thmasker
  • 406
  • 1
  • 9
  • 21

2 Answers2

3

In the case of a task, it's quite easy... You can't for the same reason that we answered in your last question, tasks can only have entries which act as a way to synchronize tasks (read part on entries and the followings).

But in fact, you could perform a kind of getters as entries and using selective wait depending on when you want to query your attributes.

Now, about setting attributes on your task, using parameters on your start entry seems to me as the best way to do.

As a note, you are writing about classes attributes but you are currently using no classes at all. Tasks are a first-citizen type in Ada and is not implemented through a class type as it's done in Java. Using object oriented programming is a different beast here.

Frédéric Praca
  • 1,620
  • 15
  • 29
2

As said above, generally tasks are not the normal way to do it. Back in Ada83, there were no protected types, so if you needed something like that then you emulated it with a task. That aside, here are some examples using tasks, protected types and classes (or as Ada calls them, Tagged types):

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Real_Time; use Ada.Real_Time;

procedure jdoodle is
    ------------------------------------------------------
    -- Task Example
    ------------------------------------------------------
    task Task_Example is
        entry Get(Time : out Time_Span);
        entry Set(Time : in  Time_Span);
    end Task_Example;

    task body Task_Example is
        Value : Time_Span := To_Time_Span(0.0);
    begin
        loop
            select
                accept Get(Time : out Time_Span) do
                    Time := Value;
                end Get;
            or
                accept Set(Time : in  Time_Span) do
                    Value := Time;
                end Set;
            or
                terminate;
            end select;
        end loop;
    end Task_Example;

    ------------------------------------------------------
    -- Protected type example
    ------------------------------------------------------
    protected type Protected_Example is
        procedure Put(Time : Time_Span); -- or use entry
        function Get return Time_Span;   -- or use procedure or entry
    private
        Value : Time_Span := To_Time_Span(0.0);
    end Protected_Example;

    protected body Protected_Example is
        procedure Put(Time : Time_Span) is
        begin
            Value := Time;
        end Put;
        function Get return Time_Span is
        begin
            return Value;
        end Get;
    end Protected_Example;

    ------------------------------------------------------
    -- Class Example
    ------------------------------------------------------
    package Classes is
        type Class_Example is tagged limited private;
        procedure Put(Self : in out Class_Example; Time : Time_Span);
        function Get(Self : in Class_Example) return Time_Span; -- or use procedure
    private
        type Class_Example is tagged limited record
            Value : Time_Span := To_Time_Span(0.0);
        end record;
    end Classes;

    package body Classes is
        procedure Put(Self : in out Class_Example; Time : Time_Span) is
        begin
            Self.Value := Time;
        end Put;

        function Get(Self : in Class_Example) return Time_Span is
        begin
            return Self.Value;
        end Get;
    end Classes;
begin
    Put_Line("Starting");
end jdoodle;

Keep in mind the tagged type example is also applicable to regular records and other private types.

Jere
  • 3,124
  • 7
  • 15