如何实现Type-safe COM枚举?

时间:2021-02-13 16:00:58

How could i implement Type-Safe Enumerations in Delphi in a COM scenario ? Basically, i'd like to replace a set of primitive constants of a enumeration with a set of static final object references encapsulated in a class ? . In Java, we can do something like:

如何在COM场景中实现Delphi中的Type-Safe枚举?基本上,我想用一组封装在类中的静态最终对象引用替换枚举的一组原始常量? 。在Java中,我们可以做类似的事情:

public final class Enum
{
    public static final Enum ENUMITEM1 = new Enum ();
    public static final Enum ENUMITEM2 = new Enum ();
    //...
    private Enum () {}
} 

and make comparisons using the customized enumeration type:

并使用自定义枚举类型进行比较:

if (anObject != Enum.ENUMITEM1) ...

I am currently using the old Delphi 5 and i would like to declare some enums parameters on the interfaces, not allowing that client objects to pass integers (or long) types in the place of the required enumeration type. Do you have a better way of implementing enums other than using the native delphi enums ?

我目前正在使用旧的Delphi 5,我想在接口上声明一些枚举参数,不允许客户端对象在所需的枚举类型的位置传递整数(或长)类型。除了使用本机delphi枚举之外,您是否有更好的方法来实现枚举?

4 个解决方案

#1


1  

Now you have provided us with some more clues about the nature of your question, namely mentioning COM, I think I understand what you mean. COM can marshal only a subset of the types Delphi knows between a COM server and client. You can define enums in the TLB editor, but these are all of the type TOleEnum which basically is an integer type (LongWord). You can have a variable of the type TOleEnum any integer value you want and assign values of different enum types to each other. Not really type safe.

现在你已经为我们提供了一些关于你问题性质的更多线索,即提到COM,我想我明白你的意思。 COM只能编组Delphi在COM服务器和客户端之间知道的部分类型。您可以在TLB编辑器中定义枚举,但这些都是TOleEnum类型,它基本上是整数类型(LongWord)。您可以使用TOleEnum类型的变量获得所需的任何整数值,并为彼此分配不同枚举类型的值。不是真正的类型安全。

I can not think of a reason why Delphi's COM can't use the type safe enums instead, but it doesn't. I am afraid nothing much can be done about that. Maybe the changes in the TLB editor in the upcoming Delphi 2009 version might change that.

我想不出Delphi的COM不能使用类型安全枚举的原因,但事实并非如此。我担心没有什么可以做的。也许在即将到来的Delphi 2009版本中TLB编辑器的变化可能会改变这一点。

For the record: When the TLB editor is not used, Delphi is perfectly able to have interface with methods who have type safe enums as parameters.

对于记录:当不使用TLB编辑器时,Delphi完全能够与具有类型安全枚举作为参数的方法接口。

#2


4  

Native Delphi enumerations are already type-safe. Java enumerations were an innovation for that language, because before it didn't have enumerations at all. However, perhaps you mean a different feature - enumeration values prefixed by their type name.

本机Delphi枚举已经是类型安全的。 Java枚举是该语言的一项创新,因为它之前根本没有枚举。但是,也许您的意思是一个不同的功能 - 枚举值以其类型名称为前缀。

Upcoming Delphi 2009, and the last version of the Delphi for .NET product, support a new directive called scoped enums. It looks like this:

即将推出的Delphi 2009和最新版本的Delphi for .NET产品支持一个名为scoped enums的新指令。它看起来像这样:

{$APPTYPE CONSOLE}
{$SCOPEDENUMS ON}
type
  TFoo = (One, Two, Three);
{$SCOPEDENUMS OFF}

var
  x: TFoo;
begin
  x := TFoo.One;
  if not (x in [TFoo.Two, TFoo.Three]) then
    Writeln('OK');
end.

#3


3  

What is wrong with native Delphi enums? They are type safe.

本机Delphi枚举有什么问题?它们是类型安全的。

type
  TMyEnum = (Item1, Item2, Item3);

if MyEnum <> Item1 then...

Since Delphi 2005 you can have consts in a class, but Delphi 5 can not.

自Delphi 2005以来,你可以在一个类中拥有consts,但是Delphi 5不能。

