Delphi. Lambda. Anonymous methods

Example

program LambdaExamples;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

type
  TSayHello = reference to function(aName: string): string;

  TFuncOfIntToString = reference to function(x: Integer): string;

  TFuncOfString = reference to function(a, b: string): string;

  TMethodReference = reference to procedure(aArg: string);

  TSomeClass = class
    procedure Foo(aArg: string);
  end;

var
  myFunc: TFuncOfIntToString;
  concater: TFuncOfString;
  someClassInstance: TSomeClass;
  m: TMethodReference;
  sayHello: TSayHello;


// lets say we have following functions
procedure AnalyzeProcedure(func: TFuncOfIntToString);
begin
  // use argument
  WriteLn(func(12));
end;

function MakeConcat(y: Integer): TFuncOfString;
begin
  Result :=
    function(a, b: string): string
    begin
      Result := Format('%s %s %s', [a, b, y.ToString()]);
    end;
end;

{ TSomeClass }

procedure TSomeClass.Foo(aArg: string);
begin
  Writeln(aArg);
end;

begin

   // 1. lets say we in context of some class and we need to do
   // smth fast without overhead of instantiate new class
   // we can do it like this
  sayHello :=
    function(aName: string): string
    begin
      Result := Format('hello %s', [aName]);
    end;
  WriteLn(sayHello('Stan'));

  //2.  use lambda directly in another function
  AnalyzeProcedure(
    function(x: Integer): string
    begin
      Result := IntToStr(x);
    end);

  //3.  put lambda to variable
  myFunc :=
    function(x: Integer): string
    begin
      Result := IntToStr(x);
    end;

  AnalyzeProcedure(myFunc);

  //4.  --- Closure
  concater := MakeConcat(123);
  WriteLn(concater('a', 'b'));

  //5.  reference to method
  someClassInstance := TSomeClass.Create();
  try
    m := someClassInstance.Foo;
    m('this is reference to method');

    // ACHTUNG !!! DONT' DO LIKE THIS
    // someClassInstance.Foo := m;  << DANGER OF MEMORY LEAKS
  finally
    someClassInstance.Free();
  end;

  ReadLn;
end.
Posted in Без рубрики | Leave a comment

Delphi. Jcl. HashMaps

Example

program JediHashMaps;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  JclContainerIntf,
  JclHashMaps;

var
  h: IJclIntegerIntegerMap;

const
  key = 1;
  value = 123;

begin
  h := TJclIntegerIntegerHashMap.Create(10);
  h.PutValue(key, value);

  if h.ContainsKey(key) then
    Writeln(Format('contains key %s with value %s', [key.ToString(), h.Items[key].ToString()]));

  ReadLn;
end.
Posted in Без рубрики | Leave a comment

Icons. Collections

icons8

Posted in Без рубрики | Comments Off on Icons. Collections

DesignPatterns. Iterator

Простой пример итератора для коллекции

program IteratorPattern;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  uIterator in 'uIterator.pas';

var
  c: TCollection;
  i: TCustomIterator;

begin
  c := TCollection.Create();
  try
    c.Add(TStrObjForTest.Create('obj 1'));
    c.Add(TStrObjForTest.Create('obj 2'));
    c.Add(TStrObjForTest.Create('obj 3'));
    // iteration
    i := TCustomIterator.Create(c);
    try
      Writeln(TStrObjForTest(i.First).Value);
      while not i.IsDone do
        Writeln(TStrObjForTest(i.Next).Value);
      Readln;
    finally
      i.Free();
    end;
  finally
    c.Free();
  end;
end.
Continue reading
Posted in Без рубрики | Comments Off on DesignPatterns. Iterator

Delphi. UniGUI. Как отключить меню с сортировкой в гриде?

UniDBTreeGrid -> Columns -> Column[xxx] -> Menu -> MenuEnabled = False

Спасибо Delphi Developer с форума UniGUI!

Posted in Без рубрики | Comments Off on Delphi. UniGUI. Как отключить меню с сортировкой в гриде?

Delphi. Соответствие типов С и Delphi

