首页 > 解决方案 > 具有持续访问父类型对象的 Ada 记录

问题描述

我最近开始学习 Ada。我想看看是否有可能在 Ada 中创建一个类似Boost::Statechart框架。为此,我需要一个记录结构,该结构具有对父类型对象的常量访问对象,例如静态指向另一个树节点的树节点,并且父指针始终不得更改。像这样的东西:

-- Not working sample

type Node_T is record
    Parent : constant access Node_T;
    -- error: constant components are not permitted
end record;
-- I wish to create objects of this type like this

Top_Node : Node_T (null);
Child1_Node : Node_T (Top_Node'Access);
Child2_Node : Node_T (Top_Node'Access);

Ada 似乎不支持常量成员字段。所以我求助于使用访问判别器:

-- Not working sample

type Node_T (Parent : access Node_T) is null record;
-- error: type declaration cannot refer to itself

但是,使用命名访问类型作为判别式有效

type Node_T;
type Ref_Node_T is access all Node_T;

type Node_T (Parent : Ref_Node_T) is null record;

但是,据我所知,这会导致Node_T对象的生命周期绑定到一个Ref_Node_T对象的生命周期,而不是另一个父Node_T对象的生命周期。这是真的?

有没有更好的方法来实现我所需要的?

标签: ada

解决方案


https://www.sigada.org/ada_letters/june2000/sanden.pdf中描述了创建有限状态机的另一种方法。 此解决方案使用受保护对象和任务的组合来实现有限状态机。


推荐阅读