Recent

Author Topic: How to automatically "Assign" an instance to another using RTTI?  (Read 276 times)

JayEm

  • Newbie
  • Posts: 1
How to automatically "Assign" an instance to another using RTTI?
« on: November 10, 2019, 04:12:56 pm »
I've made a persistent class that works perfectly when streamed and destreamed (to/from JSON in this case, so presumably it would work using any other component streaming approach). Given that it is now set up in a such way that instances of it can be correctly recreated with some RTTI magic, I was wondering if there exists a way to "automatically" make the Assign method do the right thing, without me having to write any manual copy / assignment code? In other words, since I could write the Assign method of my class to stream the source argument and then destream it to Self and achieve the desired result (in a very roundabout way), I would think there's a way to cut out the middleman and have the assignment be performed directly.

The idea feels very obvious, but I'm having difficulty expressing it concisely (probably the reason my search efforts have turned up nothing). Is there a straightforward / "official" way to do what I'm asking for?

Thaddy

  • Hero Member
  • *****
  • Posts: 9285
Re: How to automatically "Assign" an instance to another using RTTI?
« Reply #1 on: November 10, 2019, 06:09:27 pm »
Well, the streaming is already there and is also capable for serializing to line protocols. I would not want to make any statements about instantiating through RTTI yet, because it is a moving target.
Seems to me a long way to reach a short distance. Will add example tomorrow morning.
also related to equus asinus.

Renat.Su

  • Full Member
  • ***
  • Posts: 149
    • Renat.Su
Re: How to automatically "Assign" an instance to another using RTTI?
« Reply #2 on: November 11, 2019, 07:31:12 pm »
Well You can see an example of copying published properties from one object to another via rtti at https://github.com/risoflora/brookfreepascal/blob/master/core/brookutils.pas
Code: Pascal  [Select]
  1. procedure BrookCopyObject(AFrom, ATo: TObject);

Thaddy

  • Hero Member
  • *****
  • Posts: 9285
Re: How to automatically "Assign" an instance to another using RTTI?
« Reply #3 on: November 12, 2019, 08:02:59 am »
Here's an example that is a bit more transparent. Although for Delphi, also works in Lazarus after conversion.
http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/Classes_TStream_WriteComponent.html
also related to equus asinus.