 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ignacio Vazquez Guest
|
Posted: Sun Nov 09, 2003 4:19 am Post subject: TCollectionItem and polymorphism |
|
|
I have a TCollection descendant that contains a TCollectionItem descendant,
e.g., TMyItem. Now, TMyItem is the parent to a couple of separate items,
e.g., TMyFirstItem and TMySecondItem. I've now come to writing the Assign
method for TMyItem et al, and I'm trying to think of an elegant way to do so
whereby:
1) TMyItem is responsible for its properties,
2) the descendants are responsible for their properties, and
3) the various descendants are _not_ assignable to each other.
Any ideas?
Also, is there a built-in property editor for this type of collection or
would I have to write my own?
Thanks,
Ignacio
|
|
| Back to top |
|
 |
Rob Kennedy Guest
|
Posted: Sun Nov 09, 2003 5:12 am Post subject: Re: TCollectionItem and polymorphism |
|
|
Ignacio Vazquez wrote:
| Quote: | I have a TCollection descendant that contains a TCollectionItem
descendant, e.g., TMyItem. Now, TMyItem is the parent to a couple of
separate items, e.g., TMyFirstItem and TMySecondItem.
|
I hope you know what you're getting into. TCollection is not designed to
hold heterogeneous classes. TCollection.Add is not virtual; it always
creates items determined by the TCollectionItemClass you specify in the
collection's constructor. This means that you cannot use the Form
Designer to edit the collection, and you cannot use the streaming system
to load a property of your collection type. Since this rules out any
design-time benefits of TCollection, you might be better off designing
your own container class, which can accomodate polymorphic contents.
| Quote: | I've now come
to writing the Assign method for TMyItem et al, and I'm trying to
think of an elegant way to do so whereby:
1) TMyItem is responsible for its properties,
2) the descendants are responsible for their properties, and
3) the various descendants are _not_ assignable to each other.
|
Define a protected method in TMyItem that assigns TMyItem's properties.
(Call it AssignBaseProperties, say, and don't make it virtual.) Call it
from within the descendents' Assign methods. You won't override
TMyItem.Assign in this case since you want to preserve the default
error-raising code from TPersistent.
Each of the descendents will override Assign like this:
procedure TMyFirstItem.Assign(Source: TPersistent);
begin
if Source is TMyFirstItem then begin
AssignBaseProperties(Source);
// Assign TMyFirstItem properties, etc.
end else inherited;
end;
Calling Inherited will propagate the Assign call down to TPersistent and
then down Source's AssignTo chain, which will eventually raise an
exception and prevent the assignment.
Another strategy would have you overriding TMyItem.Assign and doing an
explicit check that the source and destination are of the same type. I
suppose this strategy might be better since it would simplify the
descendents' assignment code, but I'm not as sure about the ClassType
check below.
procedure TMyItem.Assign(Source: TPersistent);
begin
if ClassType = Source.ClassType then begin
// Assign properties, etc.
end else inherited;
end;
procedure TMyFirstItem.Assign(Source: TPersistent);
begin
inherited;
if Source is TMyFirstItem then begin
// Assign properties, etc.
end;
end;
If you try calling MyFirstItem.Assign(MySecondItem), then the inherited
Assign call will eventually catch the type mismatch.
--
Rob
|
|
| Back to top |
|
 |
Ignacio Vazquez Guest
|
Posted: Sun Nov 09, 2003 5:53 am Post subject: Re: TCollectionItem and polymorphism |
|
|
Hello, Rob!
You wrote on Sat, 08 Nov 2003 23:12:18 -0600:
| Quote: | Ignacio Vazquez wrote:
I have a TCollection descendant that contains a TCollectionItem
descendant, e.g., TMyItem. Now, TMyItem is the parent to a couple of
separate items, e.g., TMyFirstItem and TMySecondItem.
I hope you know what you're getting into. TCollection is not designed to
hold heterogeneous classes. TCollection.Add is not virtual; it always
creates items determined by the TCollectionItemClass you specify in the
collection's constructor.
|
Yes, that would be problematic.
| Quote: | This means that you cannot use the Form
Designer to edit the collection,
and you cannot use the streaming system
to load a property of your
collection type. Since this rules out any
design-time benefits of
TCollection, you might be better off designing
your own container class,
which can accomodate polymorphic contents.
|
I believe that I can do that. It'll take some time, but it is doable.
I suppose in creating the polymorphic collection I can also do a polymorphic
collection item which could do the Assign cleanly.
Thank you for the ideas.
Cheers,
Ignacio
|
|
| Back to top |
|
 |
Constantine Yannakopoulos Guest
|
Posted: Mon Nov 10, 2003 10:29 am Post subject: Re: TCollectionItem and polymorphism |
|
|
Ignacio,
| Quote: | I suppose in creating the polymorphic collection I can also do a
polymorphic collection item which could do the Assign cleanly.
|
Instead of creating a polymorphic collection consider the other
"container" strategy: subcomponents. An implementation similar of
TAction/TActionList should not be very difficult to implement. The most
troublesome part should be the design-time component editor <g>.
--
Constantine
|
|
| Back to top |
|
 |
Ignacio Vazquez Guest
|
Posted: Mon Nov 10, 2003 3:35 pm Post subject: Re: TCollectionItem and polymorphism |
|
|
"Constantine Yannakopoulos" <kyan (AT) deltasingular (DOT) remove.this.gr> wrote in
message [email]3faf76b6 (AT) newsgroups (DOT) borland.com[/email]...
| Quote: | Ignacio,
I suppose in creating the polymorphic collection I can also do a
polymorphic collection item which could do the Assign cleanly.
Instead of creating a polymorphic collection consider the other
"container" strategy: subcomponents. An implementation similar of
TAction/TActionList should not be very difficult to implement.
|
....
You'll have to explain a little clearer; it's not sticking this morning :)
| Quote: | The most
troublesome part should be the design-time component editor
g>.
|
Yes, but I'd have to write that regardless.
Actually, I'm about 80% done with the runtime part of TPolyCollection and
TPolyCollectionItem (just debugging left, I think...), but I haven't done
the property editor yet. Would anybody be interested in taking a look at the
whole thing once I've done it?
Cheers,
Ignacio
--
The strange part isn't so much that he had an accent. No accent was
detectable. It was just sounds and burbs and gurgles coming from him. He
was a like a chubby, old R2-D2.
- La Üter
|
|
| Back to top |
|
 |
