Re: [fpc-pascal] Clone a instance of object

2013-09-23 Thread Zaher Dirkey
On Mon, Sep 23, 2013 at 4:28 PM, Zaher Dirkey wrote: > ​The idea is simply than needs to use a stream or RTTI, it is like a > record, if looked at an object like as a record but it is inheritable. > ​Compile can now every object body, and do the assigning as record assign. > ​ > That's the featur

Re: [fpc-pascal] Clone a instance of object

2013-09-23 Thread Zaher Dirkey
On Mon, Sep 23, 2013 at 12:58 PM, Graeme Geldenhuys wrote: > > Also be careful about the pitfalls of cloning a class. eg: How you want > to handle more complex cases. eg: Cloning a class with a reference to > another class, or cloning a class with an embedded class (field), etc... > tiOPF's imple

Re: [fpc-pascal] Clone a instance of object

2013-09-23 Thread Graeme Geldenhuys
On 23/09/13 10:46, Michael Van Canneyt wrote: > I've done this several times, it works well for most situations. My I also suggest Zaher takes a look at tiOPF's code to see how they implement class cloning. Also be careful about the pitfalls of cloning a class. eg: How you want to handle more com

Re: [fpc-pascal] Clone a instance of object

2013-09-23 Thread Sven Barth
Am 23.09.2013 14:23, schrieb patspiper: On 23/09/13 14:57, Michael Van Canneyt wrote: On Mon, 23 Sep 2013, Mattias Gaertner wrote: On Mon, 23 Sep 2013 11:46:54 +0200 (CEST) Michael Van Canneyt wrote: On Mon, 23 Sep 2013, Zaher Dirkey wrote: On Mon, Sep 23, 2013 at 10:02 AM, Michael V

Re: [fpc-pascal] Clone a instance of object

2013-09-23 Thread Michael Van Canneyt
On Mon, 23 Sep 2013, Frederic Da Vitoria wrote: Yes. But that works only for 'simple' types and still requires you to write code. For the more general case where you can have arrays, classes, interfaces and whatnot as fields of your object, there is simply no correct way. Isn't there? Ma

Re: [fpc-pascal] Clone a instance of object

2013-09-23 Thread Frederic Da Vitoria
2013/9/23 Michael Van Canneyt > > On Mon, 23 Sep 2013, Mattias Gaertner wrote: > > On Mon, 23 Sep 2013 11:46:54 +0200 (CEST) >> Michael Van Canneyt wrote: >> >> On Mon, 23 Sep 2013, Zaher Dirkey wrote: >>> >>> On Mon, Sep 23, 2013 at 10:02 AM, Michael Van Canneyt < mich...@freepascal.org

Re: [fpc-pascal] Clone a instance of object

2013-09-23 Thread patspiper
On 23/09/13 14:57, Michael Van Canneyt wrote: On Mon, 23 Sep 2013, Mattias Gaertner wrote: On Mon, 23 Sep 2013 11:46:54 +0200 (CEST) Michael Van Canneyt wrote: On Mon, 23 Sep 2013, Zaher Dirkey wrote: On Mon, Sep 23, 2013 at 10:02 AM, Michael Van Canneyt wrote: Var C : TBaseObjectC

Re: [fpc-pascal] Clone a instance of object

2013-09-23 Thread Michael Van Canneyt
On Mon, 23 Sep 2013, Mattias Gaertner wrote: On Mon, 23 Sep 2013 11:46:54 +0200 (CEST) Michael Van Canneyt wrote: On Mon, 23 Sep 2013, Zaher Dirkey wrote: On Mon, Sep 23, 2013 at 10:02 AM, Michael Van Canneyt wrote: Var   C : TBaseObjectClass; begin   C:=TBase

Re: [fpc-pascal] Clone a instance of object

2013-09-23 Thread Mattias Gaertner
On Mon, 23 Sep 2013 12:10:41 +0200 Frederic Da Vitoria wrote: > 2013/9/23 Mattias Gaertner > > > On Mon, 23 Sep 2013 11:46:54 +0200 (CEST) > > Michael Van Canneyt wrote: > > > > > > > > > > > On Mon, 23 Sep 2013, Zaher Dirkey wrote: > > > > > > > > > > > On Mon, Sep 23, 2013 at 10:02 AM, Micha

Re: [fpc-pascal] Clone a instance of object

2013-09-23 Thread Mattias Gaertner
On Mon, 23 Sep 2013 13:13:27 +0300 Zaher Dirkey wrote: > On Mon, Sep 23, 2013 at 12:57 PM, Mattias Gaertner < > nc-gaert...@netcologne.de> wrote: > > > type > > TMyClass = class(TPersistent) > > private > >type > > TMyClassVars = record > >a,b,c: string; > > end; > > pri

Re: [fpc-pascal] Clone a instance of object

