This commit is contained in:
Mikhalkovich Stanislav 2019-04-28 22:02:53 +03:00
parent 824e3fe357
commit ecfa6a3027
4 changed files with 96 additions and 0 deletions

View file

@ -0,0 +1,6 @@
type AAA = record s: string[4] end;
begin
var f := OpenFile&<AAA>('a.dat');
//f.Read;
end.

View file

@ -0,0 +1,24 @@
type
t1 = class
static a: integer := 1;//не обязательно статичное поле
procedure p1;
begin
var a := 2;
var p: procedure := ()->begin
Assert(a=2);
end;
p;
end;
end;
begin
(new t1).p1;
end.

View file

@ -0,0 +1,24 @@
type
t1 = class
b: byte;
static procedure p1(b: byte);
begin
var p: procedure;
p := ()->
begin
p := ()->
begin
b := b;
end;
end;
end;
end;
begin
Assert(1=1);
end.

View file

@ -5,6 +5,25 @@ unit PABCExtensions;
uses PABCSystem;
function GetCurrentLocale: string;
begin
var locale: object;
if __CONFIG__.TryGetValue('locale', locale) then
Result := locale as string
else
Result := 'ru';
end;
function GetTranslation(message: string): string;
begin
var cur_locale := GetCurrentLocale();
var arr := message.Split(new string[1]('!!'), System.StringSplitOptions.None);
if (cur_locale = 'en') and (arr.Length > 1) then
Result := arr[1]
else
Result := arr[0]
end;
//{{{doc: Начало секции подпрограмм для типизированных файлов для документации }}}
// -----------------------------------------------------
@ -35,15 +54,34 @@ begin
PABCSystem.Rewrite(Result, fname, en);
end;
function ContainsReferenceTypes(t: System.Type): boolean;
begin
if t.IsPrimitive then
Result := False
else if t.IsValueType then
begin
var fa := t.GetFields(System.Reflection.BindingFlags.GetField or System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Public or System.Reflection.BindingFlags.NonPublic);
Result := fa.Any(x->ContainsReferenceTypes(x.FieldType));
end
else Result := True;
end;
const
BAD_TYPE_IN_TYPED_FILE = 'Для типизированных файлов нельзя указывать тип элементов, являющийся ссылочным или содержащий ссылочные поля!!Typed file cannot contain elements that are references or contains fields-references';
/// Открывает типизированный файл и возвращает значение для инициализации файловой переменной
function OpenFile<T>(fname: string): file of T;
begin
if ContainsReferenceTypes(typeof(T)) then
raise new System.SystemException(GetTranslation(BAD_TYPE_IN_TYPED_FILE));
PABCSystem.Reset(Result, fname);
end;
/// Создаёт или обнуляет типизированный файл и возвращает значение для инициализации файловой переменной
function CreateFile<T>(fname: string): file of T;
begin
if ContainsReferenceTypes(typeof(T)) then
raise new System.SystemException(GetTranslation(BAD_TYPE_IN_TYPED_FILE));
var res: file of T;
PABCSystem.Rewrite(res, fname);
Result := res;
@ -52,12 +90,16 @@ end;
/// Открывает типизированный файл в заданной кодировке и возвращает значение для инициализации файловой переменной
function OpenFile<T>(fname: string; en: Encoding): file of T;
begin
if ContainsReferenceTypes(typeof(T)) then
raise new System.SystemException(GetTranslation(BAD_TYPE_IN_TYPED_FILE));
PABCSystem.Reset(Result, fname, en);
end;
/// Создаёт или обнуляет типизированный файл в заданной кодировке и возвращает значение для инициализации файловой переменной
function CreateFile<T>(fname: string; en: Encoding): file of T;
begin
if ContainsReferenceTypes(typeof(T)) then
raise new System.SystemException(GetTranslation(BAD_TYPE_IN_TYPED_FILE));
var res: file of T;
PABCSystem.Rewrite(res, fname, en);
Result := res;