type
  TMyEnum = sealed class
  public
    const Item1 = 0;
    const Item2 = 1;
    const Item3 = 2;
  end;

#4


1  

I think I know why Borland choose not to use type safe enums in the TLB editor. Enums in COM can be different values while Delphi only since Delphi 6 (I think) can do that.

我想我知道为什么Borland选择不在TLB编辑器中使用类型安全枚举。 COM中的枚举可以是不同的值,而Delphi只有Delphi 6(我认为)才能做到这一点。

type
  TSomeEnum = (Enum1 = 1, Enum2 = 6, Enum3 = 80);  // Only since Delphi 6

#1


1  

Now you have provided us with some more clues about the nature of your question, namely mentioning COM, I think I understand what you mean. COM can marshal only a subset of the types Delphi knows between a COM server and client. You can define enums in the TLB editor, but these are all of the type TOleEnum which basically is an integer type (LongWord). You can have a variable of the type TOleEnum any integer value you want and assign values of different enum types to each other. Not really type safe.

现在你已经为我们提供了一些关于你问题性质的更多线索,即提到COM,我想我明白你的意思。 COM只能编组Delphi在COM服务器和客户端之间知道的部分类型。您可以在TLB编辑器中定义枚举,但这些都是TOleEnum类型,它基本上是整数类型(LongWord)。您可以使用TOleEnum类型的变量获得所需的任何整数值,并为彼此分配不同枚举类型的值。不是真正的类型安全。

I can not think of a reason why Delphi's COM can't use the type safe enums instead, but it doesn't. I am afraid nothing much can be done about that. Maybe the changes in the TLB editor in the upcoming Delphi 2009 version might change that.

我想不出Delphi的COM不能使用类型安全枚举的原因,但事实并非如此。我担心没有什么可以做的。也许在即将到来的Delphi 2009版本中TLB编辑器的变化可能会改变这一点。

For the record: When the TLB editor is not used, Delphi is perfectly able to have interface with methods who have type safe enums as parameters.

对于记录:当不使用TLB编辑器时,Delphi完全能够与具有类型安全枚举作为参数的方法接口。

#2


4  

Native Delphi enumerations are already type-safe. Java enumerations were an innovation for that language, because before it didn't have enumerations at all. However, perhaps you mean a different feature - enumeration values prefixed by their type name.

本机Delphi枚举已经是类型安全的。 Java枚举是该语言的一项创新,因为它之前根本没有枚举。但是,也许您的意思是一个不同的功能 - 枚举值以其类型名称为前缀。

Upcoming Delphi 2009, and the last version of the Delphi for .NET product, support a new directive called scoped enums. It looks like this:

即将推出的Delphi 2009和最新版本的Delphi for .NET产品支持一个名为scoped enums的新指令。它看起来像这样:

{$APPTYPE CONSOLE}
{$SCOPEDENUMS ON}
type
  TFoo = (One, Two, Three);
{$SCOPEDENUMS OFF}

var
  x: TFoo;
begin
  x := TFoo.One;
  if not (x in [TFoo.Two, TFoo.Three]) then
    Writeln('OK');
end.

#3


3  

What is wrong with native Delphi enums? They are type safe.

本机Delphi枚举有什么问题?它们是类型安全的。

type
  TMyEnum = (Item1, Item2, Item3);

if MyEnum <> Item1 then...

Since Delphi 2005 you can have consts in a class, but Delphi 5 can not.

自Delphi 2005以来,你可以在一个类中拥有consts,但是Delphi 5不能。

type
  TMyEnum = sealed class
  public
    const Item1 = 0;
    const Item2 = 1;
    const Item3 = 2;
  end;

#4


1  

I think I know why Borland choose not to use type safe enums in the TLB editor. Enums in COM can be different values while Delphi only since Delphi 6 (I think) can do that.

我想我知道为什么Borland选择不在TLB编辑器中使用类型安全枚举。 COM中的枚举可以是不同的值,而Delphi只有Delphi 6(我认为)才能做到这一点。

type
  TSomeEnum = (Enum1 = 1, Enum2 = 6, Enum3 = 80);  // Only since Delphi 6