在ADA中很简单:
type ITEM_RECORD;
type ITEM_ACCESS is access ITEM_RECORD;
type ITEM_RECORD Is
record
ITEM: item_type;
Next: item_access;
Pred: item_access;
end record;
容易,对吧?现在,如果我想成为通用软件包中声明的智能/安全指针,该怎么办?我直观地做到这一点,只要它起作用:
type ITEM_access;
type Item_Record is record
Item : Item_Type;
Next : Item_Access;
Pred : Item_Access;
end record;
type pointers_on_record is access Item_record;
package pointers_p is new pointers(Item_Record, pointers_on_record);
type item_access is new pointers_p.Pointer_Type;
通用规格是以下内容:
generic
type Item_Type(<>) is limited private;
type Access_Type is access Item_Type;
package Pointers is
type Pointer_Type is private;
我还没有弄清楚如何做。
谢谢!
由于循环依赖性,您要创建的结构是不可能的。考虑一下:
通用软件包定义指针。Pointer_Type
的结构和实现(可能(取决于通用参数Item_Type
(这不一定是正确的,但是如果不是正确的,则无需将Item_Type
作为通用参数(。现在,在您的通用软件包实例化中的Item_Type
包含来自通用软件包的两个智能指针,因此取决于Pointer_Type
的结构。那是一个经典的鸡肉或蛋的问题。
因此,解决方案是更改类型的设计。让我给你一些指示(没有双关语(:
看来您正在实现双关联列表。请注意,由于列表的循环性质,使用实施参考计数的智能指针是一个严重的错误。如果您的列表至少有两个项目,因为它们总是彼此指向,什么都不会被划分。因此,除非您的智能指针进行周期检测(根据您的规格是不可能的(,否则您不能使用智能指针以您想要的方式。
一个可能的解决方案是将智能指针用于Item_Type
,而不是记录。您需要手动处理记录,但是无论如何您都需要如上所述这样做。
一个不同的解决方案是为整个列表提供全局参考计数器。创建一个不透明的列表类型,该类型为列表提供访问器和迭代器子例程,该列表将智能指针分配给项目。智能指针增加并减少了整个列表上的参考计数,一旦对列表的最后一个引用消失,整个列表将被交易。因此,只要至少一个提到其中的东西就存在。该解决方案将要求您自己实施参考计数,因为它专门用于列表结构。
最后,您当然可以像Jeffrey建议的那样使用Ada.Containers.Doubly_Linked_Lists
。正如我在我的第一个解决方案中所建议的那样,您可以将智能指针放入Item_Type
。
为了使用智能指针进行操作,您的智能指针必须使用不完整的类型。因此,您将进一步提供删除访问变量(当然还有访问类型(的最终确定过程。当然,这也意味着您的分配功能需要采用访问类型而不是变量。最后,您绝对需要使用弱指针打破参考计数智能指针产生的周期性参考。
generic
type Item_Type(<>);
type Item_Access is access Item_Type;
with procedure Finalize(Ref : in out Item_Access);
package Pointers is
function Make(Ref : not null Item_Access) return Smart_Pointer;
-- other stuff
end Pointers;
然后您可以做类似:
的事情 type Node_Impl;
type Node_Access is access Node_Impl;
procedure Finalize(Ref : in out Node_Access);
package Ptrs is new Pointers(Node_Impl,Node_Access,Finalize);
subtype Node is Ptrs.Smart_Pointer;
subtype Weak_Node is Ptrs.Weak_Pointer;
type Node_Impl is record
Value : Some_Type;
Next : Node;
Prev : Weak_Node;
end record;
这是我用来制作带有智能指针的AVL树的示例规范。我没有方便的链接列表示例。
package Trees is
type Node;
type Node_Access is access Node;
procedure Finalize(Memory : in out Node_Access);
package Node_Smart_Access is new Smart_Access
(Item_Type => Node,
Item_Access => Node_Access,
Finalize => Finalize,
Atomic_Increment => True);
type Node is record
Value : Integer := 0;
Height : Integer := 1;
Parent : Node_Smart_Access.Weak_Access;
Left : Node_Smart_Access.Shared_Access;
Right : Node_Smart_Access.Shared_Access;
end record;
type Tree is tagged record
Root : Node_Smart_Access.Shared_Access;
end record;
end Trees;
我的智能指针规范是:
generic
type Item_Type(<>);
type Item_Access is access Item_Type;
with procedure Finalize(Memory : in out Item_Access);
Atomic_Increment : Boolean := True;
package Smart_Access is
type Shared_Access is new Ada.Finalization.Controlled with private;
type Weak_Access is new Ada.Finalization.Controlled with private;
-- more stuff
package Make is
function Shared_Access
(Source : in not null Item_Access)
return Smart_Access.Shared_Access;
-- more stuff
end Make;
private
-- implementation
end Smart_Access;
这很麻烦,但是如果您想使用ADA中的智能指针制作自我参考类型,则需要。另请注意,如果您在智能指针的规范中使用不完整的类型,则某些版本的GNAT具有具有隐式_derference方面的编译器错误。如果您使用的版本具有错误,则它们会导致编译器崩溃。