Board index » delphi » enumerated types in value type framework

enumerated types in value type framework


2005-07-28 10:48:23 PM
delphi190
We are working on converting our opf to a value type framework. So
instead of
FString : string;
FBoolean : boolean;
we will have
FString : TOurString;
FBoolean : TOurBoolean;
and so on, where
TOurString = class(TOurDataType)
TOurBoolean = class(TOurDataType)
These are instantiated like (we do not use interfaces)
FString := TOurString.Create;
We can then go
FString.Value = 'my string';
FBoolean.Value = True;
I'm trying to figure out how to deal with enumerated types. Currently we
have the standard
FEnum : TSomeEnum;
where TSomeEnum = (etYou,etMe,etEverybody);
I want to be able to do :
FEnum : TOurEnum;
where
TOurEnum = class(TOurDataType)
and instantiate like
FEnum := TOurEnum.Create(TSomeEnum);
and then be able to go
FEnum.Value := etYou;
Any ideas on how to setup TOurEnum so it can cope with this?
Thanks, Tom.
 
 

Re:enumerated types in value type framework

Tom Corcoran <XXXX@XXXXX.COM>wrote in news:42e8f036
$XXXX@XXXXX.COM:
Quote
Any ideas on how to setup TOurEnum so it can cope with this?
see GetEnumProp, SetEnumProp, GetOrdProp, SetOrdProp, GetPropInfo.
in the TypInfo unit.
cc.borland.com/Item.aspx
see Properties.pas for an idea on how to use them.
--
Iman
 

Re:enumerated types in value type framework

"Tom Corcoran" <XXXX@XXXXX.COM>a écrit dans le
message de news: 42e8f036$XXXX@XXXXX.COM...
Quote
Any ideas on how to setup TOurEnum so it can cope with this?
I use this kind of enum for Aspects of the Observer pattern :
type
// base class
TAspect = class(TOurDataType)
private
fName: string;
protected
constructor Create(const Name: string); overload;
function CompareTo(const Other: TAspect): Boolean; virtual;
function GetName: string; virtual;
public
constructor Create; overload;
end;
// example Selection Aspect
TSelectionAspectEnum = (selEmpty, selSingle, selMultiple);
TSelectionAspect = class(TAspect)
public
class function Empty: TSelectionAspect;
class function Single: TSelectionAspect;
class function Multiple: TSelectionAspect;
private
fEnumValue: TSelectionAspectEnum;
constructor Create(EnumValue: TSelectionAspectEnum);
public
function EnumValue: TSelectionAspectEnum;
end;
implementation
constructor TAspect.Create;
begin
Create('');
end;
constructor TAspect.Create(const Name: string);
begin
inherited Create;
fName := Name;
end;
function TAspect.CompareTo(const Other: TAspect): Boolean;
begin
Result := Other = self;
if not Result then
Result := Other.GetName = fName;
end;
function TAspect.GetName: string;
begin
Result := fName;
end;
constructor TSelectionAspect.Create(EnumValue: TSelectionAspectEnum);
begin
inherited Create(GetEnumName(TypeInfo(TSelectionAspectEnum),
Ord(EnumValue)));
fEnumValue := EnumValue;
end;
function TSelectionAspect.EnumValue: TSelectionAspectEnum;
begin
Result := fEnumValue;
end;
class function TSelectionAspect.Empty: TSelectionAspect;
begin
Result := Create(selEmpty);
end;
class function TSelectionAspect.Single: TSelectionAspect;
begin
Result := Create(selSingle);
end;
class function TSelectionAspect.Multiple: TSelectionAspect;
begin
Result := Create(selMultiple);
end;
//////////////////////
OF course, you also have to consider that without interfaces, you would be
better off making the class functions return a Singleton instance to avoid
memory leaks, cleaning up in the finalisation section of the unit.
Joanna
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
 

Re:enumerated types in value type framework

"Tom Corcoran" <XXXX@XXXXX.COM>writes
Quote
I want to be able to do :
FEnum : TOurEnum;
where
TOurEnum = class(TOurDataType)
and instantiate like
FEnum := TOurEnum.Create(TSomeEnum);

and then be able to go
FEnum.Value := etYou;
How do you know what type of Enum is assignable to FEnum?
We created new field objects for each enum:
TOurEnum = class(TOurDataType)
TEnumA = class(TOurEnum)
TEnumB = class(TourEnum)
etc.
I can see where that would be cumbersome if you use a lot of enums, but it
seems to work for us.
 

Re:enumerated types in value type framework

Thanks for the replies. Priorities changed their for a while.
Joanna Carter (TeamB) writes:
Quote
"Tom Corcoran" a écrit dans...
>We are working on converting our opf to a value type framework.
>I'm trying to figure out how to deal with enumerated types.
I use this kind of enum for Aspects of the Observer pattern :
constructor TSelectionAspect.Create(EnumValue: TSelectionAspectEnum);
begin
inherited Create(GetEnumName(TypeInfo(TSelectionAspectEnum),
Ord(EnumValue)));
fEnumValue := EnumValue;
end;
Ok. I think this create is if you want to initialise TSelectionAspect?
Quote
class function TSelectionAspect.Empty: TSelectionAspect;
begin
Result := Create(selEmpty);
end;
Perhaps this is overly complicated for my case. Why would I create an
instance of TAspect to access an EnumValue? Couldn't it be "self
contained" by combining this idea with Scotts...
Scott Roberts writes:
Quote
How do you know what type of Enum is assignable to FEnum?
We created new field objects for each enum:

TOurEnum = class(TOurDataType)
TEnumA = class(TOurEnum)
TEnumB = class(TourEnum)
That's the problem allright. I guess there is no way around having to
have a new class for each enum.
So I could do :
TEnumAEnum = (firstEnum,secondEnum,thirdEnum);
TEnumA = class(TOurEnum)
private
FEnumValue : TEnumAEnum;
public
property Value : TEnumAEnum read FEnumValue write FEnumValue;
end;
Thanks everyone.
Tom.
 

Re:enumerated types in value type framework

Iman L Crawford writes:
Quote
Tom Corcoran <XXXX@XXXXX.COM>wrote in news:42e8f036
$XXXX@XXXXX.COM:

>Any ideas on how to setup TOurEnum so it can cope with this?


see GetEnumProp, SetEnumProp, GetOrdProp, SetOrdProp, GetPropInfo.
in the TypInfo unit.

cc.borland.com/Item.aspx
see Properties.pas for an idea on how to use them.
Hi Iman,
Your post reminded me of that unit, used before but forgotten about,
cheers. In my TOurEnum class I set up a
FEnumType : PTypeInfo;
constructor Create(EnumType : PTypeInfo); overload;
to call it I have to do
TOurEnum.Create(TypInfo((TSomeEnum)));
where TSomeEnum = (etYou,etMe,etEverybody);
Is there any other way around having to do a cast like this I wonder?
Thanks, Tom.
 

Re:enumerated types in value type framework

Tom Corcoran <XXXX@XXXXX.COM>wrote in
Quote
Is there any other way around having to do a cast like this I wonder?
I don't think so, at least all of the examples I have seen do a typecast.
--
Iman