QString 单元新增 TQBits 实现

TQBits 是用于方便设置标志位的一个实现,它实现对特定位的存取,以节省存贮空间。

TQBits = record
  private
    FBits: TBytes;
    function GetSize: Integer;
    procedure SetSize(const Value: Integer);
    function GetIsSet(AIndex: Integer): Boolean;
    procedure SetIsSet(AIndex: Integer; const Value: Boolean);
  public
    property Size: Integer read GetSize write SetSize;
    property IsSet[AIndex: Integer]: Boolean read GetIsSet
      write SetIsSet; default;
    property Bytes: TBytes read FBits;
  end;

通过 Size 属性可以调用 TQBits 的存贮空间大小,该尺寸始终是 8  的整数倍 ( 1 个字节 8 位),IsSet可以用来设置或移除某位,而 Bytes 属性则用于外部直接访问 FBits 实例的内容。

由于使用 record,所以不需要用户手动释放。

QDB 的 TQBinaryConverter 就使用该类型,来每位对应记录的某一个字段是否为空及是否发生变更等内容,具体代码可以参考 qconverter_stds.pas 中 TQBinaryConverter 的 ReadRecord  和 WriteRecord 实现。

分享到: