Clone a instance of object

classic Classic list List threaded Threaded
23 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Clone a instance of object

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:TmyObject;
o1  already created;

o2 := o1.Clone;

in Clone i want to create new object from TmyObject but in base object in Clone method,like this

TBaseObject.Clone:TBaseObject;
begin
  Result:=TBaseObject(ClassType).Create;//I know it is wrong here
end;

A
​ny good idea?​

--
I am using last revision of Lazarus, FPC 2.6 on Windows XP SP3

Best Regards
Zaher Dirkey

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

Sven Barth-2

Am 23.09.2013 07:15 schrieb "Zaher Dirkey" <[hidden email]>:
>
> 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;
>
> o2 := o1.Clone;
>
> in Clone i want to create new object from TmyObject but in base object in Clone method,like this
>
> TBaseObject.Clone:TBaseObject;
> begin
>   Result:=TBaseObject(ClassType).Create;//I know it is wrong here
> end;
>
> A
> ny good idea?

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.

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

zaher dirkey

On Mon, Sep 23, 2013 at 8:50 AM, Sven Barth <[hidden email]> 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 as example above, it is crashed when creating it

Result:=TBaseObject(ClassType).Create;

--
I am using last revision of Lazarus, FPC 2.6 on Windows XP SP3

Best Regards
Zaher Dirkey

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

Michael Van Canneyt
In reply to this post by zaher dirkey


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;
>
> o2 := o1.Clone;
>
> in Clone i want to create new object from TmyObject but in base object in Clone method,like this
>
> TBaseObject.Clone:TBaseObject;
> begin
>   Result:=TBaseObject(ClassType).Create;//I know it is wrong here
> end;
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 = Class of TBaseObject;

constructor TBaseObject.Create;

begin
end;

Function TBaseObject.Clone:TBaseObject;

Var
   C : TBaseObjectClass;
begin
   C:=TBaseObjectClass(ClassType);
   Result:=C.Create;
end;

Type
   TMyObject = class(TBaseObject)
   //   my fields here
   end;

Var
o1, o2:TmyObject;

begin
   o1:=TMyObject.Create;
   o2 := o1.Clone as TmyObject;
end.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

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 = Class of TBaseObject;
>
> constructor TBaseObject.Create;
>
> begin
> end;
>
> Function TBaseObject.Clone:TBaseObject;
>
> Var
>   C : TBaseObjectClass;
> begin
>   C:=TBaseObjectClass(ClassType);
>   Result:=C.Create;
> end;
What would happen if Result := Self.ClassType.Create were used instead?

>
> Type
>   TMyObject = class(TBaseObject)
>   //   my fields here
>   end;
>
> Var
> o1, o2:TmyObject;
>
> begin
>   o1:=TMyObject.Create;
>   o2 := o1.Clone as TmyObject;
> end.

Stephano
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

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:TBaseObject;
>>   end;
>>   TBaseObjectClass = Class of TBaseObject;
>>
>> constructor TBaseObject.Create;
>>
>> begin
>> end;
>>
>> Function TBaseObject.Clone:TBaseObject;
>>
>> Var
>>   C : TBaseObjectClass;
>> begin
>>   C:=TBaseObjectClass(ClassType);
>>   Result:=C.Create;
>> end;
> What would happen if Result := Self.ClassType.Create were used instead?

The wrong constructor would be called.
Classtype is equivalend to 'Class of TObject'.

The typecast is needed to ensure that the TBaseObject.Create constructor is called.
Your basic polymorphism problem...

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

zaher dirkey
In reply to this post by Michael Van Canneyt

On Mon, Sep 23, 2013 at 10:02 AM, Michael Van Canneyt <[hidden email]> 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 its field members, is there any trick to copy the values like assigning record var to another record var?
I know it is kind of strange way :P

​Thanks​

​Zaher Dirkey​


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

Michael Van Canneyt


On Mon, 23 Sep 2013, Zaher Dirkey wrote:

