MS16-051
MS16-052
MS16-053
MS16-054
MS16-055
MS16-056
MS16-057
MS16-058
MS16-059
MS16-060
MS16-061
MS16-062
MS16-064
MS16-065
MS16-066
MS16-067
とりあえず Embarcadero RAD Studio/Delphi/C++Builder 関係のあれやこれや。 Prism.jsによるコードのハイライトのテスト中。
function IsWindowsServer: Boolean;
var
OSVI: TOSVersionInfoEx;
ConditionMask: UInt64;
begin
FillChar(OSVI,SizeOf(TOSVersionInfoEX),0);
OSVI.dwOSVersionInfoSize := SizeOf(OSVI);
OSVI.wProductType := VER_NT_WORKSTATION;
ConditionMask := VerSetConditionMask(0,VER_PRODUCT_TYPE,VER_EQUAL);
Result := not (VerifyVersionInfo(OSVI,VER_PRODUCT_TYPE,ConditionMask));
end;
OSVERSIONINFOEX構造体を初期化後、wProductTypeにVER_NT_WORKSTATIONを格納し、これに対応する条件マスク(VER_PRODUCT_TYPEがVER_EQUAL)をVerifyVersionInfoで作成してVerifyVersionInfoで問い合わせて、結果がFalse(wProductTypeがVER_NT_WORKSTATION以外)ならサーバ版Windows、という判定です。function PopulationCount(Value: UInt8): Integer;
begin
Value := (Value and $55) + ((Value shr 1) and $55);
Value := (Value and $33) + ((Value shr 2) and $33);
Value := (Value and $0F) + ((Value shr 4) and $0F);
Result := Value;
end;
function PopulationCount(Value: UInt16): Integer;
begin
Value := (Value and $5555) + ((Value shr 1) and $5555);
Value := (Value and $3333) + ((Value shr 2) and $3333);
Value := (Value and $0F0F) + ((Value shr 4) and $0F0F);
Value := (Value and $00FF) + ((Value shr 8) and $00FF);
Result := Value;
end;
function PopulationCount(Value: UInt32): Integer;
begin
Value := (Value and $55555555) + ((Value shr 1) and $55555555);
Value := (Value and $33333333) + ((Value shr 2) and $33333333);
Value := (Value and $0F0F0F0F) + ((Value shr 4) and $0F0F0F0F);
Value := (Value and $00FF00FF) + ((Value shr 8) and $00FF00FF);
Value := (Value and $0000FFFF) + ((Value shr 16) and $0000FFFF);
Result := Value;
end;
function PopulationCount(Value: UInt64): Integer;
begin
Value := (Value and $5555555555555555) + ((Value shr 1) and $5555555555555555);
Value := (Value and $3333333333333333) + ((Value shr 2) and $3333333333333333);
Value := (Value and $0F0F0F0F0F0F0F0F) + ((Value shr 4) and $0F0F0F0F0F0F0F0F);
Value := (Value and $00FF00FF00FF00FF) + ((Value shr 8) and $00FF00FF00FF00FF);
Value := (Value and $0000FFFF0000FFFF) + ((Value shr 16) and $0000FFFF0000FFFF);
Value := (Value and $00000000FFFFFFFF) + ((Value shr 32) and $00000000FFFFFFFF);
Result := Value;
end;
→整数の中で立っているビットの数を数える(Gist)CountLeadingZeros32(先頭の(MSBからの)連続している0の数/32bit整数)、CountLeadingZeros64(先頭の(MSBからの)連続している0の数/64bit整数)、CountTrailingZeros32(末尾のの(LSBからの)連続している0の数/32bit整数)、CountTrailingZeros64(末尾のの(LSBからの)連続している0の数/64bit整数)、CountPopulation32(1になっているビットの数/32bit整数)、CountPopulation64(1になっているビットの数/64bit整数)が追加されました。これらの関数はコンパイラによってインラインで展開されるため、System.pasなどに定義がありません。またこれらの関数は現時点ではヘルプ上に記述がありません。
type
TFoo = (Foo0 = -1,
Foo1,
Foo2,
Foo3,
Foo4,
Foo5,
FooMax = 256);
順序値が-1から256なのでこれを基底型とする集合型を定義するとtype
TFooSet = set of TFoo; // Error: E2028 Sets may have at most 256 elements
(当然ですが)エラーになります。では代替となる高度なレコード型を定義していきます。const
BYTE_BIT = 8; // Number of bits in byte
type
TFooSet = record
private
FData: array [0..((Ord(High(TFoo)) - Ord(Low(TFoo)) + 1 + (BYTE_BIT - 1)) div BYTE_BIT) - 1] of Byte;
public
procedure Empty;
function IsEmpty: Boolean;
end;
procedure TFooSet.Empty;
begin
FillChar(FData[0],SizeOf(FData),0);
end;
function TFooSet.IsEmpty: Boolean;
var
Offset: Integer;
begin
Result := False;
for Offset := Low(FData) to High(FData) do
begin
if FData[Offset] <> 0 then
begin
Result := True;
Exit;
end;
end;
end;
FDataは基底型に対応したビット列を実際に格納する領域で、Emptyはその領域を0クリアするメソッド、IsEmptyはビット列が全て0となっているかどうかをチェックするメソッドです。type
TFooSet = record
private
...
class procedure CalcOffsets(Value: TFoo; var AOffset: Integer; var ABit: Byte); static;
public
...
procedure Include(Value: TFoo); overload;
procedure Include(const Values: array of TFoo); overload;
procedure Exclude(Value: TFoo); overload;
procedure Exclude(const Values: array of TFoo); overload;
end;
procedure TFooSet.Include(Value: TFoo);
var
Offset: Integer;
Bit: Byte;
begin
CalcOffsets(Value,Offset,Bit);
FData[Offset] := FData[Offset] or Bit;
end;
procedure TFooSet.Include(const Values: array of TFoo);
var
Value: TFoo;
begin
if Length(Values) > 0 then
begin
for Value in Values do
begin
Include(Value);
end;
end;
end;
procedure TFooSet.Exclude(Value: TFoo);
var
Offset: Integer;
Bit: Byte;
begin
CalcOffsets(Value,Offset,Bit);
FData[Offset] := FData[Offset] and (not Bit);
end;
procedure TFooSet.Exclude(const Values: array of TFoo);
var
Value: TFoo;
begin
for Value in Values do
begin
Exclude(Value);
end;
end;
class procedure TFooSet.CalcOffsets(Value: TFoo; var AOffset: Integer; var ABit: Byte);
var
RelPos: Integer;
begin
RelPos := Ord(Value) - Ord(Low(TFoo));
AOffset := RelPos div BYTE_BIT;
ABit := 1 shl (RelPos mod BYTE_BIT);
end;
CalcOffsetsは基底型の要素がどのビット位置に割り当てられているのかを計算します。type
TFooSet = record
...
public
...
function &In(Value: TFoo): Boolean;
end;
function TFooSet.&In(Value: TFoo): Boolean;
var
Offset: Integer;
Bit: Byte;
begin
CalcOffsets(Value,Offset,Bit);
Result := (FData[Offset] and Bit) <> 0;
end;
type
TFooSet = record
...
public
...
function ToString(ZeroSuppression: Boolean = True): String;
class function Parse(const Value: String): TFooSet; static;
end;
function TFooSet.ToString(ZeroSuppression: Boolean): String;
var
Index: Integer;
Len: Integer;
Offset: Integer;
begin
Result := '';
for Offset := Low(FData) to High(FData) do
begin
Result := IntToHex(FData[Offset],2) + Result;
end;
if ZeroSuppression = True then
begin
Index := 1;
Len := Length(Result);
while (Index < Len) and (Result[Index] = '0') do
begin
Index := Index + 1;
end;
if Index > 1 then
begin
Delete(Result,1,Index - 1);
end;
end;
end;
class function TFooSet.Parse(const Value: String): TFooSet;
var
Offset: Integer;
S: String;
begin
Result.Clear;
S := StringOfChar('0',(SizeOf(Result.FData) * 2) - Length(Value)) + Value;
for Offset := Low(Result.FData) to High(Result.FData) do
begin
Result.FData[Offset] := StrToInt('$' + Copy(S,(SizeOf(Result.FData) * 2) - (Offset * 2) - 1,2));
end;
end;
type
TFooSet = record
...
public
...
{$IF CompilerVersion>=24.00}
class operator Implicit(const Values: array of TFoo): TFooSet;
{$IFEND}
class operator Add(const lvalue: TFooSet; const rvalue: TFooSet): TFooSet; overload;
{$IF CompilerVersion>=28.00}
class operator Add(const lvalue: TFooSet; const rvalue: array of TFoo): TFooSet; overload;
{$IFEND}
class operator Subtract(const lvalue: TFooSet; const rvalue: TFooSet): TFooSet; overload;
{$IF CompilerVersion>=28.00}
class operator Subtract(const lvalue: TFooSet; const rvalue: array of TFoo): TFooSet; overload;
{$IFEND}
class operator Multiply(const lvalue: TFooSet; const rvalue: TFooSet): TFooSet; overload;
{$IF CompilerVersion>=28.00}
class operator Multiply(const lvalue: TFooSet; const rvalue: array of TFoo): TFooSet; overload;
{$IFEND}
end;
{$IF CompilerVersion>=24.00}
class operator TFooSet.Implicit(const Values: array of TFoo): TFooSet;
begin
Result.Clear;
Result.Include(Values);
end;
{$IFEND}
class operator TFooSet.Add(const lvalue: TFooSet; const rvalue: TFooSet): TFooSet;
var
Offset: Integer;
begin
Result.Clear;
for Offset := Low(Result.FData) to High(Result.FData) do
begin
Result.FData[Offset] := lvalue.FData[Offset] or rvalue.FData[Offset];
end;
end;
{$IF CompilerVersion>=28.00}
class operator TFooSet.Add(const lvalue: TFooSet; const rvalue: array of TFoo): TFooSet;
var
Value: TFoo;
begin
Result := lvalue;
for Value in rvalue do
begin
Result.Include(Value);
end;
end;
{$IFEND}
class operator TFooSet.Subtract(const lvalue: TFooSet; const rvalue: TFooSet): TFooSet;
var
Offset: Integer;
begin
Result.Clear;
for Offset := Low(Result.FData) to High(Result.FData) do
begin
Result.FData[Offset] := lvalue.FData[Offset] and (not rvalue.FData[Offset]);
end;
end;
{$IF CompilerVersion>=28.00}
class operator TFooSet.Subtract(const lvalue: TFooSet; const rvalue: array of TFoo): TFooSet;
var
Value: TFoo;
begin
Result := lvalue;
for Value in rvalue do
begin
Result.Exclude(Value);
end;
end;
{$IFEND}
class operator TFooSet.Multiply(const lvalue: TFooSet; const rvalue: TFooSet): TFooSet;
var
Offset: Integer;
begin
Result.Clear;
for Offset := Low(Result.FData) to High(Result.FData) do
begin
Result.FData[Offset] := lvalue.FData[Offset] and rvalue.FData[Offset];
end;
end;
{$IF CompilerVersion>=28.00}
class operator TFooSet.Multiply(const lvalue: TFooSet; const rvalue: array of TFoo): TFooSet;
begin
Result := lvalue * TFooSet(rvalue);
end;
{$IFEND}
ここで明示的な型キャストを行うExplicitを用意しなかったのは、"TFooSet([Foo0,Foo2])"の"[Foo0,Foo2]"の部分がarray of TFooではなくset of TFooと解釈されてしまいコンパイルエラー(E1012)となる問題を解決できなかったためです。また同様の理由からImplicitはXE3以降、Add/Subtract/Multiplyの2番目のoverload(右項がarray of TFoo)はXE7以降のみ有効です。type
TFooSet = record
...
public
...
class operator LessThanOrEqual(const lvalue: TFooSet; const rvalue: TFooSet): Boolean; overload;
class operator GreaterThanOrEqual(const lvalue: TFooSet; const rvalue: TFooSet): Boolean; overload;
class operator Equal(const lvalue: TFooSet; const rvalue: TFooSet): Boolean; overload;
class operator NotEqual(const lvalue: TFooSet; const rvalue: TFooSet): Boolean; overload;
class operator In(lvalue: TFoo; const rvalue: TFooSet): Boolean;
end;
class operator TFooSet.LessThanOrEqual(const lvalue: TFooSet; const rvalue: TFooSet): Boolean;
var
Offset: Integer;
begin
for Offset := Low(lvalue.FData) to High(lvalue.FData) do
begin
if (lvalue.FData[Offset] and rvalue.FData[Offset]) <> lvalue.FData[Offset] then
begin
Result := False;
Exit;
end;
end;
Result := True;
end;
class operator TFooSet.GreaterThanOrEqual(const lvalue: TFooSet; const rvalue: TFooSet): Boolean;
var
Offset: Integer;
begin
for Offset := Low(lvalue.FData) to High(lvalue.FData) do
begin
if (lvalue.FData[Offset] and rvalue.FData[Offset]) <> rvalue.FData[Offset] then
begin
Result := False;
Exit;
end;
end;
Result := True;
end;
class operator TFooSet.Equal(const lvalue: TFooSet; const rvalue: TFooSet): Boolean;
var
Offset: Integer;
begin
for Offset := Low(lvalue.FData) to High(lvalue.FData) do
begin
if (lvalue.FData[Offset] xor rvalue.FData[Offset]) <> 0 then
begin
Result := False;
Exit;
end;
end;
Result := True;
end;
class operator TFooSet.NotEqual(const lvalue: TFooSet; const rvalue: TFooSet): Boolean;
begin
Result := not (lvalue = rvalue);
end;
class operator TFooSet.In(lvalue: TFoo; const rvalue: TFooSet): Boolean;
begin
Result := rvalue.&In(lvalue);
end;
ここでclass operator LessThanOrEqual/GreaterThanOrEqual/Equal/NotEqualにAdd/Subtract/Multiplyのようにarray of TFooを第2パラメータ(右項)とするものを用意しなかったのもExplicitと同様の理由です。type
PFooSet = ^TFooSet;
TFooSet = record
private
type
TEnumerator = class(TObject)
private
FContainer: PFooSet;
FIndex: Integer;
function GetCurrent: TFoo;
public
constructor Create(Container: PFooSet);
function MoveNext: Boolean;
property Current: TFoo
read GetCurrent;
end;
public
...
function GetEnumerator: TEnumerator;
end;
function TFooSet.GetEnumerator: TEnumerator;
begin
Result := TEnumerator.Create(@Self);
end;
constructor TFooSet.TEnumerator.Create(Container: PFooSet);
begin
inherited Create;
FIndex := Ord(Low(TFoo)) - 1;
FContainer := Container;
end;
function TFooSet.TEnumerator.MoveNext: Boolean;
begin
while (FIndex < Ord(High(TFoo))) do
begin
FIndex := FIndex + 1;
if TFoo(FIndex) in FContainer^ then
begin
Result := True;
Exit;
end;
end;
Result := False;
end;
function TFooSet.TEnumerator.GetCurrent: TFoo;
begin
Result := TFoo(FIndex);
end;
これでTFooSetに対して通常の集合型のように"+"、"-"、"*"のような演算や"="、"<>"のような比較、for..inによる要素の取り出しを行うことができるようになりました。
同様にすることで部分範囲型で値が0..255に収まらないようなものを基底型とするような集合型も実装することができます。
→要素数が256を超える列挙型の集合型(Gist)
→値範囲が0..255に収まらない部分範囲型の集合型(Gist)
type
TNotifyNumber = procedure (Sender: TObject; Number: Integer) of object;
ISubject = interface
procedure Add(AEvent: TNotifyNumber);
procedure Remove(AEvent: TNotifyNumber);
end;
TNotifyNumberが通知のためのイベントハンドラの型、ISubjectがそのイベントハンドラを登録/削除する機能を持たせたインタフェースです。メインフォーム、サブフォームの両方で使用するので別ユニットで定義しておきます。次はメインフォームです。uses
Spring, ...
type
TForm1 = class(TForm,ISubject)
...
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
FEvent: Event<TNotifyNumber>
public
procedure Add(AEvent: TNotifyNumber);
procedure Remove(AEvent: TNotifyNumber);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FEvent := Event<TNotifyNumber>.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FEvent.Clear;
end;
procedure TForm1.Add(AEvent: TNotifyNumber);
begin
FEvent.Add(AEvent);
end;
procedure TForm1.Remove(AEvent: TNotifyNumber);
begin
FEvent.Remove(AEvent);
end;
メインフォームはISubjectインタフェースを継承するようにして、AddとRemoveを実装します。Event<T>はレコード型ですが、内部の適切な初期化のためにclass function Createを呼び出しておく必要があります。AddとRemoveでは単にEvent<T>のAddとRemoveを呼び出すだけです。一方サブフォームではtype
TForm2 = class(TForm)
...
private
FSubject: ISubject;
public
property Subject: ISubject
read FSubject
write FSubject;
end;
メインフォームをISubjectとして保持するためのプロパティを用意します。これでメインフォームからサブフォームを生成、表示する処理はprocedure TForm1.Button1Click(Sender: TObject);
begin
with TForm2.Create(Self) do
begin
Subject := Self;
Show;
end;
end;
となります。サブフォーム側ではボタンクリックで任意にマルチキャストイベントを登録、削除できるようにしてみます、type
TForm2 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
...
procedure DoNotifyNumber(Sender: TObject; Number: Integer);
public
...
end;
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Subject.Remove(DoNotifyNumber);
Action := caFree;
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
Subject.Add(DoNotifyNumber);
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
Subject.Remove(DoNotifyNumber);
end;
procedure TForm2.DoNotifyNumber(Sender: TObject; Number: Integer);
begin
Label1.Caption := 'Notified number is ' + IntToStr(Number);
end;
procedure DoNotifyNumberがマルチキャストイベントのイベントハンドラで、これをButton1/Button2のクリックイベントでSubjectにAdd/Removeすることで登録、削除します。最後にメインフォーム側からのマルチキャストイベントの呼び出しです。type
TForm1 = class(TForm,ISubject)
...
Label1: TLabel;
Button2: TButton;
procedure Button2Click(Sender: TObject);
private
FCount: Integer;
...
public
...
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
FCount := FCount + 1;
Label1.Caption := IntToSTr(FCount);
FEvent.Invoke(Self,FCount);
end;
Selfと1ずつ増える整数値(FCount)でEvent<T>のInvokeメソッドを呼び出すだけです(正確にはInvokeはイベントハンドラ型(<T> = <TNotifyNumber>)のプロパティで、Invoke(...)とすることでメソッドの呼び出しになる)。サーバメソッドでサポートされているデータ型
クラスインスタンスのマーシャリング/アンマーシャリング
var
Values: TJSONArray;
Item: TFoo;
List: TObjectList<TFoo>;
begin
...
for Item in List do
begin
Values.AddElement(Marshal.Marshal(Item));
end;
のようにそれぞれの要素をマーシャリングしたものをTJSONArrayにAddElementメソッドで追加していって、そのTJSONArrayを受け渡して、
var
Values: TJSONArray;
Item: TJSONValue;
List: TObjectList<TFoo>;
begin
...
for Item in Values do
begin
List.Add(UnMarshal.Unmarshal(Item) as TFoo);
end;
TJSONArrayのItems[]プロパティ(またはenumerator)で取り出したJSONValueをアンマーシャリングする、という方法が考えられます。TJSONValueとその派生クラスについて
サーバクラスのインスタンスのライフサイクルについて
セッションの管理について
TDSSessionManager.Instance.ForEachSession(
procedure (const Session: TDSSession)
begin
//
end);
とします。TDSSessionManager.Instance.AddSessionEvent(
procedure(Sender: TObject;
const EventType: TDSSessionEventType;
const Session: TDSSession)
begin
case EventType of
SessionCreate:
begin
// Session is created
end;
SessionClose:
begin
// Session is closed
end;
end
end);
重量コールバック(クライアントコールバック)の管理
TDSCallbackTunnelManager.Instance.AddTunnelEvent(
procedure(Sender: TObject; const EventItem: TDSCallbackTunnelEventItem)
begin
case EventItem.EventType of
TunnelCreate:
begin
// Tunnel is created
end;
TunnelClose:
begin
// Tunnel is closed
end;
TunnelClosedByServer:
begin
// Tunnel is closed by user
end;
CallbackAdded:
begin
// Callback is added
end;
CallbackRemoved:
begin
// Callback is removed
end;
end;
end);
TCP/IP接続を監視