C Type    | Pascal    | Description  
---------------------------------------------- 
LPSTR       PAnsiChar;  String >pointer  
LPCSTR      PAnsiChar;  String >pointer  
DWORD       Integer;    Whole numbers  
BOOL        LongBool;   Boolean values  
PBOOL       ^BOOL;      Pointer to a Boolean value  
Pbyte       ^Byte;      Pointer to a byte value  
PINT        ^Integer;   Pointer to an integer value  
Psingle     ^Single;    Pointer to a single (floating point) value  
PWORD       ^Word;      Pointer to a 16-bit value  
PDWORD      ^DWORD;     Pointer to a 32-bit value  
LPDWORD     PDWORD;     Pointer to a 32-bit value  
UCHAR       Byte;       8-bit values (can represent characters)  
PUCHAR      ^Byte;      Pointer to 8-bit values  
SHORT       Smallint;   16-bit whole numbers  
UINT        Integer;    32-bit whole numbers. Traditionally,  
                        this was used to represent unsigned integers,  
                        but Object Pascal does not have a true  
                        unsigned integer data type.  
PUINT       ^UINT;      Pointer to 32-bit whole numbers  
ULONG       Longint;    32-bit whole numbers. Traditionally,  
                        this was used to represent unsigned integers,  
                        but Object Pascal does not have a true  
                        unsigned integer data type.  
PULONG      ^ULONG;     Pointer to 32-bit whole numbers  
PLongint    ^Longint;   Pointer to 32-bit values  
PInteger    ^Integer;   Pointer to 32-bit values  
PSmallInt   ^Smallint;  Pointer to 16-bit values  
PDouble     ^Double;    Pointer to double (floating point) values  
LCID        DWORD;      A local identifier  
LANGID      Word;       A language identifier  
THandle     Integer;    An object handle. Many Windows API functions return a value  
                        of type THandle, which identobject ifies that object within  
                        Windows'internal object tracking tables.  
PHandle     ^THandle;   A pointer to a handle  
WPARAM      Longint;    A 32-bit message parameter. Under earlier versions of Windows,  
                        this was a 16-bit data type.  
LPARAM      Longint;    A 32-bit message parameter  
LRESULT     Longint;    A 32-bit function return value  
HWND        Integer;    A handle to a window. All windowed controls, child windows,  
                        main windows, etc., have a corresponding window handle that  
                        identifies them within Windows'internal tracking tables.  
HHOOK       Integer;    A handle to an installed Windows system hook  
ATOM        Word;       An index into the local or global atom table for a string  
HGLOBAL     THandle;    A handle identifying a globally allocated dynamic memory object.  
                        Under 32-bit Windows, there is no distinction between globally  
                        and locally allocated memory.  
HLOCAL      THandle;    A handle identifying a locally allocated dynamic memory object.  
                        Under 32-bit Windows, there is no distinction between globally  
                        and locally allocated memory.  
FARPROC     Pointer;    A pointer to a procedure, usually used as a parameter type in  
                        functions that require a callback function  
HGDIOBJ     Integer;    A handle to a GDI object. Pens, device contexts, brushes, etc.,  
                        all have a handle of this type that identifies them within  
                        Windows'internal tracking tables.  
HBITMAP     Integer;    A handle to a Windows bitmap object  
HBRUSH      Integer;    A handle to a Windows brush object  
HDC         Integer;    A handle to a device context  
HENHMETAFILE  Integer;  A handle to a Windows enhanced metafile object  
HFONT       Integer;    A handle to a Windows logical font object  
HICON       Integer;    A handle to a Windows icon object  
HMENU       Integer;    A handle to a Windows menu object  
HMETAFILE   Integer;    A handle to a Windows metafile object  
HINST       Integer;    A handle to an instance object  
HMODULE     HINST;      A handle to a module  
HPALETTE    Integer;    A handle to a Windows color palette  
HPEN        Integer;    A handle to a Windows pen object  
HRGN        Integer;    A handle to a Windows region object  
HRSRC       Integer;    A handle to a Windows resource object  
HKL         Integer;    A handle to a keyboard layout  
HFILE       Integer;    A handle to an open file  
HCURSOR     HICON;      A handle to a Windows mouse cursor object  
COLORREF    DWORD;      A Windows color reference value, containing values  
                        for the red, green, and of ;bsp;blue components of a color 