>
> On Mon, Sep 23, 2013 at 10:02 AM, Michael Van Canneyt <[hidden email]> 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 its field members, is there any trick to copy the values like assigning record var to another record
> var?
> I know it is kind of strange way :P
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 most situations.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

zaher dirkey

On Mon, Sep 23, 2013 at 12:46 PM, Michael Van Canneyt <[hidden email]> 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 most situations.

​It is not the fastest way like assigning a record, can we ask it as a feature in FPC ?​

Best Regards
Zaher Dirkey

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

Mattias Gaertner
In reply to this post by Michael Van Canneyt
On Mon, 23 Sep 2013 11:46:54 +0200 (CEST)
Michael Van Canneyt <[hidden email]> wrote:

>
>
> On Mon, 23 Sep 2013, Zaher Dirkey wrote:
>
> >
> > On Mon, Sep 23, 2013 at 10:02 AM, Michael Van Canneyt <[hidden email]> 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 its field members, is there any trick to copy the values like assigning record var to another record
> > var?
> > I know it is kind of strange way :P
>
> There is no such safe mechanism.

Well, actually there is a safe mechanism. You use a record to store the
class variables. But it looks clumsily:

type
 TMyClass = class(TPersistent)
 private
   type
     TMyClassVars = record
       a,b,c: string;
     end;
 private
   F: TMyClassVars;
 public
   procedure Assign(Source: TPersistent);
   property a: string read F.a write F.a;
 end;

procedure TMyClass.Assign(Source: TPersistent);
begin
  F:=TMyClass(Source).F;
  inherited;
end;

 
> 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 most situations.

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

Michael Van Canneyt
In reply to this post by zaher dirkey


On Mon, 23 Sep 2013, Zaher Dirkey wrote:

>
> On Mon, Sep 23, 2013 at 12:46 PM, Michael Van Canneyt <[hidden email]> 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 most situations.
>
>
> ​It is not the fastest way like assigning a record, can we ask it as a feature in FPC ?​

It's impossible to do correctly.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

Graeme Geldenhuys-3
In reply to this post by Michael Van Canneyt
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 complex cases. eg: Cloning a class with a reference to
another class, or cloning a class with an embedded class (field), etc...
tiOPF's implementation allows the developer to decided how such cases
should be handled.

Regard,
  Graeme.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

Frederic Da Vitoria
In reply to this post by Mattias Gaertner
2013/9/23 Mattias Gaertner <[hidden email]>
On Mon, 23 Sep 2013 11:46:54 +0200 (CEST)
Michael Van Canneyt <[hidden email]> wrote:

>
>
> On Mon, 23 Sep 2013, Zaher Dirkey wrote:
>
> >
> > On Mon, Sep 23, 2013 at 10:02 AM, Michael Van Canneyt <[hidden email]> 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 its field members, is there any trick to copy the values like assigning record var to another record
> > var?
> > I know it is kind of strange way :P
>
> There is no such safe mechanism.

Well, actually there is a safe mechanism. You use a record to store the
class variables. But it looks clumsily:

type
 TMyClass = class(TPersistent)
 private
   type
     TMyClassVars = record
       a,b,c: string;
     end;
 private
   F: TMyClassVars;
 public
   procedure Assign(Source: TPersistent);
   property a: string read F.a write F.a;
 end;

procedure TMyClass.Assign(Source: TPersistent);
begin
  F:=TMyClass(Source).F;
  inherited;
end;

This wouldn't clone included objects. I mean: if the object to be cloned contains objects, those included objects should probably be cloned too, but putting them into a record wouldn't achieve that.

--
Frederic Da Vitoria
(davitof)

Membre de l'April - « promouvoir et défendre le logiciel libre » - http://www.april.org

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

zaher dirkey
In reply to this post by Mattias Gaertner

On Mon, Sep 23, 2013 at 12:57 PM, Mattias Gaertner <[hidden email]> wrote:
type
 TMyClass = class(TPersistent)
 private
   type
     TMyClassVars = record
       a,b,c: string;
     end;
 private
   F: TMyClassVars;
 public
   procedure Assign(Source: TPersistent);
   property a: string read F.a write F.a;
 end;

