« D2バージョン2.2.3a(ベータ版)を公開しました | メイン | xls2htmlの独自タグについての説明を加筆しました »

__propertyの使い方

__propertyの使い方を整理してみた。

基本形

__property 型 名前 = { プロパティ属性 = データ/関数, … }

プロパティ属性の種類

  • read
    読み込み時に参照する変数/関数

  • write
    書き込み時に参照する変数/関数

  • stored
    ture/false/論理値を返す関数
    コンポーネントとして保存するかどうか

  • default
    デフォルトの値

  • nodefault

  • index
    リストや配列のインデックス。

例:メンバ変数を直接読み書き

class TMyClass {
private:
  int FCount;
public:
  TMyClass() : FCount(0) {};
  __property int Count = { read = FCount, write=FCount }; //メンバ変数を指定
};

TMyClass obj;
obj.Count = 123;
cout << obj.Count << endl; //=>123

例:メンバ関数を通して読み書き

class TMyClass {
private:
  int FCount;
  void SetCount(int Count);
  int GetCount();
public:
  TMyClass() : FCount(0) {};
  __property int Count = { read = FCount, write=FCount }; //メンバ関数を指定
};

例:読み込み専用

class TMyClass {
private:
  int FCount;
public:
  TMyClass() : FCount(0) {};
  __property int Count = { read = FCount }; //readのみ
};

例:書き込み専用

class TMyClass {
private:
  int FCount;
public:
  TMyClass() : FCount(0) {};
  __property int Count = { write=FCount }; //writeのみ
};

例:静的プロパティ

class TMyClass {
public:
  static int FCount;
  static int GetCount();
  static void SetCount(int Count);
  __property int Count = { read = GetCount, write = SetCount }; //静的プロパティ
};

cout << TMyClass::Count << endl;

例:配列

class TMyClass {
private:
  int FDate[3];
  int GetDate(int Index) { return FDate[Index]; }
public:
  TMyClass() { FDate[0] = 2009; FDate[1] = 1; FDate[2] = 2; };
  __property int Year = { read=GetDate, index = 0 };
  __property int Month = { read=GetDate, index = 1 };
  __property int Day = { read=GetDate, index = 2 };
};

TMyClass obj;
cout << obj.Year << endl; //=> 2009
cout << obj.Month << endl; //=> 1
cout << obj.Day << endl; //=> 2

例:リスト

class TMyClass {
private:
  unique_ptr<TStringList> FDate;
  UnicodeString GetDate(int Index) { return FDate->Strings[Index]; }
public:
  TMyClass() : FDate(new TStringList()) {
    FDate->Add("2009"); FDate->Add("1"); FDate->Add("2");
  };
  __property UnicodeString Year = { read=GetDate, index = 0 };
  __property UnicodeString Month = { read=GetDate, index = 1 };
  __property UnicodeString Day = { read=GetDate, index = 2 };
};

TMyClass obj;
wcout << obj.Year.c_str() << endl; //=> 2009
wcout << obj.Month.c_str() << endl; //=> 1
wcout << obj.Day.c_str() << endl; //=> 2

トラックバック

このエントリーのトラックバックURL:
http://www.gesource.jp/mt/mt-tb.cgi/992

コメントを投稿

(いままで、ここでコメントしたことがないときは、コメントを表示する前にこのブログのオーナーの承認が必要になることがあります。承認されるまではコメントは表示されません。そのときはしばらく待ってください。)

About

2009年06月18日 11:39に投稿されたエントリーのページです。

ひとつ前の投稿は「D2バージョン2.2.3a(ベータ版)を公開しました」です。

次の投稿は「xls2htmlの独自タグについての説明を加筆しました」です。

他にも多くのエントリーがあります。メインページアーカイブページも見てください。

Powered by
Movable Type 3.35