![]() |
|
|
#1
|
|||
|
|||
|
ок. хочу получить возможность работать со вложенными открытыми массивами, вроде такого:
[ 'a', ['b','c'], ['d',['e','f'],'g'], 'h' ] при этом желательно, чтобы это был массив array of const собственно вопросы возникли по статье: http://www.delphisources.ru/pages/fa..._of_const.html объявляю типы: Код:
type
ArrayOfConst = array of TVarRec;
TArrayOfConst = array [ 0 .. High (WORD) div SizeOf (TVarRec) - 1 ] of TVarRec;
PArrayOfConst = ^TArrayOfConst;
описываю функции слияния: Код:
function PArrayOfConstMerge (anArgs1, anArgs2: array of const) : PArrayOfConst;
var
I : WORD;
Index : WORD;
Length : WORD;
begin
Result := NIL;
try
Length := 0;
if ( High (anArgs1) >= 0 ) then
Length := Length + High (anArgs1) - Low (anArgs1) +1;
if ( High (anArgs2) >= 0 ) then
Length := Length + High (anArgs2) - Low (anArgs2) +1;
if ( Length > 0 ) then
begin
Result := AllocMem ( Length * SizeOf (TVarRec) );
Index := 0;
for I := Low (anArgs1) to High (anArgs1) do
begin
Result^ [Index] := anArgs1 [i];
Inc (Index);
end;
for I := Low (anArgs2) to High (anArgs2) do
begin
Result^ [Index] := anArgs2 [i];
Inc (Index);
end;
end;
except
Result := NIL;
end;
end;
function ArrayOfConstMerge (anArgs1, anArgs2: array of const) : ArrayOfConst;
var
I : WORD;
Index : WORD;
Length : WORD;
begin
Result := NIL;
try
Length := 0;
if ( High (anArgs1) >= 0 ) then
Length := Length + High (anArgs1) - Low (anArgs1) +1;
if ( High (anArgs2) >= 0 ) then
Length := Length + High (anArgs2) - Low (anArgs2) +1;
if ( Length > 0 ) then
begin
SetLength (Result,Length);
Index := 0;
for I := Low (anArgs1) to High (anArgs1) do
begin
Result [Index] := anArgs1 [i];
Inc (Index);
end;
for I := Low (anArgs2) to High (anArgs2) do
begin
Result [Index] := anArgs2 [i];
Inc (Index);
end;
end;
except
Result := NIL;
end;
end;передать окрытый массив в качестве параметра в другой открытый массив нельзя, поэтому передаем указатель на него: Код:
function ArrayOfConstToPointer (anArgs: array of const) : PArrayOfConst;
var
I : WORD;
Index : WORD;
Length : WORD;
begin
Result := NIL;
try
Length := 0;
if ( High (anArgs) >= 0 ) then
Length := Length + High (anArgs) - Low (anArgs) +1;
if ( Length > 0 ) then
begin
Result := AllocMem ( Length * SizeOf (TVarRec) );
Index := 0;
for I := Low (anArgs) to High (anArgs) do
begin
Result^ [Index] := anArgs [i];
Inc (Index);
end;
end;
except
Result := NIL;
end;
end;Последний раз редактировалось mirt steelwater, 16.11.2010 в 13:55. |
|
#2
|
|||
|
|||
|
теперь можно написать что-то вроде:
Код:
function f1 (anArgs: array of const) : String;
var
I : Integer;
begin
Result := '';
for I := Low (anArgs) to High (anArgs) do
Result := Format ('%s:%s',[ Result, ParamToStr (anArgs [i]) ]);
end;
function f2 (anArgs: array of const) : String;
begin
Result := f1 ( ArrayOfConstMerge (anArgs,['test','testtest','yo-ho','hi-hi','laaa']) );
end;
function arrays (anArgs: array of const) : String;
var
I : Integer;
P : PArrayOfConst;
begin
Result := '';
for I := Low (anArgs) to High (anArgs) do
begin
P := ParamToPointer (anArgs [i]);
if Assigned (P) then
Result := Format ('%s;%s',[ Result, f1 ( PointerToArrayOfConst (P) ) ])
else
Result := Format ('%s;%s',[ Result, ParamToStr (anArgs [i]) ]);
end;
end;Код:
s := arrays ( [ 'a', ArrayOfConstToPointer (['hello','world','test1']), 1, ArrayOfConstToPointer (['test2','test3']), ArrayOfConstToPointer ([]) ] ); должно получиться: ;a;:hello:world:test1;1;:test2:test3;0 проблема в определении размера массива по указателю на него: Код:
function PointerToArrayOfConst (anArgs: PArrayOfConst) : ArrayOfConst;
var
ArrayOfConstCallAlias : procedure (var Result: ArrayOfConst; var anArgs: TVarRec; High: WORD);
I : WORD;
A : ArrayOfConst;
procedure ArrayOfConstCall (var Result: ArrayOfConst; anArgs: array of const);
var
I : WORD;
Index : WORD;
Length : WORD;
begin
Result := NIL;
try
Length := 0;
if ( High (anArgs) >= 0 ) then
Length := Length + High (anArgs) - Low (anArgs) +1;
if ( Length > 0 ) then
begin
SetLength (Result,Length);
Index := 0;
for I := Low (anArgs) to High (anArgs) do
begin
Result [Index] := anArgs [i];
Inc (Index);
end;
end;
except
Result := NIL;
end;
end;
begin
Result := NIL;
try
@ArrayOfConstCallAlias := @ArrayOfConstCall;
I := 0;
while ( I < H ) do
begin
ArrayOfConstCallAlias ( A, anArgs^ [i], SizeOf (anArgs) div SizeOf (TVarRec) );
if ( I = 0 ) then
Result := A
else
Result := ArrayOfConstMerge (Result,A);
Inc (I);
end;
except
Result := NIL;
end;
end;как получить H? |
|
#3
|
|||
|
|||
|
все молчат...
|