如何获取属性的默认值?

问题描述 投票:0回答:1

组件通常有很长的properties列表,其中包含可用的默认值:

class PACKAGE TMySpecialComboBox : public TCustomComboBox
{
public:
  __fastcall TMySpecialComboBox(TComponent *Owner);
  // ...

private:
  // ...
  bool fetch_all_;
  bool rename_;
  TColor background1_, background2_, background3_;
  // ...

__published:
  // ...
  __property bool FetchAll = {read = fetch_all_, write = fetch_all_,
                              default = false};
  __property bool Rename = {read = rename_, write = rename_,
                            default = false};
  __property TColor Background1 = {read = background1_, write = background1_,
                                   default = clWindow};
  __property TColor Background2 = {read = background2_, write = background2_,
                                   default = clWindow};
  __property TColor Background3 = {read = background3_, write = background3_,
                                   default = clWindow};
  // ...
};

将所有这些信息存储在form file浪费空间并将其读回需要时间,这是不可取的,考虑到在大多数情况下,很少有默认值发生变化。

要最小化表单文件中的数据量,可以为每个属性指定默认值(写入表单文件时,the form editor skips any properties whose values haven't been changed)。

请注意,这样做不会设置默认值:

注意:属性值不会自动初始化为默认值。也就是说,默认指令仅在属性值保存到表单文件时控制,而不是在新创建的实例上保存属性的初始值。

构造函数负责这样做:

__fastcall TMySpecialComboBox::TMySpecialComboBox(TComponent* Owner)
  : TCustomComboBox(Owner), // ...
{
  FetchAll = false;       // how to get the default value ?
  Rename = false;         // how to get the default value ?
  Background1 = clWindow  // how to get the default value ?
  // ...
}

但是以这种方式编写初始化非常容易出错。

如何获得__property的默认值?

c++builder vcl rtti dfm
1个回答
0
投票

它可以通过TRttiContext结构完成。

#include <Rtti.hpp>

int get_property_default(const String &name)
{
  TRttiContext ctx;

  auto *p(ctx.GetType(__classid(TMySpecialComboBox))->GetProperty(name));
  assert(dynamic_cast<TRttiInstanceProperty *>(p));

  return static_cast<TRttiInstanceProperty *>(p)->Default;
}

__fastcall TMySpecialComboBox::TMySpecialComboBox(TComponent* Owner)
  : TCustomComboBox(Owner), // ...
{
  FetchAll = get_property_default("FetchAll");
  // ...
}

参考文献:

© www.soinside.com 2019 - 2024. All rights reserved.