types - Ada 将有限的私有(private)类型传递给任务

标签 types private ada

我有一种情况,我想为任务提供有限的私有(private)类型,以便它可以调用其上的各种函数(特别是它是来自 GNAT.Serial_Communications 的串行端口)。但我似乎不知道如何让它发挥作用。

我尝试过直接传递它,直到我必须使用该类型为止,这一直有效,此时我无法将其复制到已保存的变量:

task body MyTask is
    MyT : LPType;
begin
    accept MyEntry (t : LPType) do
        MyT := t; -- not valid, LPType is limited private
    end MyEntry;
    -- Later I do something using MyT
end MyTask;

由于这不起作用,我尝试传递对有限私有(private)类型的访问权限,不幸的是,由于访问的局部性,这仍然不起作用:

procedure MyProcedure is
    MyT : aliased LPType;
    Task : MyTask;
begin
    Initialize (MyT);

    Task.MyEntry (MyT'Access);
end MyProcedure;

从技术上讲,我是通过使用 Unrestricted_Access 来编译它的,但这感觉像是一种 hack,根据我的经验,当您使用 hack 绕过 Ada 中的编译器时,编译器有充分的理由强制您这样做这样做你就犯了一个错误。

所以我的问题是:是否可以通过条目将有限的私有(private)类型传递给任务,或者这只是一个坏主意?

最佳答案

在 Ada 中,limited 关键字表示该类型没有与其关联的赋值。这与大多数主流语言完全相反,但考虑诸如[物理]时钟或RNG类型之类的东西:复制这些没有任何意义,因此以这种方式建模是有意义的。

但是,task 构造不能保证与调用其条目的线程位于同一内存空间中。 -- 所以我们在这里有点矛盾:limited 禁止复制,而 entry 需要进行某种复制。

有多种方法可以解决此问题,但根据您提供的代码片段,解决方案是将 task 设为 limited 的组件> 类型。

Package Example is
    Type LP is limited private;
    Function Create return LP;
    Function Create( Value : Integer ) return LP;
    Procedure Print( Object : LP );

Private

    Task Type Printer( Item : not null access LP ) is
        Entry Print;
    End Printer;

    Type LP is limited record
        Value     : Integer := 100;
        Task_Item : Printer(LP'Access);
    end record;

end Example;

Package Body Example is
    Function Create return LP is (others => <>);
    Function Create( Value : Integer ) return LP is
      (Value => Value, others => <>);
    Procedure Print( Object : LP ) is
    Begin
        Object.Task_Item.Print;
    end Print;

    Task Body Printer is
    Begin
        accept Print do
            Null;
        end Print;
        Ada.Text_IO.Put_Line( "Value:" & Integer'Image(Item.Value) );
    End Printer;
end Example;

关于types - Ada 将有限的私有(private)类型传递给任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44812755/

相关文章:

ada - Ada 编程语言在军队中仍然有用吗?

programming-languages - 非升降式产品的缺点?

typescript - 是否有可互操作的浏览器扩展对象的@types?

java - 使用 System.in.read() 时类型不匹配

C++ - 从类外部访问类的私有(private)成员

ruby - 'private' 以它在 Ruby 中的方式工作是否有充分的理由?

java - 带有继承的Java程序应该如何追溯?

gcc - 使用 GCC 编译 Ada

c# - 声明类型列表

performance - Ada 是如何实现 'mod' 和 'range' 类型的?性能影响是什么?