Recent

Author Topic: [SOLVED] Asking for an example of an object cloning itself  (Read 2622 times)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1353
  • Professional amateur ;-P
[SOLVED] Asking for an example of an object cloning itself
« on: July 15, 2025, 08:29:20 pm »
Hey Y'All,

I always mess it up when using any type of non generics object list. My favourite is TFPObjectList from Contnrs cuz it has no notification events.
So, a thought came to mind: Wouldn't it be nice if when we ask for owned items, the list would clone the object and then we could do this:
Code: Pascal  [Select][+][-]
  1. MyObjectVar:= TMyObjectClass.Create;
  2. // Set some stuff on MyObjectVar's properties
  3. MyObjectList.Add(MyObjectVar); // Store it away in a list that owns it's items
  4. MyObjectVar.Free; // Free it so we don't have to worry about it

I'm kinda guessing that this would involve RTTI, and everyone tries to avoid it if they can. It bloats everything up down the line.

Then I thought: This is a great question to ask on the forums!! And here I am!!
  • Would a self cloning Class be possible without RTTI? If so, please provide code example.
  • If not, lets think with RTTI! Please provide code example.

Cheers,
Gus
« Last Edit: July 17, 2025, 04:50:04 pm by Gustavo 'Gus' Carreno »

paule32

  • Hero Member
  • *****
  • Posts: 645
  • One in all. But, not all in one.
Re: Asking for an example of an object cloning itself
« Reply #1 on: July 15, 2025, 08:39:39 pm »
you can inspect the SYSTEM.PAS Module ...
and you can discover a struct that holds VMT Fields that are used for RTTI - which are important Thing of FPC Runtime (Compiler) and the Runtime Library (RTL) itself.

The FPC Runtime and the RTL are essential Things in / within the FP Compiler and can not be simple replace.

You have to deal with a bunch of COMPILERPROC edure's you must have and this is not enough.
These COMPILERPROC's must be occur in a voodoo Order in Modules and Positions.
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6393
  • Compiler Developer
Re: Asking for an example of an object cloning itself
« Reply #2 on: July 15, 2025, 09:15:01 pm »
Then I thought: This is a great question to ask on the forums!! And here I am!!
  • Would a self cloning Class be possible without RTTI? If so, please provide code example.
  • If not, lets think with RTTI! Please provide code example.

It's not even (safely) possible with RTTI, because some internal state might not be exposed through a published property.

The way to do this is to have a base class that provides some kind of virtual Assign method that does the dirty work of copying the object instance. This way is also used in the RTL with TPersistent.

you can inspect the SYSTEM.PAS Module ...
and you can discover a struct that holds VMT Fields that are used for RTTI - which are important Thing of FPC Runtime (Compiler) and the Runtime Library (RTL) itself.

The FPC Runtime and the RTL are essential Things in / within the FP Compiler and can not be simple replace.

You have to deal with a bunch of COMPILERPROC edure's you must have and this is not enough.
These COMPILERPROC's must be occur in a voodoo Order in Modules and Positions.

All that is not relevant for the topic at hand. You don't need to shoot with cannons at sparrows for cloning objects.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1353
  • Professional amateur ;-P
Re: Asking for an example of an object cloning itself
« Reply #3 on: July 15, 2025, 09:15:43 pm »
Hey paule32,

you can inspect the SYSTEM.PAS Module ...
and you can discover a struct that holds VMT Fields that are used for RTTI - which are important Thing of FPC Runtime (Compiler) and the Runtime Library (RTL) itself.

The FPC Runtime and the RTL are essential Things in / within the FP Compiler and can not be simple replace.

You have to deal with a bunch of COMPILERPROC edure's you must have and this is not enough.
These COMPILERPROC's must be occur in a voodoo Order in Modules and Positions.

Don't get me wrong, I do appreciate your insights, quite a lot...

But... Where's the code ?!?! :D

Cheers,
Gus

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1353
  • Professional amateur ;-P
Re: Asking for an example of an object cloning itself
« Reply #4 on: July 15, 2025, 09:26:15 pm »
Hey PascalDragon,

Missed your, always very welcomed, insights!! Hope you're doing well !!

Then I thought: This is a great question to ask on the forums!! And here I am!!
  • Would a self cloning Class be possible without RTTI? If so, please provide code example.
  • If not, lets think with RTTI! Please provide code example.

It's not even (safely) possible with RTTI, because some internal state might not be exposed through a published property.

The way to do this is to have a base class that provides some kind of virtual Assign method that does the dirty work of copying the object instance. This way is also used in the RTL with TPersistent.

Okidokes !! So, for code examples have a look at any Class that sponsors an Assign method, starting with TPersistent and it's ancestor. Gotcha !!!

As per usual, many thanks for dropping your gems, you're the ultimate mother goose :D ( and I say this with the utmost love )

All that is not relevant for the topic at hand. You don't need to shoot with cannons at sparrows for cloning objects.

I'm gonna be VERY rude and laugh my arse off at this :D

Cheers,
Gus

paule32

  • Hero Member
  • *****
  • Posts: 645
  • One in all. But, not all in one.
Re: Asking for an example of an object cloning itself
« Reply #5 on: July 15, 2025, 09:39:01 pm »
@Gus:

I wrote in the Context, that you need a own SYSTEM.PAS Module like me.
With Help of fibodev I have wrote a own SYSTEM.PAS and needed COMPILERPROC edure's - not all, but many ...

So I was thinking, if you wan't to have your own RTTI, you can do this with own RECORD s and own RECORD Fields.

But since the Bootstrap for this contains FPC Original Code, you have a payload of Meta Information's till you broke it down to your need.

But this implicit that you write your own Pascal Compiler...
And this can be a very high risk, and time waste ...
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12845
  • FPC developer.

Thaddy

  • Hero Member
  • *****
  • Posts: 19136
  • Glad to be alive.
Re: Asking for an example of an object cloning itself
« Reply #7 on: July 16, 2025, 07:33:15 pm »
I would write a class function for that.
objects are fine constructs. You can even initialize them with constructors.

Khrys

  • Sr. Member
  • ****
  • Posts: 436
Re: Asking for an example of an object cloning itself
« Reply #8 on: July 17, 2025, 07:21:57 am »
I think a special-purpose method (the approach taken by  TPersistent) might be the only solid option.

Sure, you could do a simple bitwise copy:

Code: Pascal  [Select][+][-]
  1. function TObject.Clone(): TObject;
  2. begin
  3.   TObject(Result) := NewInstance();
  4.   Move(Pointer(Self)^, Pointer(Result)^, InstanceSize());
  5. end;

But this will mess up managed types (strings, arrays etc. will cause use-after-free due to the reference count being too low) and, in the case of references (pointers, class instances) only copies the pointer value (no deep copy).

Even if you handled managed types correctly you'd still have to decide whether to deep-copy references, which might still be somewhat doable with classes (but requiring prior knowledge of deep vs. shallow types) but is completely unfeasible for opaque pointers.

 

TinyPortal © 2005-2018