Тема: arra of const
Показать сообщение отдельно
  #1  
Старый 16.11.2010, 13:19
mirt steelwater mirt steelwater вне форума
Прохожий
 
Регистрация: 17.10.2010
Сообщения: 35
Репутация: 10
Вопрос arra of const

ок. хочу получить возможность работать со вложенными открытыми массивами, вроде такого:
[ '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;
Ответить с цитированием