Archive
Delphi clone object
This is a nice piece of code I found over at Delphi Haven (Chris Rollingston). I have cleaned it up a bit, moved it out of its record type placeholder, and now use it in a baseclass in my toolbox.
Why cloning?
Cloning is something which is very hard to do under Delphi. Other languages like .Net and Java enjoy this en-mass, but it goes without saying that under a virtual language (bytecodes) the assembly information is fairly well evolved compared to a native language. Thankfully, Delphi’s RTTI is getting better and better so we are almost there.
I must have seen hundreds of “deep copy” cloning variations over the years, but this is actually the first one that in my experience is good enough to get the job done. It goes without saying that I’m sure you can compose some class which wont clone perfectly, recursive properties are especially problematic and also nested components or controls. But for plain classes in a hierarchy it works brilliantly and makes my programming life easier.
Note: Just ignore the class structure, just copy the Clone() method to your own class.
type TMSPersistent = Class(TMSObject,ISerializable) strict protected (* ISerializable *) function GetObjectData:TMSSerializationInfo;virtual; public function Clone(const Parameters:Array of TValue): T; end; function TMSPersistent.Clone(const Parameters:Array of TValue): T; var Context: TRttiContext; IsComponent, LookOutForNameProp: Boolean; RttiType: TRttiType; Method: TRttiMethod; MinVisibility: TMemberVisibility; Params: TArray; Prop: TRttiProperty; SourceAsPointer, ResultAsPointer: Pointer; begin RttiType := Context.GetType(self.ClassType); isComponent := self.InheritsFrom(TComponent); for Method in RttiType.GetMethods do Begin if Method.IsConstructor then begin Params := Method.GetParameters; if Params = nil then Break; if (Length(Params) = 1) and IsComponent and (Params[0].ParamType is TRttiInstanceType) and SameText(Method.Name, 'Create') then Break; end; end; if Method<>NIL then Result := Method.Invoke(self.ClassType, Parameters).AsType else Raise Exception.Create('No constructor found error'); try if self.InheritsFrom(TControl) then TControl(Result).Parent := TControl(self).Parent; Move(self, SourceAsPointer, SizeOf(Pointer)); Move(Result, ResultAsPointer, SizeOf(Pointer)); LookOutForNameProp := IsComponent and (TComponent(self).Owner <> nil); if IsComponent then MinVisibility := mvPublished else MinVisibility := mvPublic; for Prop in RttiType.GetProperties do begin if (Prop.Visibility >= MinVisibility) and Prop.IsReadable and Prop.IsWritable then Begin if LookOutForNameProp and (Prop.Name = 'Name') and (Prop.PropertyType is TRttiStringType) then LookOutForNameProp := False else Prop.SetValue(ResultAsPointer, Prop.GetValue(SourceAsPointer)); end; end; except Result.Free; raise; end; end;
Using the method
The method allows you to clone any object, and you can also provide parameters for the constructor (if any). It works like this:
type //Inherit from an object which has the clone method TMyClass = Class(TMSPersistent) private FText: String; public Property Text:String read FText write FText; end; Procedure TForm1.Button1Click(sender:TObject); var first, second:TMyClass; begin First:=TMyClass.Create; First.Text:='This is the first object'; Second:=First.Clone<TMyClass>([]); showmessage(second.Text); end;
In the above code we simply ship in the parameters as an empty array []. This is because TMyClass doesnt have any parameters for it’s constructor.
Had it been a TComponent you would have done something like this:
Second:=First.Clone<TMyClass>([first.Owner]);
Recent
The vatican vault
- March 2023
- February 2023
- December 2022
- October 2022
- January 2022
- October 2021
- March 2021
- November 2020
- September 2020
- July 2020
- June 2020
- April 2020
- March 2020
- February 2020
- January 2020
- November 2019
- October 2019
- September 2019
- August 2019
- July 2019
- June 2019
- May 2019
- April 2019
- March 2019
- February 2019
- January 2019
- December 2018
- November 2018
- October 2018
- September 2018
- August 2018
- July 2018
- June 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- December 2017
- November 2017
- October 2017
- August 2017
- July 2017
- June 2017
- May 2017
- April 2017
- March 2017
- February 2017
- January 2017
- December 2016
- November 2016
- October 2016
- September 2016
- August 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- January 2016
- December 2015
- November 2015
- October 2015
- September 2015
- August 2015
- June 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- November 2014
- October 2014
- September 2014
- August 2014
- July 2014
- June 2014
- May 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- August 2013
- July 2013
- June 2013
- May 2013
- February 2013
- August 2012
- June 2012
- May 2012
- April 2012
You must be logged in to post a comment.