Constantine Yannakopoulos Guest
|
Posted: Tue Nov 11, 2003 8:59 am Post subject: Re: TCollectionItem and polymorphism |
|
|
Author := "Ignacio Vazquez";
| Quote: | You'll have to explain a little clearer; it's not sticking this
morning
|
Instead of creating a collection, a "header" collection item and a set
of "detail" TPersistents (at least that's what i understood <g>) you
could have the heterogenous items as subcomponents of your component
(no collection necessary). You will have to implement a way of keeping
track of them in your component (a plain TList will do) and override
some methods like DefineChildren(), Get/SetChildOrder and
GetParentComponent to make it work. TAction/TActionList is a perfect
example of this.
BTW, you haven't told us anything about what you are specificly trying
to implement, so maybe my suggestion is not applicable. Care to
elaborate a bit?
| Quote: | Yes, but I'd have to write that regardless.
|
$(Delphi)SourceProperty EditorsColnEdit.pas contains the source of
the standard collection editor.
| Quote: | Would anybody be interested in
taking a look at the whole thing once I've done it?
|
Sure <g>
--
Constantine
|
|
| Back to top |
|
 |
Ignacio Vazquez Guest
|
Posted: Tue Nov 11, 2003 3:31 pm Post subject: Re: TCollectionItem and polymorphism |
|
|
"Constantine Yannakopoulos" <kyan (AT) deltasingular (DOT) remove.this.gr> wrote in
message [email]3fb0b319 (AT) newsgroups (DOT) borland.com[/email]...
| Quote: | Author := "Ignacio Vazquez";
Instead of creating a collection, a "header" collection item and a set
of "detail" TPersistents (at least that's what i understood <g>) you
could have the heterogenous items as subcomponents of your component
(no collection necessary). You will have to implement a way of keeping
track of them in your component (a plain TList will do) and override
some methods like DefineChildren(), Get/SetChildOrder and
GetParentComponent to make it work. TAction/TActionList is a perfect
example of this.
|
Yes, that is a good idea.
| Quote: | BTW, you haven't told us anything about what you are specificly trying
to implement, so maybe my suggestion is not applicable. Care to
elaborate a bit?
|
I can't at this point. All I can say is that I'm working on a custom
component for work and I need a collection that can hold a heterogenous set
of objects.
However, I thought about it a bit and I've realized that there's a fixed
number of types that I need to support so I'll just bite the bullet and use
multiple collections.
| Quote: | Yes, but I'd have to write that regardless.
$(Delphi)SourceProperty EditorsColnEdit.pas contains the source of
the standard collection editor.
|
Yes, I saw that, thanks.
| Quote: | Would anybody be interested in
taking a look at the whole thing once I've done it?
Sure
|
I have to put it aside for the moment, but once I've finished my current
project I'll look at finishing the property editor, plus it still needs a
tweak or two.
Cheers,
Ignacio
--
The strange part isn't so much that he had an accent. No accent was
detectable. It was just sounds and burbs and gurgles coming from him. He
was a like a chubby, old R2-D2.
- La Üter
|
|
| Back to top |
|
 |
Robert Cerny Guest
|
Posted: Wed Nov 12, 2003 11:34 am Post subject: Re: TCollectionItem and polymorphism |
|
|
"Ignacio Vazquez" <ivazquezATorioncommunications.com> wrote
| Quote: | Instead of creating a polymorphic collection consider the other
"container" strategy: subcomponents. An implementation similar of
TAction/TActionList should not be very difficult to implement.
...
You'll have to explain a little clearer; it's not sticking this morning :)
|
Actionlist is a container for various actions, each action can be different
class, but all have common ancestor. It also provides action class
registration, so that component editor can create action items of any
registered class.
Similar, but more limited example is TDataset which is container for TField
items.
--
Robert Cerny
DelphiShaman
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|