Streaming components/ja: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Streaming components/ja
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## はじめに | |
| 2 | + | ||
| 3 | + | 通常、ハードディスクやネットワークストリームにデータを蓄えておく場合は、それらの特性に応じてロードやセーブ(読み込みや書き込み)のためのコードを書かなければなりません。このチュートリアルでは、余分な load/save コードを書くことなく、RTTI を使用して、ストリームから読み込みや書き込みを行うクラスの書き方について述べます。 | |
| 4 | + | ||
| 5 | + | lazarus コードでのサンプルや TCheckBox を含んだ TGroupBox をストリームに保存したり、両方のコンポーネントのコピーを作成してストリームから戻すといったデモを説明します。 | |
| 6 | + | ||
| 7 | + | ` <lazaruspath>/examples/componentstreaming/ を参照してください。` | |
| 8 | + | ||
| 9 | + | RTTI コントロールとのコンビネーションで、GUI と ディスク/ネットワークとの間でプログラムデータをやり取りする際に必要なコードの量を最小限にすることができます。 | |
| 10 | + | ||
| 11 | + | ## TComponent / TPersistent | |
| 12 | + | ||
| 13 | + | **TPersistent** クラスは、Classes ユニット内で定義され、**{\$M+}**というコンパイラスイッチで使用されます。このスイッチは定義されたクラスに対して、 Run Time Type Information(**RTTI**)(訳注:実行時型情報)を付加するコンパイラ指令です。 つまり、TPersistentクラスと、その派生クラスは、実行時型情報をもつクラス群として機能することを意味します。'Published' のプロパティは'public'のプロパティと同様に使用することができますが、実行時型情報が付加されていることで、これらのクラスに対しては、実行時にプロパティへの動的なアクセスが可能になります。 | |
| 14 | + | ||
| 15 | + | すべてのPublished のプロパティが、読み書きできるだけでなく、実行時に、どのような名前で、どのような型になっているかをリストアップすることができます。 IDE では、ユーザーが生成したコンポーネントを、(今つくられたコンポーネントがどのようなものか、過去につくられたIDEが知る由もありませんが、しかし..)IDEがそのクラスをプロパティを通して自由に扱えるようにするための優れた機能です。 | |
| 16 | + | ||
| 17 | + | **TComponent**はTPersistentから派生し、子コンポーネントを持てるようになっています。これはストリーミングで大変重要な事項です。1つのコンポーネントが**ルートコンポーネント**になると、子コンポーネントのリストから**lookup root**によって参照されます。 | |
| 18 | + | ||
| 19 | + | (訳注:1つのクラスがストリーミング or シリアライズされる時は、メモリ上での参照問題をなんらかの形で解決する必要がある。ルートはストリーミングの基点となるクラスであり、参照解決の重要な1つである。) | |
| 20 | + | ||
| 21 | + | ## TReader / TWriter | |
| 22 | + | ||
| 23 | + | TReaderとTWriterは、ストリームにたいしてTComponentを読み書きするためのワーカークラスです。(訳注:ストリームクラスにたいして補助的に便利な関数の集まりになっています。) CreateLRSReaderとCreateLRSWriterを見てください。 これらは、特別なフォーマットを読み書きする場合**Driver**を使います。 このとき、LResourcesユニットで定義されるバイナリオブジェクトフォーマットのためのreader(TLRSObjectReader)とwriter(TLRSObjectWriter)があり、Laz_XMLStreamingで定義されるTDOMDocumentのためのwriter (TXMLObjectWriter)があります。 | |
| 24 | + | ||
| 25 | + | LResourcesユニットは、バイナリフォーマットからテキストに変換する関数とその逆の関数を含みます。 (LRSObjectBinaryToText, LRSObjectTextToBinary). DelphiではWidestringでしたが、LCLは文字列に対してはUTF8を好みます。 そのための変換関数も同様にいくつかあります。 | |
| 26 | + | ||
| 27 | + | ## 独自コンポーネントの書き方 - Part 1 | |
| 28 | + | ||
| 29 | + | カスタムコンポーネントは次のようにシンプルです: | |
| 30 | + | ||
| 31 | + | `type`\ | |
| 32 | + | ` TMyComponent = class(TComponent)`\ | |
| 33 | + | ` private`\ | |
| 34 | + | ` FID: integer;`\ | |
| 35 | + | ` published`\ | |
| 36 | + | ` property ID: integer read FID write FID;`\ | |
| 37 | + | ` end;` | |
| 38 | + | ||
| 39 | + | ## ストリームにコンポーネントを書き出す | |
| 40 | + | ||
| 41 | + | [[doc:lcl/lresources|LResources]] ユニットには次のような関数があります: | |
| 42 | + | ||
| 43 | + | ` procedure WriteComponentAsBinaryToStream(AStream: TStream; AComponent: TComponent);` | |
| 44 | + | ||
| 45 | + | これは、コンポーネントをバイナリフォーマットでストリームに書き出します。 | |
| 46 | + | ||
| 47 | + | たとえば次のようにします: | |
| 48 | + | ||
| 49 | + | procedure TForm1.Button1Click(Sender: TObject); | |
| 50 | + | var | |
| 51 | + | AStream: TMemoryStream; | |
| 52 | + | begin | |
| 53 | + | AStream:=TMemoryStream.Create; | |
| 54 | + | try | |
| 55 | + | WriteComponentAsBinaryToStream(AStream,AGroupBox); | |
| 56 | + | ... save stream somewhere ... | |
| 57 | + | finally | |
| 58 | + | AStream.Free; | |
| 59 | + | end; | |
| 60 | + | end; | |
| 61 | + | ||
| 62 | + | (訳注:この例では、メモリ上にグループボックスのすべてのプロパティがバイナリ形式で保管される。 一般的には、バッファとして使う以外には、メモリ上に入れる例は少ない。ファイルストリームをつかって、それをファイルに保存して、後日読み出したり、ソケットストリームをつかって、ソケットで別のマシンに転送したりして、オブジェクトの状態を「保存」「転送」することができる。完全に保存するためには、オブジェクトの状態がすべてプロパティでpublishされていなくてはならないことに注意しよう。 Lazarusでは、フォームのデザイン等(=つまりプロパティの設定値)を設計時(コンパイル時)にリソースに保存し、実行時にリソースからプロパティを復元して実行するメカニズムがある。 他の言語では、一般的には、シリアライズと呼ばれることがある) | |
| 63 | + | ||
| 64 | + | ## ストリームからコンポーネントを読み出す | |
| 65 | + | ||
| 66 | + | LResources には次のような関数があります: | |
| 67 | + | ||
| 68 | + | ` procedure ReadComponentFromBinaryStream(AStream: TStream;`\ | |
| 69 | + | ` var RootComponent: TComponent; OnFindComponentClass: TFindComponentClassEvent; TheOwner: TComponent = nil);` | |
| 70 | + | ||
| 71 | + | - AStream はコンポーネントをバイナリフォーマットで含んでいるストリームを指定します。 | |
| 72 | + | - RootComponent は存在しているコンポーネントを指定した場合はそのプロパティは上書きされます。nilを指定した場合は、新しいコンポーネントがストリームから生成されます。 | |
| 73 | + | - OnFindComponentClass はTReaderによって、ストリーム内のクラス名から、クラスを取得するのに使われる、イベント関数です。(訳注:クラス参照型を取得することで、TReaderは名前文字列からそのクラスのコンストラクタを呼ぶことができる) | |
| 74 | + | ||
| 75 | + | たとえば、次のように、書くことができます: | |
| 76 | + | ||
| 77 | + | procedure TCompStreamDemoForm.OnFindClass(Reader: TReader; | |
| 78 | + | const AClassName: string; var ComponentClass: TComponentClass); | |
| 79 | + | begin | |
| 80 | + | if CompareText(AClassName,'TGroupBox')=0 then | |
| 81 | + | ComponentClass:=TGroupBox | |
| 82 | + | else if CompareText(AClassName,'TCheckBox')=0 then | |
| 83 | + | ComponentClass:=TCheckBox; | |
| 84 | + | end; | |
| 85 | + | ||
| 86 | + | - コンポーネントを新規に生成した場合、Owner にはそのコンポーネントのオーナーとなります。 | |
| 87 | + | ||
| 88 | + | ## ストリーム可能なプロパティについて | |
| 89 | + | ||
| 90 | + | TReaderやTWriterがストリームできるプロパティには、いくつかの条件があります。 | |
| 91 | + | ||
| 92 | + | - 基本型はストリームできます。すなわち、integer, char, single, double, extended, byte, word, cardinal, shortint, method pointers, などなど. . | |
| 93 | + | - TPersistent とその派生クラスはストリームできます。 | |
| 94 | + | ||
| 95 | + | - 構造体、object型、TPersistentから派生しないクラス型は、ストリームできません。 | |
| 96 | + | ||
| 97 | + | それらをストリームするには、TReader/TWriterにその方法を教える必要があります。次を見てください。#Streaming custom Data - DefineProperties. | |
| 98 | + | ||
| 99 | + | ## カスタムデータをストリームする - DefineProperties | |
| 100 | + | ||
| 101 | + | DefinePropertiesをオーバーライドすることで、通常ではストリームできないデータをストリームすることができるようになります。この方法は、すべてのデータに対して、たとえ基本型をもたないものでも、ストリームすることを可能にします。たとえば、あなたが作ったコンポーネントの変数 **FMyRect: TRect**をストリームしたい場合、次のような3つのメソッドをコンポーネントに加えます。 | |
| 102 | + | ||
| 103 | + | procedure DefineProperties(Filer: TFiler); override; | |
| 104 | + | procedure ReadMyRect(Reader: TReader); | |
| 105 | + | procedure WriteMyRect(Writer: TWriter); | |
| 106 | + | ||
| 107 | + | 続けて次のように実装してください: | |
| 108 | + | ||
| 109 | + | procedure TMyComponent.DefineProperties(Filer: TFiler); | |
| 110 | + | var | |
| 111 | + | MyRectMustBeSaved: Boolean; | |
| 112 | + | begin | |
| 113 | + | inherited DefineProperties(Filer); | |
| 114 | + | MyRectMustBeSaved:=(MyRect.Left<>0) | |
| 115 | + | or (MyRect.Top<>0) | |
| 116 | + | or (MyRect.Right<>0) | |
| 117 | + | or (MyRect.Bottom<>0); | |
| 118 | + | Filer.DefineProperty('MyRect',@ReadMyRect,@WriteMyRect,MyRectMustBeSaved); | |
| 119 | + | end; | |
| 120 | + | ||
| 121 | + | procedure TMyComponent.ReadMyRect(Reader: TReader); | |
| 122 | + | begin | |
| 123 | + | with Reader do begin | |
| 124 | + | ReadListBegin; | |
| 125 | + | FMyRect.Left:=ReadInteger; | |
| 126 | + | FMyRect.Top:=ReadInteger; | |
| 127 | + | FMyRect.Right:=ReadInteger; | |
| 128 | + | FMyRect.Bottom:=ReadInteger; | |
| 129 | + | ReadListEnd; | |
| 130 | + | end; | |
| 131 | + | end; | |
| 132 | + | ||
| 133 | + | procedure TMyComponent.WriteMyRect(Writer: TWriter); | |
| 134 | + | begin | |
| 135 | + | with Writer do begin | |
| 136 | + | WriteListBegin; | |
| 137 | + | WriteInteger(FMyRect.Left); | |
| 138 | + | WriteInteger(FMyRect.Top); | |
| 139 | + | WriteInteger(FMyRect.Right); | |
| 140 | + | WriteInteger(FMyRect.Bottom); | |
| 141 | + | WriteListEnd; | |
| 142 | + | end; | |
| 143 | + | end; | |
| 144 | + | ||
| 145 | + | This will save MyRect as a property 'MyRect'. これで、MyRectをプロパティとして保存できます。 しかし、もし、たくさんのTRectをストリームする必要がある時、このようなコードで何度も書きたくないでしょう。 LResourcesユニットには、rectプロパティを定義する手続きを書く例があります。 | |
| 146 | + | ||
| 147 | + | ` procedure DefineRectProperty(Filer: TFiler; const Name: string; ARect, DefaultRect: PRect);`\ | |
| 148 | + | ` ` | |
| 149 | + | ||
| 150 | + | さきほどの方法はこれを使うと非常に短く記述することができます: | |
| 151 | + | ||
| 152 | + | procedure TMyComponent.DefineProperties(Filer: TFiler); | |
| 153 | + | begin | |
| 154 | + | inherited DefineProperties(Filer); | |
| 155 | + | DefineRectProperty(Filer,'MyRect',@FMyRect,nil); | |
| 156 | + | end; | |
| 157 | + | ||
| 158 | + | ## 独自コンポーネント - Part 2 | |
| 159 | + | ||
| 160 | + | 今回の例では、たった数行のコードで、クラスを拡張して任意のプロパティを使うことができるか、を示します。 | |
| 161 | + | ||
| 162 | + | type | |
| 163 | + | TMyComponent = class(TComponent) | |
| 164 | + | private | |
| 165 | + | FID: integer; | |
| 166 | + | FRect1: TRect; | |
| 167 | + | FRect2: TRect; | |
| 168 | + | protected | |
| 169 | + | procedure DefineProperties(Filer: TFiler); override; | |
| 170 | + | public | |
| 171 | + | property Rect1: TRect read FRect1 write FRect1; | |
| 172 | + | property Rect2: TRect read FRect2 write FRect2; | |
| 173 | + | published | |
| 174 | + | property ID: integer read FID write FID; | |
| 175 | + | end; | |
| 176 | + | ||
| 177 | + | procedure TMyComponent.DefineProperties(Filer: TFiler); | |
| 178 | + | begin | |
| 179 | + | inherited DefineProperties(Filer); | |
| 180 | + | DefineRectProperty(Filer,'Rect1',@FRect1,nil); | |
| 181 | + | DefineRectProperty(Filer,'Rect2',@FRect2,nil); | |
| 182 | + | end; | |
| 183 | + | ||
| 184 | + | このコンポーネントは [[RTTI controls/ja|RTTI コントロール]] を使用して、保存、読み込み利用ができます。これ以上、詳細なコードを書く必要はありません。 | |
| 185 | + | ||
| 186 | + | ## コンポーネントをXMLへストリームする | |
| 187 | + | ||
| 188 | + | XML形式でコンポーネントをストリームさせるのはとても簡単です。 lazarus/examples/xmlstreaming/ の例を見てください。 | |
| 189 | + | ||
| 190 | + | ## 結論 | |
| 191 | + | ||
| 192 | + | RTTI は強力なメカニズムです。これは全てのクラスでストリームを簡単に取り扱うことができ、また膨大で退屈な load/save コードの記述を回避するのに役立ちます。 | |
| 193 | + | ||
| 194 | + | こちらも参照してください。 [[RTTI controls/ja|RTTI コントロール]] | |
| 195 | + | ||
| 196 | + | --- | |
| 197 | + | *From the Free Pascal wiki, page "Streaming components/ja", as it stood on 2007-04-12 after a change by Vincent, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
