program example;
{$mode objfpc}{$H+}
uses
Classes,typinfo
{ you can add units after this };
type
{$m+}
generic datatype<dtype> = class (tpersistent)
public
var
value:dtype;
procedure valinit;
constructor create; overload;
destructor destroy; override;
end;
generic node<dtype> = packed class
type
datum=specialize datatype<dtype>;
gptr=specialize node<dtype>;
var
fwd:gptr;
bck:gptr;
data:datum;
public
constructor create; overload;
destructor destroy; override;
end;
generic chain<gptr, dtype> = packed class
public
var
first:gptr;
last:gptr;
constructor create; overload;
destructor destroy; override;
end;
ennexample=(a,b,c,d);
setexample=set of ennexample;
cardnode=specialize node<cardinal>;
cardchain=specialize chain<cardnode,cardinal>;
setnode=specialize node<setexample>;
setchain=specialize chain<setnode,setexample>;
nodeCschain=specialize node<setchain>;
chainCnodeCschain=specialize chain<nodeCschain,setchain>;
{**************************Datatype*********************************************}
procedure datatype.valinit;
type
gptr=specialize node<dtype>;
schain=specialize chain<gptr,dtype>;
var
nvar:gptr;
svar:schain;
begin
if (ptypeinfo(typeinfo(self.value))^.kind = tkset) then
begin
writeln('set');
{self.value:=[];}
end
else if (ptypeinfo(typeinfo(self.value))^.kind = tkinteger) then
begin
writeln('cardinal');
{self.value:=0;}
end
else if (ptypeinfo(typeinfo(self.value))^.kind = tkclass) then
begin
if (gettypedata(typeinfo(self.value))^.ClassType.ClassName =
gettypedata(typeinfo(svar.first.data.value))^.ClassType.ClassName) then
begin
writeln(gettypedata(typeinfo(self.value))^.ClassType.ClassName);
{self.value:=schain.create;} {create value as chain}
end
else if (gettypedata(typeinfo(self.value))^.ClassType.ClassName =
gettypedata(typeinfo(nvar.data.value))^.ClassType.ClassName) then
begin
writeln(gettypedata(typeinfo(self.value))^.ClassType.ClassName);
{self.value:=gptr.create;} {create value as node}
end;
end;
end;
constructor datatype.create; overload; {create datatype}
begin
inherited; {inhereted from root}
self.valinit;
end;
destructor datatype.destroy; {destroy datatype}
begin
inherited; {inhereted from root}
end;
{**************************Node*************************************************}
constructor node.create; overload; {create node}
begin
inherited; {inhereted from root}
data:=datum.create; {create data}
end;
destructor node.destroy; {destroy node}
begin
inherited; {inhereted from root}
end;
{**************************Chain************************************************}
constructor chain.create; overload; {creates instances of chain}
begin
inherited; {inhereted from root}
self.first:=gptr.create;
self.last:=self.first;
end;
destructor chain.destroy; {releases instances of chain}
begin
inherited; {inhereted from root}
end;
{**************************Variables********************************************}
var
cnode:cardnode;
snode:setnode;
nCchain:nodeCschain;
cCnodeCchain:chainCnodeCschain;
{**************************Main*************************************************}
begin
cnode:=cardnode.create;
snode:=setnode.create;
nCchain:=nodeCschain.create;
cCnodeCchain:=chainCnodeCschain.create;
readln;
end.