2013-09-23 Thread Zaher Dirkey
On Mon, Sep 23, 2013 at 12:57 PM, Mattias Gaertner < nc-gaert...@netcologne.de> wrote: > type > TMyClass = class(TPersistent) > private >type > TMyClassVars = record >a,b,c: string; > end; > private >F: TMyClassVars; > public >procedure Assign(Source: TPersistent)

Re: [fpc-pascal] Clone a instance of object

2013-09-23 Thread Frederic Da Vitoria
2013/9/23 Mattias Gaertner > On Mon, 23 Sep 2013 11:46:54 +0200 (CEST) > Michael Van Canneyt wrote: > > > > > > > On Mon, 23 Sep 2013, Zaher Dirkey wrote: > > > > > > > > On Mon, Sep 23, 2013 at 10:02 AM, Michael Van Canneyt < > mich...@freepascal.org> wrote: > > > Var > > > C : TB

Re: [fpc-pascal] Clone a instance of object

2013-09-23 Thread Mattias Gaertner
On Mon, 23 Sep 2013 11:46:54 +0200 (CEST) Michael Van Canneyt wrote: > > > On Mon, 23 Sep 2013, Zaher Dirkey wrote: > > > > > On Mon, Sep 23, 2013 at 10:02 AM, Michael Van Canneyt > > wrote: > > Var > >   C : TBaseObjectClass; > > begin > >   C:=TBaseObjectClass(Clas

Re: [fpc-pascal] Clone a instance of object

2013-09-23 Thread Michael Van Canneyt
On Mon, 23 Sep 2013, Zaher Dirkey wrote: On Mon, Sep 23, 2013 at 12:46 PM, Michael Van Canneyt wrote: There is no such safe mechanism. The best you can do is make the properties published, and write an RTTI lookup routine that copies all Published values. I've done this

Re: [fpc-pascal] Clone a instance of object

2013-09-23 Thread Zaher Dirkey
On Mon, Sep 23, 2013 at 12:46 PM, Michael Van Canneyt < mich...@freepascal.org> wrote: > There is no such safe mechanism. > > The best you can do is make the properties published, and write an RTTI > lookup routine that copies all Published values. > I've done this several times, it works well for

Re: [fpc-pascal] Clone a instance of object

2013-09-23 Thread Michael Van Canneyt
On Mon, 23 Sep 2013, Zaher Dirkey wrote: On Mon, Sep 23, 2013 at 10:02 AM, Michael Van Canneyt wrote: Var   C : TBaseObjectClass; begin   C:=TBaseObjectClass(ClassType);   Result:=C.Create; end; ​Yes, that help me, thanks. Now about assigning I must re

Re: [fpc-pascal] Clone a instance of object

2013-09-23 Thread Zaher Dirkey
On Mon, Sep 23, 2013 at 10:02 AM, Michael Van Canneyt < mich...@freepascal.org> wrote: > Var > C : TBaseObjectClass; > begin > C:=TBaseObjectClass(ClassType)**; > Result:=C.Create; > end; > ​Yes, that help me, thanks. Now about assigning I must repeat it (assign) for every child class copy

Re: [fpc-pascal] Clone a instance of object

2013-09-23 Thread Michael Van Canneyt
On Mon, 23 Sep 2013, patspiper wrote: On 23/09/13 10:02, Michael Van Canneyt wrote: This is wrong. You are typecasting a class reference to an object instance. The following works: {$mode objfpc} Type TBaseObject = class(TObject) public constructor Create; virtual; function Clone

Re: [fpc-pascal] Clone a instance of object

2013-09-23 Thread patspiper
On 23/09/13 10:02, Michael Van Canneyt wrote: This is wrong. You are typecasting a class reference to an object instance. The following works: {$mode objfpc} Type TBaseObject = class(TObject) public constructor Create; virtual; function Clone:TBaseObject; end; TBaseObjectClass

Re: [fpc-pascal] Clone a instance of object

2013-09-23 Thread Michael Van Canneyt
On Mon, 23 Sep 2013, Zaher Dirkey wrote: Hi, I have objects (classes) derived from base one TBaseObject = class(TObject) public    constructor Create; virtual;    function Clone:TBaseObject; end; TMyObject = class(TBaseObject)    my fields here end; o1, o2:TmyObject; o1  already created; o

Re: [fpc-pascal] Clone a instance of object

2013-09-22 Thread Zaher Dirkey
On Mon, Sep 23, 2013 at 8:50 AM, Sven Barth wrote: > You need to add a "Result.Assign(Self)" to your clone call and implement > a virtually inherited "Assign" method in each class where you copy all > fields. This is how "TPersistent" and its descendants do it as well. ​Yes I added assign, but a

Re: [fpc-pascal] Clone a instance of object

2013-09-22 Thread Sven Barth
Am 23.09.2013 07:15 schrieb "Zaher Dirkey" : > > Hi, > I have objects (classes) derived from base one > > TBaseObject = class(TObject) > public >constructor Create; virtual; >function Clone:TBaseObject; > end; > > TMyObject = class(TBaseObject) >my fields here > end; > > o1, o2:TmyObjec