想要设计一个通用的环行缓冲区管理器对象来管理不同的Record类型的数据,应该怎样写?

littlefat 2006-07-11 09:52:08
有两种记录类型:
TPoint=record
Counter:Integer;
Value:Byte;
end;

TPeak=record
Counter:Integer;
Value:Byte;
Width:Integer;
end;

这两种记录类型的数据,都需要一个环行缓冲管理器来管理,该环行缓冲区管理器要实现的一些功能完全相同。为了减少代码重复(提高代码复用率并减少差错),应该只设计一个通用的环行缓冲区管理类,可是我不知道应该怎样来设计这个类,所以现在万不得已,只能编下面的重复代码,希望高手指点我怎样才能写出这个通用类:

TRingBufferA=class(TObject)
private
FBuffer:Array [0..1024] of TPoint;
...
public
property Current:TPoint read GetCurrent;
property Last:TPoint read GetLast;
property Item[ItemIndex:Integer]:TPoint read GetItem;default;
function Add(APoint:TPoint):Integer;
end;

TRingBufferB=class(TObject)
private
FBuffer:Array [0..1024] of TPeak;
...
public
property Current:TPeak read GetCurrent;
property Last:TPeak read GetLast;
property Item[ItemIndex:Integer]:TPeak read GetItem;default;
function Add(APeak:TPeak):Integer;
end;


附带还有另外一个问题也让我比较难受,下面的语句编译不通过:
RingBufferA[3].Value:=50;
编译器报告说左边不能被赋值,现在我变通的办法是,将类中的代码:
property Item[ItemIndex:Integer]:TPeak read GetItem;default;
改成:
property Item[ItemIndex:Integer]:TPeak read GetItem write SetItem;default
...
TRingBufferA.SetItem(ItemIndex: Integer; const APoint: TPoint);
begin
Buffer[ItemIndex].Counter:=APoint.Counter;
Buffer[ItemIndex].Value:=APoint.Value;
end;

真的就不能直接赋值吗?

谢谢!
...全文
264 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
littlefat 2006-07-27
  • 打赏
  • 举报
回复
给分,经管楼上的方法我用不上。。。
risingsoft 2006-07-11
  • 打赏
  • 举报
回复
使用 TObjList 管理 Tobj,TObj可以兼容两中记录类对象
risingsoft 2006-07-11
  • 打赏
  • 举报
回复
unit uCircleMem;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, math, StdCtrls, Buttons, uImagePanel, ExtCtrls, Contnrs;

Type
TObjType=(otPoint,otPeak);


TPoint=record
Counter:Integer;
Value:Byte;
end;

TPeak=record
Counter:Integer;
Value:Byte;
Width:Integer;
end;

//组合对象
TObj=class
obj:TObject;
objType:TObjType;
end;
//列表对象
TObjList=class(TObjectList)
private
fObjMaxCount: integer;
fItemIndex: Integer;
function GetObjs(ItemIndex: Integer): TObj;
procedure SetObjs(ItemIndex: Integer; const Value: TObj);
procedure setItemIndex(const Value: Integer);
public
constructor Create;
property Objs[ItemIndex:Integer]:TObj read GetObjs write SetObjs;
property ObjMaxCount:integer read fObjMaxCount write fObjMaxCount;

property ItemIndex:Integer read fItemIndex write setItemIndex;

function Add(AObject: TObject): Integer;
function First: TObject;
function Last: TObject;
function GetObjAt(ItemIndex:integer):TObject;
function Next:TObject;
function Prev:TObject;
end;



implementation

{ TObjList }

function TObjList.Add(AObject: TObject): Integer;
begin
if Count < fObjMaxCount then
Inherited Add(AObject);
end;

constructor TObjList.Create;
begin
fObjMaxCount := 0;
end;

function TObjList.First: TObject;
begin
Result := TObject(inherited First);
fItemIndex := 0;
end;

function TObjList.GetObjAt(ItemIndex: integer): TObject;
begin
Result := nil;
if ItemIndex in [0..count-1] then
begin
result := Objs[ItemIndex];
fItemIndex := ItemIndex;
end;
end;

function TObjList.GetObjs(ItemIndex: Integer): TObj;
begin
result := inherited Items[ItemIndex] as TObj;
end;

function TObjList.Last: TObject;
begin
Result := TObject(inherited Last);
fItemIndex := count -1;
end;

function TObjList.Next: TObject;
begin
Result := nil;
if ItemIndex in [0..count-2] then
begin
result := objs[ItemIndex+1];
inc(fItemIndex);
end;
end;

function TObjList.Prev: TObject;
begin
Result := nil;
if ItemIndex in [1..count-1] then
begin
result := objs[ItemIndex-1];
dec(fItemIndex);
end;
end;

procedure TObjList.setItemIndex(const Value: Integer);
begin
fItemIndex := min(max(0,Value),count-1);
end;

procedure TObjList.SetObjs(ItemIndex: Integer; const Value: TObj);
begin
inherited Items[ItemIndex] := Value;
end;

end.

16,749

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