​But in derived classes fields will not assigned​?

Best Regards
Zaher Dirkey

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

Mattias Gaertner
On Mon, 23 Sep 2013 13:13:27 +0300
Zaher Dirkey <[hidden email]> wrote:

> On Mon, Sep 23, 2013 at 12:57 PM, Mattias Gaertner <
> [hidden email]> wrote:
>
> > type
> >  TMyClass = class(TPersistent)
> >  private
> >    type
> >      TMyClassVars = record
> >        a,b,c: string;
> >      end;
> >  private
> >    F: TMyClassVars;
> >  public
> >    procedure Assign(Source: TPersistent);
> >    property a: string read F.a write F.a;
> >  end;
> >
>
> ​But in derived classes fields will not assigned​?

Derived classes can use the same technique.

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

Mattias Gaertner
In reply to this post by Frederic Da Vitoria
On Mon, 23 Sep 2013 12:10:41 +0200
Frederic Da Vitoria <[hidden email]> wrote:

> 2013/9/23 Mattias Gaertner <[hidden email]>
>
> > On Mon, 23 Sep 2013 11:46:54 +0200 (CEST)
> > Michael Van Canneyt <[hidden email]> wrote:
> >
> > >
> > >
> > > On Mon, 23 Sep 2013, Zaher Dirkey wrote:
> > >
> > > >
> > > > On Mon, Sep 23, 2013 at 10:02 AM, Michael Van Canneyt <
> > [hidden email]> 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 its field members, is there any trick to copy the values like
> > assigning record var to another record
> > > > var?
> > > > I know it is kind of strange way :P
> > >
> > > There is no such safe mechanism.
> >
> > Well, actually there is a safe mechanism. You use a record to store the
> > class variables. But it looks clumsily:
> >
> > type
> >  TMyClass = class(TPersistent)
> >  private
> >    type
> >      TMyClassVars = record
> >        a,b,c: string;
> >      end;
> >  private
> >    F: TMyClassVars;
> >  public
> >    procedure Assign(Source: TPersistent);
> >    property a: string read F.a write F.a;
> >  end;
> >
> > procedure TMyClass.Assign(Source: TPersistent);
> > begin
> >   F:=TMyClass(Source).F;
> >   inherited;
> > end;
> >
>
> This wouldn't clone included objects. I mean: if the object to be cloned
> contains objects, those included objects should probably be cloned too, but
> putting them into a record wouldn't achieve that.

Anything that needs code to create needs code to clone.
You can always creates cases where cloning need special code. But if
you limit yourself you can use some of the existing frameworks like
TReader/TWriter of tiopf.

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

Michael Van Canneyt
In reply to this post by Mattias Gaertner


On Mon, 23 Sep 2013, Mattias Gaertner wrote:

> On Mon, 23 Sep 2013 11:46:54 +0200 (CEST)
> Michael Van Canneyt <[hidden email]> wrote:
>
>>
>>
>> On Mon, 23 Sep 2013, Zaher Dirkey wrote:
>>
>>>
>>> On Mon, Sep 23, 2013 at 10:02 AM, Michael Van Canneyt <[hidden email]> 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 its field members, is there any trick to copy the values like assigning record var to another record
>>> var?
>>> I know it is kind of strange way :P
>>
>> There is no such safe mechanism.
>
> Well, actually there is a safe mechanism. You use a record to store the
> class variables. But it looks clumsily:
>
> type
> TMyClass = class(TPersistent)
> private
>   type
>     TMyClassVars = record
>       a,b,c: string;
>     end;
> private
>   F: TMyClassVars;
> public
>   procedure Assign(Source: TPersistent);
>   property a: string read F.a write F.a;
> end;
>
> procedure TMyClass.Assign(Source: TPersistent);
> begin
>  F:=TMyClass(Source).F;
>  inherited;
> end;
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.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

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 <[hidden email]> wrote:
>>
>>>
>>>
>>> On Mon, 23 Sep 2013, Zaher Dirkey wrote:
>>>
>>>>
>>>> On Mon, Sep 23, 2013 at 10:02 AM, Michael Van Canneyt
>>>> <[hidden email]> 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 its field members, is there any trick to copy the values like
>>>> assigning record var to another record
>>>> var?
>>>> I know it is kind of strange way :P
>>>
>>> There is no such safe mechanism.
>>
>> Well, actually there is a safe mechanism. You use a record to store the
>> class variables. But it looks clumsily:
>>
>> type
>> TMyClass = class(TPersistent)
>> private
>> type
>> TMyClassVars = record
>> a,b,c: string;
>> end;
>> private
>> F: TMyClassVars;
>> public
>> procedure Assign(Source: TPersistent);
>> property a: string read F.a write F.a;
>> end;
>>
>> procedure TMyClass.Assign(Source: TPersistent);
>> begin
>> F:=TMyClass(Source).F;
>> inherited;
>> end;
>
> 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.