Источник

Posted in Без рубрики | Comments Off on Delphi. Соответствие типов С и Delphi

Delphi. UniGUI. PopupMenu

Подключим всплывающее меню. Выглядеть это может так

Само подключение всплывающего меню происходит в коде, в событии OnCellContextClick

procedure TMainForm.UniDBGrid1CellContextClick(Column: TUniDBGridColumn; X, Y: Integer);
begin
  if ClientDataSet.State in dsEditModes then
    UniPopupMenu2.Popup(X, Y, UniDBGrid1)
  else
    UniPopupMenu1.Popup(X, Y, UniDBGrid1);
end;

Само Popup меню один в один с традиционным дельфийским аналогом. Нам просто нужно разместить его на форме и настроить в дизайнере.

Ну и обработка некоторых событий наших меню

TMainForm.Cancel1Click(Sender: TObject);
begin
  if ClientDataSet.State in dsEditModes then
    ClientDataSet.Cancel;
end;

procedure TMainForm.DeleteRowClick(Sender: TObject);
begin
  ClientDataSet.Delete;
end;

procedure TMainForm.EditClick(Sender: TObject);
begin
  ClientDataSet.Edit;
end;

procedure TMainForm.InsertRowClick(Sender: TObject);
begin
  ClientDataSet.Insert;
end;

procedure TMainForm.PostClick(Sender: TObject);
begin
  if ClientDataSet.State in dsEditModes then
    ClientDataSet.Post;
end;
Posted in Без рубрики | Comments Off on Delphi. UniGUI. PopupMenu

DesignPatterns. Mediator

Объекты связаны друг с другом через посредника. Так называемая слабая связанность.

program MediatorPattern;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  uMediator in 'uMediator.pas';

var
  Stas: TColleague;
  Masha: TColleague;
  chat: TMediator;

begin
  chat := TMediator.Create();
  chat.AddParticipant(TColleague.Create(1, 'Stas'));
  chat.AddParticipant(TColleague.Create(2, 'Masha'));
  Stas := chat.Colleague[0];
  Masha := chat.Colleague[1];
  try
    Stas.Send('Привет, Маша, пойдем гулять?', Masha);
    Masha.Send('Привет, Стас, конечно!', Stas);
    ReadLn;
  finally
    chat.Free();
  end;
end.
Continue reading
Posted in Без рубрики | Comments Off on DesignPatterns. Mediator

DesignPatterns. ChainOfResponsibilities

Есть цепочка объектов. Одно звено выполняет одни операции, другое – другие.

program ChainsOfResponsibilities;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  uChainsOfResponsibilities in 'uChainsOfResponsibilities.pas';

var
  d: TDirector;
  m: TManager;
  w: TWorker;
  p: TPurchase;

begin
  d := TDirector.Create();
  m := TManager.Create();
  w := TWorker.Create();
  p := TPurchase.Create(5);
  try
    w.SetNextApprover(m);
    m.SetNextApprover(d);
    w.ProcessRequest(p); // trying to buy
    Readln;
  finally
    d.Free();
    m.Free();
    w.Free();
    p.Free();
  end;
end.
Continue reading
Posted in Без рубрики | Comments Off on DesignPatterns. ChainOfResponsibilities

DesignPatterns. Proxy

Для распределения нагрузки. Есть интерфейс, который поддерживает основной класс и его заместитель. Легкие операции может выполнять заместитель, а тяжелые – основной класс.

program Proxy;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  uProxy in 'uProxy.pas';

var
  server: TServer;
  si: IServerInterface;

begin
  server := TServer.Create();
  si := TProxy.Create(server);
  try
    si.LightOperation();
    si.HeavyOperation();
    Readln;
  finally
    server.Free();
  end;
end.
Continue reading
Posted in Без рубрики | Comments Off on DesignPatterns. Proxy