понедельник, 17 февраля 2014 г.

ToDo. Реализация списков объектов с методом CompareWith. Через Spell и DI

type
 l3Sortable = [mixin]
  [mixinparam] ItemType;
  TCompareInfoRec = record
   rA : ^ItemType;
   rB : ^ItemType;
  end;//TCompareInfoRec
  ...
  [definelater] function CompareItems(const CI : TCompareInfoRec): Integer;
  ...
 end;//l3Sortable

 Ml3Sortable = [mirror] implements l3Sortable
 end;//Ml3Sortable
 
 l3CompareableObject = [puremixin]
  [definelater] function CompareWith(anOther: l3CompareableObject): Integer;
 end;//l3CompareableObject

 l3CompareableObjectList = [mixin] implements Ml3Sortable
  [mixinparam] ItemType inherits inherited ItemType implements l3CompareableObject;
  [definelater] function CompareItems(const CI : TCompareInfoRec): Integer; override;
  begin//CompareItems
   Result := CI.rA^.CompareWith(CI.rB^);
  end;//CompareItems
 end;//l3CompareableObjectList

 [generatormethod] SimpleClass.DoSpell; override;
 begin//SimpleClass.DoSpell
  inherited;
  ...
  if Self.ImmidiatelyInheritsFrom(l3CompareableObjectList) then
  begin//Self.ImmidiatelyInheritsFrom(l3CompareableObjectList)
   if (Self.ItemType <> nil) then
   begin
    if (not Self.ItemType.HasMethod(l3CompareableObject.CompareWith)) then
    begin
     // - метода сравнения нет, добавляем его
     l_Method := Self.ItemType.AddMethod(l3CompareableObject.CompareWith);
     l_Method.SetIsVirtual(true);
    end;//not Self.ItemType.HasMethod(l3CompareableObject.CompareWith)
   end//Self.ItemType <> nil
   else
    Assert(false);
  end;//Self.ImmidiatelyInheritsFrom(l3CompareableObjectList)
  ...
 end;//SimpleClass.DoSpell

Комментариев нет:

Отправить комментарий