0

Using Spring, when I declare a smart pointer for a record

SmartPerson =  IShared<Pperson>;

And then create it

Smartperson := Shared<PPerson>.Make;

*by the way, think this is really cool.

How then do I go about getting RTTI on the smart pointer? Obviously, I know it's based on a TPerson record, but what steps are needed to sort of reverse what happens when Spring allocates the pointer in the first place?

I see when the record pointer is created it uses code like this

 tkPointer: IShared<Pointer>(Result) := Shared.TRecordFinalizer.Create(TypeInfo(T));

followed by code like this

constructor Shared.TRecordFinalizer.Create(typeInfo: PTypeInfo);
var
  Size : integer;
begin
  inherited Create;
  fTypeInfo := typeInfo.TypeData.RefType^;
  size := GetTypeSize(fTypeInfo);
  fValue := AllocMem(Size);
end;       

My Question is how do I get, for example, a field of the record the smart pointer points to by using the smart pointer itself?

Hope that makes sense, and maybe another dumb question.


To give some more background to problem. I have a factory which uses RTTI to build a control for a field of a record.

procedure TForm3.Button1Click(Sender: TObject);
begin
  Task := Shared<pTaskRecord>.make;
  Task.AnalysisDates.ES := now;

  Task.TaskType := TTaskTypes.DelayTask;

  ControlFactory := TControlFactory.create(Self);

  Edit := Controlfactory.GetControl('TAnalysisDates','ES');
  if assigned(Edit) then
  begin
    AddObject(Edit.Invoke);
    Edit.Value := @Task.AnalysisDates;
   end;
 end;  

Which works. It returns an TDateEdit based on attribute tags on a record

I was thinking perhaps what I could do is this

Edit := Controlfactory.GetControl(Task,Task.ES);  
//passing in my smart pointer, along with field

then the factory would have everything it needs to hook the control up without having to do it in code myself.

  Edit.Value := @Task.AnalysisDates;

the above line could be handled by the factory. Anyway just an idea

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Derek Seymour
  • 151
  • 1
  • 9
  • Not dumb but to me this sounds really like an XY Problem - step back and explain what you are actually trying to achieve. – Stefan Glienke Apr 13 '19 at 15:05
  • I think I was barking up the wrong tree. I use a lot of RTTI to build maintenance forms on the fly, and I was thinking how can I get RTTI not from attributes on a record, but just tagging a smart pointer to the record instead, then I'd have all the information I would need just by interogating my smart pointer. I could get the pointer to the record, I could have all the fields from the pointer to the record. But I realised this may be pretty stupid. – Derek Seymour Apr 14 '19 at 01:01

0 Answers0