如何实现类型安全的 COM 枚举?

发布于 2024-07-04 14:55:29 字数 497 浏览 8 评论 0原文

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

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

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

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

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

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:

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云淡风轻 2024-07-11 14:55:29

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

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

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.

type
  TSomeEnum = (Enum1 = 1, Enum2 = 6, Enum3 = 80);  // Only since Delphi 6
风蛊 2024-07-11 14:55:29

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

即将推出的 Delphi 2009 以及 Delphi for .NET 产品的最后一个版本支持称为作用域枚举的新指令。 它看起来像这样:

{$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.

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.

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

{$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.
不顾 2024-07-11 14:55:29

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

我想不出 Delphi 的 COM 不能使用类型安全枚举的原因,但事实并非如此。 恐怕对此无能为力。 也许即将发布的 Delphi 2009 版本中 TLB 编辑器的更改可能会改变这一点。

郑重声明:当不使用 TLB 编辑器时,Delphi 完全能够与以类型安全枚举作为参数的方法进行交互。

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.

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.

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.

月隐月明月朦胧 2024-07-11 14:55:29

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

type
  TMyEnum = (Item1, Item2, Item3);

if MyEnum <> Item1 then...

从 Delphi 2005 开始,您可以在类中拥有常量,但 Delphi 5 不能。

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

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

type
  TMyEnum = (Item1, Item2, Item3);

if MyEnum <> Item1 then...

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

type
  TMyEnum = sealed class
  public
    const Item1 = 0;
    const Item2 = 1;
    const Item3 = 2;
  end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文