Can't the clipboard help in cloning? Similar to the IDE's copy and paste.

Stephano
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

Frederic Da Vitoria
In reply to this post by Michael Van Canneyt
2013/9/23 Michael Van Canneyt <[hidden email]>

On Mon, 23 Sep 2013, Mattias Gaertner wrote:

On Mon, 23 Sep 2013 11:46:54 +0200 (CEST)
Michael Van Canneyt <[hidden email]> wrote:

On Mon, 23 Sep 2013, Zaher Dirkey wrote:

On Mon, Sep 23, 2013 at 10:02 AM, Michael Van Canneyt <[hidden email]> 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 its field members, is there any trick to copy the values like assigning record var to another record
var?
I know it is kind of strange way :P

There is no such safe mechanism.

Well, actually there is a safe mechanism. You use a record to store the
class variables. But it looks clumsily:

type
TMyClass = class(TPersistent)
private
  type
    TMyClassVars = record
      a,b,c: string;
    end;
private
  F: TMyClassVars;
public
  procedure Assign(Source: TPersistent);
  property a: string read F.a write F.a;
end;

procedure TMyClass.Assign(Source: TPersistent);
begin
 F:=TMyClass(Source).F;
 inherited;
end;

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? Maybe it wouldn't be easy, but IIRC Delphi offers something related to this with TComponents and Streams. This Delphi mechanism I knew worked only for properties, but I found references that there existed a wider mechanism in D2010.

--
Frederic Da Vitoria
(davitof)

Membre de l'April - « promouvoir et défendre le logiciel libre » - http://www.april.org

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clone a instance of object

Sven Barth-2
In reply to this post by patspiper
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 <[hidden email]> wrote:
>>>
>>>>
>>>>
>>>> On Mon, 23 Sep 2013, Zaher Dirkey wrote:
>>>>
>>>>>
>>>>> On Mon, Sep 23, 2013 at 10:02 AM, Michael Van Canneyt
>>>>> <[hidden email]> 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 its field members, is there any trick to copy the
>>>>> values like assigning record var to another record
>>>>> var?
>>>>> I know it is kind of strange way :P
>>>>
>>>> There is no such safe mechanism.
>>>
>>> Well, actually there is a safe mechanism. You use a record to store the
>>> class variables. But it looks clumsily:
>>>
>>> type
>>> TMyClass = class(TPersistent)
>>> private
>>> type
>>> TMyClassVars = record
>>> a,b,c: string;
>>> end;
>>> private
>>> F: TMyClassVars;
>>> public
>>> procedure Assign(Source: TPersistent);
>>> property a: string read F.a write F.a;
>>> end;
>>>
>>> procedure TMyClass.Assign(Source: TPersistent);
>>> begin
>>> F:=TMyClass(Source).F;
>>> inherited;
>>> end;
>>
>> 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.
>
> Can't the clipboard help in cloning? Similar to the IDE's copy and paste.
Even then you'd need to convert the fields to the format the clipboard
expects. And if you managed to correctly "stream" all properties than
you don't need the clipboard anyway...

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
12