一个简单的Delphi框架-xDom-1 内存对象

来源:互联网 发布:国际数据公司 idc 编辑:程序博客网 时间:2024/06/06 04:37

从现在开始,着手实现一个简单而实用的Delphi框架-xDOM,这个框架准备具备以下几个特色:

1 基于DesignOne模板技术设计

2 分层设计,暂时分为对象层,存储层,显示层!

3 完整设计,理想情况下,数据模型设计完成,相应的初始完整代码也能够完成!

4 命名为:xDOM-X Delphi Object Model

 

 

基于上述目标,首先实现对象层代码如下:

  1. {*******************************************************}
  2. //
  3. //   对象模板:完成数据在内存中的组织(基于DesignOne创建)
  4. //   其中: TBaseObject是所有数据对象的基类
  5. //         TBaseObjectList是所有数据对象集合的基类,数据对象集合负责数据对象的管理,支持快速排序和搜索
  6. //         TController是一个事件控制器,负责操作完成后的通知
  7. //         TControllers对多个TController进行管理,以完成事件的多路通知
  8. //         TDataFlow负责数据流的多步处理
  9. //         TModelData负责完成以上各对象的初始化,提供一个数据对象的入口点,
  10. //                   系统一般从实例化类的对象开始
  11. //
  12. {*******************************************************}
  13. unit ModelObject;
  14. interface
  15. uses
  16.   Classes,Sysutils,Windows;
  17. type
  18.   TFilteredList = array of Integer;
  19.   TSortedList = array of Integer;
  20.   TSortedList2 = array of TSortedList;
  21.   TFilterOperation = (foMore,foMoreandEqual,foLess,foLessandEqual,foEqual,foNotEqual,foLike);
  22.   TSortType = (stUp,stDown);
  23.   TFilterShip =(fsAnd,fsOr);
  24. type
  25.   TPropertyIdent=class
  26.     Ident:Integer;
  27.   end;
  28.   TFilterParams=class(TPropertyIdent)
  29.   public
  30.     Operation :TFilterOperation;
  31.     Ship:TFilterShip;
  32.     Value:Variant;
  33.   end;
  34.   TSortParams=class(TPropertyIdent)
  35.   public
  36.     SortType:TSortType;
  37.   end;
  38. type
  39.   TBaseObject =class;
  40.   TBaseObjectList=class;
  41.   TBaseObject =class
  42.   private
  43.     FObjectType: integer;
  44.     FAction: Integer;
  45.     FChangedNotify :boolean;
  46.     FObserverlist:TList;
  47.     procedure NotifyObserver(PropertyIdent:integer);   //发出更改通知
  48.   public
  49.     constructor create;
  50.     destructor Destroy;override;
  51.     procedure  Attach(AObserver:TBaseObjectList);
  52.     procedure  Detach(AObserver:TBaseObjectList);
  53.     property ObjectType : integer read FObjectType write FObjectType;
  54.     property Action     :Integer read FAction write FAction;
  55.     property ChangedNotify :boolean read FChangedNotify   write FChangedNotify  default True;
  56.   end;
  57.   TBaseObjectList=class
  58.   private
  59.     FList:TList;
  60.     FFilterParamList: TList;
  61.     FSortParamList: TList;
  62.     function  Compare(AParams:TSortParams;ADataObject1,ADataObject2:pointer):Integer;
  63.     function  Equal(AParams:TSortParams;ADataObject1,ADataObject2:pointer):Boolean;
  64.     procedure QuickSort(AParams:TSortParams;L, R: Integer;SortList: TSortedList);
  65.     function  Filter2By(AShip:TFilterShip;AFilters1, AFilters2:TFilteredList):TFilteredList;
  66.     function  DefaultFilterResult: TFilteredList;
  67.   protected
  68.     procedure GetCompareValue(AParams: TFilterParams;ADataObject:pointer;
  69.                        var datavalue,paramvalue:Variant);overload;virtual;abstract;
  70.     procedure GetCompareValue(AParams: TSortParams;ADataObject1,ADataObject2:pointer;
  71.                        var datavalue1,datavalue2:Variant);overload;virtual;abstract;
  72.   public
  73.     constructor create;
  74.     destructor Destroy;override;
  75.     procedure Update(AObject:TBaseObject;PropertyIdent:integer);virtual;abstract;
  76.     function FiltersBy(AParamsList: TList): TFilteredList;   //搜索
  77.     function FilterBy(AParams:TFilterParams):TFilteredList;
  78.     function Sortsby(ASortParamsList: TList;AFilters:TFilteredList):TSortedList; //快速排序
  79.     function Sortby(ASortParams: TSortParams;AFilters:TFilteredList):TSortedList;
  80.     property  FilterParamList:TList read FFilterParamList; //搜索参数列表,装载TFilterParams类型的参数
  81.     property  SortParamList:TList read FSortParamList;     //排序参数列表,装载TSortParams类型的参数
  82.   end;
  83. const
  84.   PropertyOffset =1000;
  85. //对象,对象属性唯一标识符
  86. {DesignOne}
  87. {CodeTemplate}
  88. {<Object ObjectMapType=AllObjects>
  89.   dot_[Objectname] = [ObjectIndex];   //[ObjectDisplayName]
  90.    <Property PropertyMapType=AllPropertys ObjectName=[ObjectName]>
  91.    dpt_[Objectname][PropertyName] =[ObjectIndex]*PropertyOffset+[PropertyIndex]; //[PropertyDisplayName]
  92.    </Property>
  93.  </Object>}
  94. {/CodeTemplate}
  95. {/DesignOne}
  96. type
  97. {DesignOne}
  98. {CodeTemplate}
  99. {<Object ObjectMapType=AllObjects>
  100.   T[Objectname] = class;
  101.   T[Objectname]List = class;
  102.  </Object>}
  103. {/CodeTemplate}
  104. {/DesignOne}
  105.   TDataFlow =class;
  106.   TController =class;
  107.   TControllers =class(TList)
  108.   public
  109.     destructor Destroy; override;
  110.   end;
  111. {DesignOne}
  112. {CodeTemplate}
  113. {<Object ObjectMapType=AllObjects>
  114.   T[Objectname] = class(TBaseObject)  //[ObjectDisplayName]
  115.   private
  116.    <ObjectShip ObjectShipMapType=ParentObjects ObjectName=[ObjectName]>
  117.    f[ObjectShipName] : T[ObjectShipName];  //[ObjectShipDisplayName]
  118.    </ObjectShip>
  119.    <ObjectShip ObjectShipMapType=ChildObjects ObjectName=[ObjectName]>
  120.    f[ObjectShipName]List : T[ObjectShipName]List;  //[ObjectShipDisplayName]
  121.    </ObjectShip>
  122.    <Property PropertyMapType=AllPropertys ObjectName=[ObjectName]>
  123.    f[Propertyname] : [VarType];
  124.    </Property>
  125.    <Property PropertyMapType=AllPropertys ObjectName=[ObjectName]>
  126.    procedure Set[Propertyname](const Value : [VarType]);
  127.    </Property>
  128.   public
  129.     constructor create;
  130.     destructor Destroy;override;
  131.     procedure Cloneto(ADest:T[Objectname];ADeepClone:Boolean);
  132.     function GetDataBuffer(var ABuffer: PChar; var len: integer): Boolean;
  133.     function GetDataObject(ABuffer: PChar): Boolean;
  134.     <ObjectShip ObjectShipMapType=ParentObjects ObjectName=[ObjectName]>
  135.     property [ObjectShipName] : T[ObjectShipName] read f[ObjectShipName] write f[ObjectShipName]; //[ObjectShipDisplayName]
  136.    </ObjectShip>
  137.    <ObjectShip ObjectShipMapType=ChildObjects ObjectName=[ObjectName]>
  138.     property [ObjectShipName]List : T[ObjectShipName]List read f[ObjectShipName]List;  //[ObjectShipDisplayName]
  139.    </ObjectShip>
  140.     <Property PropertyMapType=AllPropertys ObjectName=[ObjectName]>
  141.     property [Propertyname] : [VarType] read f[Propertyname] write Set[Propertyname];  //[PropertyDisplayName]
  142.     </Property>
  143.    end;
  144.    TOnInsert[ObjectName]Event   =procedure(index:integer;AData: T[ObjectName]) of Object;
  145.    TOnAdd[ObjectName]Event   =procedure(AData: T[ObjectName]) of Object;
  146.    TOnDelete[ObjectName]Event=procedure(AData: T[ObjectName]) of Object;
  147.    TOnChange[ObjectName]Event=procedure(AData: T[ObjectName]) of Object;
  148.     T[ObjectName]List=class(TBaseObjectList)
  149.     private
  150.       FDataFlow:TDataFlow;
  151.       FControllers : TControllers;
  152.       FAdd[ObjectName]Event: TOnAdd[ObjectName]Event;
  153.       FChange[ObjectName]Event: TOnChange[ObjectName]Event;
  154.       FDelete[ObjectName]Event: TOnDelete[ObjectName]Event;
  155.       FInsert[ObjectName]Event: TOnInsert[ObjectName]Event;
  156.       function GetCount: integer;
  157.       function GetItems(index: integer): T[ObjectName];
  158.       procedure SetItems(index: integer; const Value: T[ObjectName]);
  159.       procedure SetDataFlow(const Value: TDataFlow);
  160.       procedure SetControllers(const Value: TControllers);
  161.     protected
  162.       procedure GetCompareValue(AParams: TFilterParams;ADataObject:pointer;
  163.                        var datavalue,paramvalue:Variant);overload;override;
  164.       procedure GetCompareValue(AParams: TSortParams;ADataObject1,ADataObject2:pointer;
  165.                        var datavalue1,datavalue2:Variant);overload;override;
  166.     public
  167.       destructor Destroy;override;
  168.       function  New[ObjectName]: T[ObjectName];     //新建立一个[ObjectDisplayName]对象
  169.       function  Add[ObjectName](A[ObjectName]: T[ObjectName]):integer;  //添加一个[ObjectDisplayName]对象
  170.       procedure Delete[ObjectName](index:integer);                      //删除一个[ObjectDisplayName]对象
  171.       function Remove[ObjectName](A[ObjectName]: T[ObjectName]):integer; //移除一个[ObjectDisplayName]对象
  172.       function  Indexof(A[ObjectName]: T[ObjectName]):integer;
  173.       procedure ClearList;
  174.       procedure Cloneto(ADest:T[ObjectName]List;ADeepClone:Boolean);    //拷贝一个[ObjectDisplayName]对象
  175.       procedure Insert(index:Integer;A[ObjectName]: T[ObjectName]);     //插入一个[ObjectDisplayName]对象
  176.       procedure Exchange(index1,index2:Integer);
  177.       procedure Update(AObject:TBaseObject;PropertyIdent:integer);override;
  178.       <Property PropertyMapType=AllPropertys ObjectName=[ObjectName]>
  179.       function Findby[Propertyname](A[Propertyname]:[VarType]):T[ObjectName]; //查找[PropertyDisplayname] = A[Propertyname]的对象,返回第一个
  180.       </Property>
  181.       property Count: integer read GetCount;
  182.       property Items[index:integer]: T[ObjectName] read GetItems write SetItems;
  183.       property OnInsert[ObjectName]Event    :TOnInsert[ObjectName]Event read FInsert[ObjectName]Event write FInsert[ObjectName]Event;
  184.       property OnAdd[ObjectName]Event    :TOnAdd[ObjectName]Event read FAdd[ObjectName]Event write FAdd[ObjectName]Event;
  185.       property OnDelete[ObjectName]Event :TOnDelete[ObjectName]Event read FDelete[ObjectName]Event write FDelete[ObjectName]Event;
  186.       property OnChange[ObjectName]Event :TOnChange[ObjectName]Event read FChange[ObjectName]Event write FChange[ObjectName]Event;
  187.       property DataFlow:TDataFlow read FDataFlow write SetDataFlow;  //数据流
  188.       property Controllers : TControllers read FControllers write SetControllers; //事件流控制
  189.     end;
  190.  </Object>}
  191. {/CodeTemplate}
  192. {/DesignOne}
  193.   TDataFlow =class
  194. {DesignOne}
  195. {CodeTemplate}
  196. {<Object ObjectMapType=AllObjects>
  197.   procedure AfterAdd[Objectname](A[Objectname]: T[Objectname]);virtual;  //在加入一个[ObjectDisplayname] 后的处理
  198.   procedure AfterDelete[Objectname](A[Objectname]: T[Objectname]);virtual;  //在删除一个[ObjectDisplayname] 后的处理
  199.    <Property PropertyMapType=AllPropertys ObjectName=[ObjectName]>
  200.    procedure AfterUpdate[Objectname]_[PropertyName](A[Objectname]: T[Objectname]);virtual;  //在更改一个[ObjectDisplayname] 的[PropertyDisplayName]后的处理
  201.    </Property>
  202.  </Object>}
  203. {/CodeTemplate}
  204. {/DesignOne}
  205.   end;
  206.  TController =class
  207.  private
  208. {DesignOne}
  209. {CodeTemplate}
  210. {<Object ObjectMapType=AllObjects>
  211.     fOnAdd[Objectname]: TOnAdd[ObjectName]Event;
  212.     fOnInsert[Objectname]: TOnInsert[ObjectName]Event;
  213.     fOnDelete[Objectname]: TOnDelete[ObjectName]Event;
  214.     fOnChange[Objectname]: TOnChange[ObjectName]Event;
  215.  </Object>}
  216. {/CodeTemplate}
  217. {/DesignOne}
  218.   public
  219. {DesignOne}
  220. {CodeTemplate}
  221. {<Object ObjectMapType=AllObjects>
  222.     property OnAdd[Objectname] : TOnAdd[ObjectName]Event read fOnAdd[Objectname] write fOnAdd[Objectname];   //成功添加一个[ObjectDisplayname] 对象后,发出添加通知
  223.     property OnInsert[Objectname] : TOnInsert[ObjectName]Event read fOnInsert[Objectname] write fOnInsert[Objectname];   //成功插入一个[ObjectDisplayname] 对象后,发出插入通知
  224.     property OnDelete[Objectname] : TOnDelete[ObjectName]Event read fOnDelete[Objectname] write fOnDelete[Objectname];   //成功删除一个[ObjectDisplayname] 对象后,发出删除通知
  225.     property OnChange[Objectname]: TOnChange[ObjectName]Event read fOnChange[Objectname] write fOnChange[Objectname];    //成功修改一个[ObjectDisplayname] 对象的值后,发出改变通知
  226.  </Object>}
  227. {/CodeTemplate}
  228. {/DesignOne}
  229.   end;
  230. //整个数据的管理入口
  231.   TModelData = class
  232.   private
  233.     FDataFlow: TDataflow;
  234.     fControllers :TControllers;
  235.     {DesignOne}
  236. {CodeTemplate}
  237. {<Object ObjectMapType=TopObjects>
  238.     F[ObjectName]List: T[ObjectName]List;
  239.  </Object>}
  240. {/CodeTemplate}
  241. {/DesignOne}
  242.     procedure SetDataFlow(const Value : TDataflow);
  243.   public
  244.     constructor Create;
  245.     destructor Destroy; override;
  246.     property DataFlow :TDataflow read FDataFlow write SetDataFlow;
  247.     property Controllers :TControllers read FControllers;
  248. {DesignOne}
  249. {CodeTemplate}
  250. {<Object ObjectMapType=TopObjects>
  251.     property [ObjectName]List : T[ObjectName]List read F[ObjectName]List;
  252.  </Object>}
  253. {/CodeTemplate}
  254. {/DesignOne}
  255.   end;
  256. implementation
  257. { TBaseObject }
  258. procedure TBaseObject.Attach(AObserver: TBaseObjectList);
  259. begin
  260.   FObserverlist.Add(AObserver);
  261. end;
  262. constructor TBaseObject.create;
  263. begin
  264.   FObserverlist:= TList.Create;
  265.   FChangedNotify := True;
  266. end;
  267. destructor TBaseObject.Destroy;
  268. begin
  269.   FObserverlist.Free;
  270.   inherited;
  271. end;
  272. procedure TBaseObject.Detach(AObserver: TBaseObjectList);
  273. begin
  274.   FObserverlist.Remove(AObserver);
  275. end;
  276. procedure TBaseObject.NotifyObserver(PropertyIdent:integer);
  277. var
  278.   i:integer;
  279.   obs:TBaseObjectList;
  280. begin
  281.   for i := 0 to FObserverlist.Count-1 do
  282.   begin
  283.     obs := FObserverlist.Items[i];
  284.     obs.Update(Self,PropertyIdent);
  285.   end;
  286. end;
  287. {TDataFlow}
  288. {DesignOne}
  289. {CodeTemplate}
  290. {<Object ObjectMapType=AllObjects>
  291. procedure TDataFlow.AfterAdd[Objectname](A[Objectname]: T[Objectname]);begin end;
  292. procedure TDataFlow.AfterDelete[Objectname](A[Objectname]: T[Objectname]);begin end;
  293.    <Property PropertyMapType=AllPropertys ObjectName=[ObjectName]>
  294. procedure TDataFlow.AfterUpdate[Objectname]_[PropertyName](A[Objectname]: T[Objectname]);begin end;
  295.    </Property>
  296.  </Object>}
  297. {/CodeTemplate}
  298. {/DesignOne}
  299. destructor TControllers.Destroy;
  300. var
  301.   C:TController;
  302. begin
  303.   while Count>0 do begin c := Items[0]; c.Free; Delete(0); end;
  304.   inherited;
  305. end;
  306. constructor TBaseObjectList.create;
  307. begin
  308.   inherited;
  309.    FList := TList.Create;
  310.    FFilterParamList := TList.Create;
  311.    FSortParamList := TList.Create;
  312. end;
  313. destructor TBaseObjectList.Destroy;
  314. begin
  315.   FList.Free;
  316.   FFilterParamList.Free;
  317.   FSortParamList.Free;
  318.   inherited;
  319. end;
  320. function TBaseObjectList.DefaultFilterResult:TFilteredList;
  321. var
  322.   i: integer;
  323. begin
  324.   SetLength(Result,0);
  325.   for i := 0 to fList.Count - 1 do
  326.   begin
  327.     SetLength(Result,i+1);
  328.     Result[i] := i;
  329.   end;
  330. end;
  331. function TBaseObjectList.FilterBy(AParams: TFilterParams): TFilteredList;
  332.   function Includeit(ADataObject:pointer):Boolean;
  333.   var
  334.     datavalue,paramvalue:Variant;
  335.   begin
  336.     GetCompareValue(AParams,ADataObject,datavalue,paramvalue);
  337.     case AParams.Operation of
  338.       foMore: Result := datavalue>paramvalue;
  339.       foLess: Result := datavalue<paramvalue;
  340.       foEqual:Result := datavalue=paramvalue;
  341.       foMoreandEqual:Result := datavalue>=paramvalue;
  342.       foLessandEqual:Result := datavalue<=paramvalue;
  343.       foNotEqual:Result := datavalue<>paramvalue;
  344.       foLike : Result := Pos(paramvalue,datavalue)>0;
  345.     end;
  346.   end;
  347. var
  348.   i,j: integer;
  349. begin
  350.   j := 0;
  351.   SetLength(Result,j);
  352.   for i := 0 to Flist.Count - 1 do
  353.   begin
  354.     if Includeit(Flist.Items[i]) then
  355.     begin
  356.       Inc(j);
  357.       SetLength(Result,j);
  358.       Result[j-1] := i;
  359.     end;
  360.   end;
  361. end;
  362. function TBaseObjectList.Filter2By(AShip:TFilterShip;AFilters1, AFilters2:TFilteredList):TFilteredList;
  363. var
  364.   i,j,k: integer;
  365.   function Exists(AFilters:TFilteredList;value:Integer):Boolean;
  366.   var
  367.     n:Integer;
  368.   begin
  369.     Result := false;
  370.     for n := Low(AFilters) to High(AFilters) do
  371.     begin
  372.       if AFilters[n]= value then
  373.       begin
  374.         Result := true;
  375.         Break;
  376.       end;
  377.     end;
  378.   end;
  379. begin
  380.   k := 0;
  381.   SetLength(Result,k);
  382.   case AShip of
  383.     fsAnd :
  384.     begin
  385.       for i := Low(AFilters1) to High(AFilters1) do
  386.       begin
  387.         for j:= Low(AFilters2) to High(AFilters2) do
  388.         begin
  389.           if   AFilters1[i] = AFilters2[j] then
  390.           begin
  391.             Inc(k);
  392.             SetLength(Result,k);
  393.             Result[k-1] := i;
  394.           end;
  395.         end;
  396.       end;
  397.     end;
  398.     fsOr :
  399.     begin
  400.       for i := Low(AFilters1) to High(AFilters1) do
  401.       begin
  402.         Inc(k);
  403.         SetLength(Result,k);
  404.         Result[k-1] := i;
  405.       end;
  406.       for i := Low(AFilters2) to High(AFilters2) do
  407.       begin
  408.         if not Exists(AFilters1,AFilters2[i]) then
  409.         begin
  410.           Inc(k);
  411.           SetLength(Result,k);
  412.           Result[k-1] := i;
  413.         end;
  414.       end;
  415.     end;
  416.   end;
  417. end;
  418. function TBaseObjectList.FiltersBy(AParamsList:TList):TFilteredList;
  419. var
  420.   i:Integer;
  421.   AParams1,AParams2:TFilterParams;
  422. begin
  423.   SetLength(Result,0);
  424.   if AParamsList.Count=0  then
  425.   begin
  426.     Result := DefaultFilterResult;
  427.     Exit;
  428.   end;
  429.   Result := FilterBy(AParamsList.Items[0]);
  430.   for i := 1 to AParamsList.Count-1 do
  431.     Result := Filter2By(TFilterParams(AParamsList.Items[i-1]).Ship,
  432.                         Result,FilterBy(AParamsList.Items[i]));
  433. end;
  434. function TBaseObjectList.Sortsby(ASortParamsList: TList;AFilters:TFilteredList): TSortedList;
  435. var
  436.   i:Integer;
  437.   function ReGroup(ASortList:TSortedList;ASortParams:TSortParams):TSortedList2;
  438.   var
  439.     j,k,n:integer;
  440.   begin
  441.     j := 0 ;n := 0;
  442.     SetLength(Result,j);
  443.     if (High(ASortList)-Low(ASortList)<0then Exit;
  444.     Inc(j);
  445.     SetLength(Result,j);
  446.     Inc(n);
  447.     SetLength(Result[j-1],n);
  448.     Result[j-1][n-1] := ASortList[0];
  449.     for k := Low(ASortList)+1 to High(ASortList) do
  450.     begin
  451.       if not Equal(ASortParams,FList.Items[ASortList[k]],FList.Items[ASortList[k-1]]) then
  452.       begin
  453.         n := 0 ;
  454.         inc(j);
  455.         SetLength(Result,j);
  456.       end;
  457.       Inc(n);
  458.       SetLength(Result[j-1],n);
  459.       Result[j-1][n-1] := ASortList[k];
  460.     end;
  461.   end;
  462.   function Sort(ASortList:TSortedList;Index:Integer):TSortedList;
  463.   var
  464.     m,l,h:Integer;
  465.     SortedList2 : TSortedList2;
  466.     SortList:TSortedList;
  467.   begin
  468.     h := 0 ;
  469.     SetLength(Result,0);
  470.     for h := Low(ASortList) to High(ASortList) do
  471.     begin
  472.       SetLength(Result,h+1);
  473.       Result[h] := ASortList[h];
  474.     end;
  475.     if Index>=ASortParamsList.Count-1  then Exit;
  476.     h := 0;
  477.     SetLength(Result,h);
  478.     SortedList2 := ReGroup(ASortList,ASortParamsList.items[Index]);
  479.     for m := Low(sortedlist2) to High(sortedlist2) do
  480.     begin
  481.       SortList := sortedlist2[m];
  482.       QuickSort(ASortParamsList.items[Index+1],Low(SortList),High(SortList),SortList);
  483.       SortList := Sort(SortList,Index+1);
  484.       for l := Low(SortList) to High(SortList) do
  485.       begin
  486.         Inc(h);
  487.         SetLength(Result,h);
  488.         Result[h-1] := SortList[l];
  489.       end;
  490.     end;
  491.   end;
  492. begin
  493.   i := 0 ;
  494.   SetLength(Result,0);
  495.   for i := Low(AFilters) to High(AFilters) do
  496.   begin
  497.     SetLength(Result,i+1);
  498.     Result[i] := AFilters[i];
  499.   end;
  500.   if ASortParamsList.Count=0 then Exit;
  501.   QuickSort(ASortParamsList.items[0],Low(Result),High(Result),Result);
  502.   Result := Sort(Result,0);
  503. end;
  504. procedure TBaseObjectList.QuickSort(AParams:TSortParams; L, R: Integer;SortList: TSortedList);
  505. var
  506.   I, J: Integer;
  507.   P: Pointer;
  508.   T:Integer;
  509. begin
  510.   if High(SortList)-low(SortList)<1 then Exit;
  511.   repeat
  512.     I := L;
  513.     J := R;
  514.     P := FList.Items[SortList[(L + R) shr 1]];
  515.     repeat
  516.       while (Compare(AParams,FList.Items[SortList[I]], P)<0)  do
  517.         Inc(I);
  518.       while (Compare(AParams,FList.Items[SortList[J]], P)>0)  do
  519.         Dec(J);
  520.       if I <= J then
  521.       begin
  522.         T := SortList[I];
  523.         SortList[I] := SortList[J];
  524.         SortList[J] := T;
  525.         Inc(I);
  526.         Dec(J);
  527.       end;
  528.     until I > J;
  529.     if L < J then
  530.       QuickSort(AParams, L, J,SortList);
  531.     L := I;
  532.   until I >= R;
  533. end;
  534. function TBaseObjectList.Compare(AParams: TSortParams; ADataObject1,
  535.   ADataObject2: pointer): integer;
  536. var
  537.   datavalue1,datavalue2:Variant;
  538. begin
  539.   getcomparevalue(AParams,ADataObject1,ADataObject2,datavalue1,datavalue2);
  540.   case AParams.SortType of
  541.     stUp : if   datavalue1>datavalue2 then Result := 1 else  if datavalue1=datavalue2 then Result := 0 else Result := -1;
  542.     stDown :if  datavalue1<datavalue2 then Result := 1 else  if datavalue1=datavalue2 then Result := 0 else Result := -1;
  543.   end;
  544. end;
  545. function TBaseObjectList.Equal(AParams: TSortParams; ADataObject1,
  546.   ADataObject2: pointer): Boolean;
  547. var
  548.   datavalue1,datavalue2:Variant;
  549. begin
  550.   getcomparevalue(AParams,ADataObject1,ADataObject2,datavalue1,datavalue2);
  551.   Result := datavalue1=datavalue2;
  552. end;
  553. function TBaseObjectList.Sortby(ASortParams: TSortParams;
  554.   AFilters: TFilteredList): TSortedList;
  555. var
  556.   i:Integer;
  557. begin
  558.   i := 0 ;
  559.   SetLength(Result,0);
  560.   for i := Low(AFilters) to High(AFilters) do
  561.   begin
  562.     SetLength(Result,i+1);
  563.     Result[i] := AFilters[i];
  564.   end;
  565.   QuickSort(ASortParams,Low(Result),High(Result),Result);
  566. end;
  567. {DesignOne}
  568. {CodeTemplate}
  569. {<Object ObjectMapType=AllObjects>
  570. constructor T[ObjectName].create;
  571. begin
  572.   inherited;
  573.   ObjectType := dot_[ObjectName];
  574.  <ObjectShip ObjectShipMapType=ChildObjects ObjectName=[ObjectName]>
  575.  f[ObjectShipName]List := T[ObjectShipName]List.Create;
  576.  </ObjectShip>
  577.  <Property PropertyMapType=AllPropertys ObjectName=[ObjectName]>
  578.  f[Propertyname] := [DefaultValue];
  579.  </Property>
  580. end;
  581. destructor T[ObjectName].Destroy;
  582. begin
  583.  <ObjectShip ObjectShipMapType=ChildObjects ObjectName=[ObjectName]>
  584.   f[ObjectShipName]List.Free;
  585.  </ObjectShip>
  586.   inherited;
  587. end;
  588. procedure T[ObjectName].Cloneto(ADest:T[ObjectName];ADeepClone:Boolean);
  589. begin
  590.  <Property PropertyMapType=AllPropertys ObjectName=[ObjectName]>
  591.     ADest.[Propertyname] := f[Propertyname];
  592.  </Property>
  593.   if ADeepClone then
  594.   begin
  595.    <ObjectShip ObjectShipMapType=ChildObjects ObjectName=[ObjectName]>
  596.     f[ObjectShipName]List.Cloneto(ADest.[ObjectShipName]List,ADeepClone);
  597.    </ObjectShip>
  598.   end;
  599. end;
  600. function T[ObjectName].GetDataBuffer(var ABuffer:PChar;var len:integer):Boolean;
  601. var
  602.   alen:Integer;
  603. begin
  604.   Result := False;
  605.   len := 0;
  606.  <Property PropertyMapType=AllPropertys ObjectName=[ObjectName]>
  607.   alen := [PropertyDataSize]([PropertyName]);
  608.   CopyMemory(@ABuffer[len],@alen,SizeOf(alen));
  609.   CopyMemory(@ABuffer[len+SizeOf(alen)],@[PropertyName],alen);
  610.   len := len+alen+SizeOf(alen);
  611.  </Property>
  612.    Result := True;
  613. end;
  614. function T[ObjectName].GetDataObject(ABuffer: PChar): Boolean;
  615. var
  616.   alen:Integer;
  617.   offset:integer;
  618. begin
  619.   Result := False;
  620.   offset := 0;
  621.  <Property PropertyMapType=AllPropertys ObjectName=[ObjectName]>
  622.   CopyMemory(@alen,@ABuffer[offset],SizeOf(alen));
  623.   CopyMemory(@[PropertyName],@ABuffer[offset+SizeOf(alen)],alen);
  624.   offset := alen+SizeOf(alen);
  625.  </Property>
  626.   Result := True;
  627. end;
  628.  <Property PropertyMapType=AllPropertys ObjectName=[ObjectName]>
  629. procedure T[ObjectName].Set[PropertyName](const Value: [VarType]);
  630. begin
  631.   if Value = f[PropertyName] then Exit;
  632.   F[PropertyName] := Value;
  633.   if FChangedNotify then NotifyObserver(dpt_[ObjectName][PropertyName]);
  634. end;
  635.  </Property>
  636. destructor T[ObjectName]List.Destroy;
  637. begin
  638.   ClearList;
  639.   inherited;
  640. end;
  641. procedure T[ObjectName]List.ClearList;
  642. var
  643.   Data : T[ObjectName];
  644. begin
  645.   while FList.Count>0 do begin Data := FList.Items[0]; Data.Detach(Self); data.Free; FList.Delete(0); end;
  646. end;
  647. function T[ObjectName]List.Add[ObjectName](A[ObjectName]: T[ObjectName]): integer;
  648. var
  649.   i:integer;
  650.   C : TController;
  651. begin
  652.   Result := FList.Add(A[ObjectName]);
  653.   A[ObjectName].Attach(Self);
  654.   if Assigned(FDataFlow)  then FDataFlow.AfterAdd[ObjectName](A[ObjectName]);
  655.   if Assigned(FControllers) then
  656.   for i := 0 to FControllers.Count-1 do
  657.   begin
  658.     C := FControllers.Items[i];
  659.     if Assigned(C.OnAdd[ObjectName]) then
  660.       C.OnAdd[ObjectName](A[ObjectName]);
  661.   end;
  662.   if Assigned(FAdd[ObjectName]Event) then FAdd[ObjectName]Event(A[ObjectName]);
  663. end;
  664. procedure T[ObjectName]List.Delete[ObjectName](index: integer);
  665. var
  666.   i:integer;
  667.   C : TController;
  668. begin
  669.   T[ObjectName](FList.Items[index]).Detach(Self);
  670.   if Assigned(FDataFlow)  then FDataFlow.AfterDelete[ObjectName](T[ObjectName](FList.Items[index]));
  671.   if Assigned(FControllers) then
  672.   for i := 0 to FControllers.Count-1 do
  673.   begin
  674.     C := FControllers.Items[i];
  675.     if Assigned(C.OnDelete[ObjectName]) then
  676.       C.OnDelete[ObjectName](FList.Items[index]);
  677.   end;
  678.   if Assigned(FDelete[ObjectName]Event) then FDelete[ObjectName]Event(FList.Items[index]);
  679.   FList.Delete(index);
  680.  end;
  681. function T[ObjectName]List.GetCount: integer;
  682. begin
  683.   Result := FList.Count;
  684. end;
  685. function T[ObjectName]List.GetItems(index: integer): T[ObjectName];
  686. begin
  687.   Result := FList.Items[Index];
  688. end;
  689. function T[ObjectName]List.Indexof(A[ObjectName]: T[ObjectName]): integer;
  690. begin
  691.   Result := FList.IndexOf(A[ObjectName]);
  692. end;
  693. function T[ObjectName]List.Remove[ObjectName](A[ObjectName]: T[ObjectName]): integer;
  694. var
  695.   i:integer;
  696.   C : TController;
  697. begin
  698.   A[ObjectName].Detach(Self);
  699.   if Assigned(FDelete[ObjectName]Event) then FDelete[ObjectName]Event(A[ObjectName]);
  700.   if Assigned(FDataFlow)  then FDataFlow.AfterDelete[ObjectName](A[ObjectName]);
  701.   if Assigned(FControllers) then
  702.   for i := 0 to FControllers.Count-1 do
  703.   begin
  704.     C := FControllers.Items[i];
  705.     if Assigned(C.OnDelete[ObjectName]) then
  706.       C.OnDelete[ObjectName](A[ObjectName]);
  707.   end;
  708.    Result := FList.Remove(A[ObjectName]);
  709. end;
  710. procedure T[ObjectName]List.SetItems(index: integer; const Value: T[ObjectName]);
  711. begin
  712.   FList.Items[index] := Value;
  713. end;
  714. procedure T[ObjectName]List.Cloneto(ADest:T[ObjectName]List;ADeepClone:Boolean);
  715. var
  716.   i:integer;
  717.   A[ObjectName]: T[ObjectName];
  718. begin
  719.   ADest.ClearList;
  720.   for i := 0 to   Count-1 do
  721.   begin
  722.     A[ObjectName]:=T[ObjectName].create;
  723.     ADest.Add[ObjectName](A[ObjectName]);
  724.     Items[i].Cloneto(A[ObjectName],ADeepClone);
  725.   end;
  726. end;
  727. procedure T[ObjectName]List.Insert(index: Integer; A[ObjectName]: T[ObjectName]);
  728. var
  729.   i:integer;
  730.   C : TController;
  731. begin
  732.   FList.Insert(index,A[ObjectName]);
  733.   A[ObjectName].Attach(Self);
  734.   if Assigned(FDataFlow)  then FDataFlow.AfterAdd[ObjectName](A[ObjectName]);
  735.   if Assigned(FControllers) then
  736.   for i := 0 to FControllers.Count-1 do
  737.   begin
  738.     C := FControllers.Items[i];
  739.     if Assigned(C.OnInsert[ObjectName]) then
  740.       C.OnInsert[ObjectName](index,A[ObjectName]);
  741.   end;
  742.   if Assigned(Finsert[ObjectName]Event) then Finsert[ObjectName]Event(index,A[ObjectName]);
  743. end;
  744. procedure T[ObjectName]List.Exchange(index1, index2: Integer);
  745. begin
  746.   FList.Exchange(index1, index2);
  747. end;
  748. <Property PropertyMapType=AllPropertys ObjectName=[ObjectName]>
  749. function T[ObjectName]List.Findby[Propertyname](A[Propertyname]:[VarType]):T[ObjectName];
  750. var
  751.   i:Integer;
  752.   l[ObjectName]:T[ObjectName];
  753. begin
  754.   Result := nil ;
  755.   for i:= 0 to FList.Count-1 do
  756.   begin
  757.     l[ObjectName] := FList.Items[i];
  758.     if [UpperCase](l[ObjectName].[Propertyname]) = [UpperCase](A[Propertyname]) then
  759.     begin
  760.       Result := l[ObjectName];
  761.       Break;
  762.     end;
  763.   end;
  764. end;
  765. </Property>
  766. procedure  T[ObjectName]List.GetCompareValue(AParams: TFilterParams;ADataObject: pointer; var datavalue,paramvalue:Variant);
  767. var
  768.   dataObject : T[ObjectName];
  769. begin
  770.   dataObject := ADataObject;
  771.   paramvalue := AParams.Value;
  772.   case  AParams.Ident of
  773.  <Property PropertyMapType=AllPropertys ObjectName=[ObjectName]>
  774.     dpt_[ObjectName][PropertyName]: datavalue := dataObject.[PropertyName];
  775.  </Property>
  776.   end;
  777. end;
  778. procedure T[ObjectName]List.GetCompareValue(AParams: TSortParams;ADataObject1,ADataObject2:pointer;
  779.                        var datavalue1,datavalue2:Variant);
  780. var
  781.   DataObject1,DataObject2 :T[ObjectName];
  782. begin
  783.   DataObject1 := ADataObject1;
  784.   DataObject2 := ADataObject2;
  785.   case AParams.Ident of
  786.     <Property PropertyMapType=AllPropertys ObjectName=[ObjectName]>
  787.     dpt_[ObjectName][PropertyName]:
  788.     begin
  789.       datavalue1 := DataObject1.[PropertyName];
  790.       datavalue2 := DataObject2.[PropertyName];
  791.     end;
  792.     </Property>
  793.   end;
  794. end;
  795. function T[ObjectName]List.New[ObjectName]: T[ObjectName];
  796. begin
  797.   Result := T[ObjectName].create;
  798.   Add[ObjectName](Result);
  799. end;
  800. procedure T[ObjectName]List.Update(AObject: TBaseObject;PropertyIdent:integer);
  801. var
  802.   i:integer;
  803.   C : TController;
  804. begin
  805.   if Assigned(FDataFlow)  then
  806.   case PropertyIdent of
  807.   <Property PropertyMapType=AllPropertys ObjectName=[ObjectName]>
  808.     dpt_[ObjectName][PropertyName] : FDataFlow.AfterUpdate[ObjectName]_[PropertyName](AObject as T[ObjectName]);
  809.   </Property>
  810.   end;
  811.   if Assigned(FControllers) then
  812.   for i := 0 to FControllers.Count-1 do
  813.   begin
  814.     C := FControllers.Items[i];
  815.     if Assigned(C.OnChange[ObjectName]) then
  816.       C.OnChange[ObjectName](AObject as T[ObjectName]);
  817.   end;
  818.   if Assigned(FChange[ObjectName]Event) then FChange[ObjectName]Event(AObject as T[ObjectName]);
  819. end;
  820. procedure T[ObjectName]List.SetDataFlow(const Value: TDataFlow);
  821. var
  822.  i:integer;
  823.  l[ObjectName]:T[ObjectName];
  824. begin
  825.   if FDataFlow=Value then exit;
  826.   FDataFlow := Value;
  827.    <ObjectShip ObjectShipMapType=ChildObjects ObjectName=[ObjectName]>
  828.     for i := 0 to count-1 do
  829.     begin
  830.       l[ObjectName] := Items[i];
  831.       l[ObjectName].[ObjectShipName]List.DataFlow := Value;
  832.     end;
  833.    </ObjectShip>
  834. end;
  835. procedure T[ObjectName]List.SetControllers(const Value: TControllers);
  836. var
  837.  i:integer;
  838.  l[ObjectName]:T[ObjectName];
  839. begin
  840.   if FControllers=Value then exit;
  841.   FControllers := Value;
  842.    <ObjectShip ObjectShipMapType=ChildObjects ObjectName=[ObjectName]>
  843.     for i := 0 to count-1 do
  844.     begin
  845.       l[ObjectName] := Items[i];
  846.       l[ObjectName].[ObjectShipName]List.Controllers := Value;
  847.     end;
  848.    </ObjectShip>
  849. end;
  850.  </Object>}
  851. {/CodeTemplate}
  852. {/DesignOne}
  853. constructor TModelData.Create;
  854. begin
  855.  fControllers := TControllers.Create;
  856. {DesignOne}
  857. {CodeTemplate}
  858. {<Object ObjectMapType=TopObjects>
  859.     F[ObjectName]List := T[ObjectName]List.Create;
  860.     F[ObjectName]List.Controllers := fControllers;
  861.  </Object>}
  862. {/CodeTemplate}
  863. {/DesignOne}
  864. end;
  865. destructor TModelData.Destroy;
  866. begin
  867. {DesignOne}
  868. {CodeTemplate}
  869. {<Object ObjectMapType=TopObjects>
  870.     F[ObjectName]List.Free;
  871.  </Object>}
  872. {/CodeTemplate}
  873. {/DesignOne}
  874.  fControllers.Free;
  875.   inherited;
  876. end;
  877. procedure TModelData.SetDataFlow(const Value: TDataflow);
  878. begin
  879.   if  FDataFlow = Value then exit;
  880.   FDataFlow := Value;
  881.  {DesignOne}
  882. {CodeTemplate}
  883. {<Object ObjectMapType=TopObjects>
  884.     F[ObjectName]List.DataFlow := Value;
  885.  </Object>}
  886. {/CodeTemplate}
  887. {/DesignOne}
  888. end;
  889. end.

模型文件下载地址:

 

http://www.onewone.com/designone/xDOM.dom

 

欢迎交流!