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