Skip to content

Instantly share code, notes, and snippets.

View owlsperspective's full-sized avatar

Owl's perspective owlsperspective

View GitHub Profile
@owlsperspective
owlsperspective / Population counting (hamming weight)
Last active February 5, 2016 05:25
整数の中で立っているビットの数を数える
unit Unit2;
interface
function PopulationCount(Value: UInt8): Integer; overload;
function PopulationCount(Value: UInt16): Integer; overload;
function PopulationCount(Value: UInt32): Integer; overload;
function PopulationCount(Value: UInt64): Integer; overload;
@owlsperspective
owlsperspective / Implement set type based sub-range type over 256 ordinalities
Last active December 13, 2015 15:04
値範囲が0..255に収まらない部分範囲型の集合型
unit Unit3;
interface
uses
{$IF CompilerVersion >= 23.0}
System.SysUtils;
{$ELSE}
SysUtils;
{$IFEND}
@owlsperspective
owlsperspective / Implement set type based enumeration over 256 ordinalities
Last active December 13, 2015 15:04
要素数が256を超える列挙型の集合型
unit Unit2;
interface
uses
{$IF CompilerVersion >= 23.0}
System.SysUtils;
{$ELSE}
SysUtils;
{$IFEND}
@owlsperspective
owlsperspective / Implement IfThen by generics and advanced record
Last active August 29, 2015 14:25
IfThen<T>をrecordで実装する
type
&IF = record
class function &Then<T>(AValue: Boolean; AThen: T; AElse: T): T; static; inline;
end;
class function &IF.&Then<T>(AValue: Boolean; AThen: T; AElse: T): T;
begin
if AValue then
begin
Result := AThen;
@owlsperspective
owlsperspective / Enumerate serial ports with friendly name (C++)
Created January 8, 2015 06:34
シリアルポートをフレンドリ名で列挙 (C++)
#include <windows.h>
#include <setupapi.h>
#include <System.hpp>
void __fastcall GetCommPortsWithName(std::vector<std::pair<int,String> >& CommPorts)
{
GUID Guid;
DWORD RequiredSize = 0;
if (!SetupDiClassGuidsFromName(_TEXT("Ports"),&Guid,1,&RequiredSize)) {
@owlsperspective
owlsperspective / Enumerate serial ports with friendly name
Last active August 29, 2015 14:12
シリアルポートをフレンドリ名で列挙
{ Setup APIs }
type
{ HDEVINFO }
HDEVINFO = THandle;
{$EXTERNALSYM HDEVINFO}
{ SP_DEVINFO_DATA }
SP_DEVINFO_DATA = packed record
cbSize: DWORD;
ClassGuid: TGUID;
@owlsperspective
owlsperspective / Record helper for enumeration
Created August 12, 2013 10:35
列挙型と列挙子名(文字列)または整数の相互変換(ジェネリックス版)
unit Unit2;
interface
uses
TypInfo, SysUtils, SysConst;
type
TEnumHelper = record
class function TryGetEnumName<T: record>(Value: T; out S: String): Boolean; static;
@owlsperspective
owlsperspective / CreateProcess with priority class
Created August 12, 2013 09:25
優先順位クラスを指定してプロセスを起動する
// Delphi 2007 and XE3
// Create form 'TForm1', place 1 Edit, 1 ComboBox, 1 Button.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
@owlsperspective
owlsperspective / GetPriorityClass and SetPriorityClass
Created August 12, 2013 09:21
GetPriorityClassとSetPriorityClassで優先順位クラスを取得/設定する
// Delphi 2007 and XE3
// Create form 'TForm1', place 2 ComboBox, 4 Button.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
@owlsperspective
owlsperspective / CopyFileEx and callback
Created August 9, 2013 06:38
Win32APIのCopyFileExのコールバックを受け入れる
// Delphi 2007 and XE3
// Create form 'TForm1', place 2 Edit, 2 Button and 1 Label.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;