关联数组,其中类 TypeInfo 是 D 中的关键?

发布于 2024-09-07 17:56:49 字数 2061 浏览 4 评论 0原文

我希望能够创建一个多维关联数组,其中一个维度是一个类。就像这样:

class Node{
    Node[?classType?][string] inputs;
}

这样我以后就可以做

Node[] getInputsOfType(?? aClass){
   if(aClass in this.inputs)
      return this.inputs[aClass];
   else
      return null;
}

// meanwhile in another file...

Node[] effects = someAudioNode.getInputsOfType(AudioEffect);

我只是迷路了。有什么想法吗?
关于最后一部分:类可以像这样单独用作参数吗? (此示例中的 AudioEffect 是一个类。)

BR

[更新/分辨率]

感谢您的回答!

我认为发布结果会很好。好吧,我在源代码中查找了.classinfo,发现它返回了一个TypeInfo_Class的实例,并且有一个.name-property,一个string< /代码>。这就是我想出的:

#!/usr/bin/env dmd -run
import  std.stdio;
class A{
    int id;
    static int newId;
    A[string][string] list;
    this(){ id = newId++; }
    void add(A a, string name){
        writefln("Adding: [%s][%s]", a.classinfo.name, name);
        list[a.classinfo.name][name] = a;
    }
    T[string] getAllOf(T)(){
        return cast(T[string]) list[T.classinfo.name];
    }
}
class B : A{ }
void main(){
    auto a = new A();
    a.add(new A(), "test");
    a.add(new B(), "bclass");
    a.add(new B(), "bclass2");

    auto myAList = a.getAllOf!(A);
    foreach(key, item; myAList)
        writefln("k: %s, i: %s id: %s",
                key, item.toString(), item.id);

    auto myBList = a.getAllOf!(B);
    foreach(key, item; myBList)
        writefln("k: %s, i: %s id: %s",
                key, item.toString(), item.id);
}

它输出:

Adding: [classtype.A][test]
Adding: [classtype.B][bclass]
Adding: [classtype.B][bclass2]
Trying to get [classtype.A]
k: test, i: classtype.A id: 1
Trying to get [classtype.B]
k: bclass2, i: classtype.B id: 3
k: bclass, i: classtype.B id: 2

所以是的,我想它有效。耶!有人有改进的想法吗?

这里有什么陷阱吗?

  • classinfo.name 会突然表现异常吗?
  • 有没有“正确”的方法来获取类名?

另外,这是最快的方法吗?我的意思是,所有类名似乎都以 classtype. 开头。哦,好吧,这可能是另一个 SO 线程。再次感谢!

BR

I'd like to be able to create an multidim associative array where one dimension is a class. Like this:

class Node{
    Node[?classType?][string] inputs;
}

so that I later can do

Node[] getInputsOfType(?? aClass){
   if(aClass in this.inputs)
      return this.inputs[aClass];
   else
      return null;
}

// meanwhile in another file...

Node[] effects = someAudioNode.getInputsOfType(AudioEffect);

I'm just lost. Any ideas?
Regarding the last part: Can a class be used as a parameter all by itself like that? (AudioEffect in this example is a class.)

BR

[update/resolution]

Thanks for the answer(s)!

I thought it'd be nice to post the result. Ok, I looked up .classinfo in the source code and found that it returns an instance of TypeInfo_Class, and that there's a .name-property, a string. So this is what I came up with:

#!/usr/bin/env dmd -run
import  std.stdio;
class A{
    int id;
    static int newId;
    A[string][string] list;
    this(){ id = newId++; }
    void add(A a, string name){
        writefln("Adding: [%s][%s]", a.classinfo.name, name);
        list[a.classinfo.name][name] = a;
    }
    T[string] getAllOf(T)(){
        return cast(T[string]) list[T.classinfo.name];
    }
}
class B : A{ }
void main(){
    auto a = new A();
    a.add(new A(), "test");
    a.add(new B(), "bclass");
    a.add(new B(), "bclass2");

    auto myAList = a.getAllOf!(A);
    foreach(key, item; myAList)
        writefln("k: %s, i: %s id: %s",
                key, item.toString(), item.id);

    auto myBList = a.getAllOf!(B);
    foreach(key, item; myBList)
        writefln("k: %s, i: %s id: %s",
                key, item.toString(), item.id);
}

It outputs:

Adding: [classtype.A][test]
Adding: [classtype.B][bclass]
Adding: [classtype.B][bclass2]
Trying to get [classtype.A]
k: test, i: classtype.A id: 1
Trying to get [classtype.B]
k: bclass2, i: classtype.B id: 3
k: bclass, i: classtype.B id: 2

So yeah, I suppose it works. Yey! Anyone has ideas for improvement?

Are there any pitfalls here?

  • Can classinfo.name suddenly behave unexpedictly?
  • Is there a »proper» way of getting the class name?

Also, is this the fastest way of doing it? I mean, all class names seems to start with classtype.. Oh well, that's perhaps another SO-thread. Thanks again!

BR

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

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

发布评论

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

评论(1

长途伴 2024-09-14 17:56:49

您可以使用 ClassInfo 类(可通过 .classinfo 属性)在运行时引用类类型。但是,ClassInfo 类没有实现在关联数组中使用的必要方法(请参阅 关于在 AA 中使用类/结构的 AA 参考页面)。我想如果您实现自己的 ClassInfo 包装器(可以用作 AA 密钥),这是可能的。这应该相当简单,因为您可以预期类类型只有一个 ClassInfo 实例,因此您可以简单地使用 ClassInfo 的地址作为唯一哈希。


顺便说一句,更简单、更快的编写方法

if (key in aa)
    return aa[key];
else
    return null;

auto pvalue = key in aa;
return pvalue ? *pvalue : null;

You can use the ClassInfo class (accessible through the .classinfo property) to refer to class types at runtime. However, the ClassInfo class doesn't implement the necessary methods to be used in an associative array (see the D reference page on AAs regarding using classes/structs in AAs). I suppose this is possible if you implement your own wrapper for ClassInfo that can be used as an AA key. This should be fairly simple, as you can expect that a class type will have only one ClassInfo instance, thus you can simply use the ClassInfo's address as the unique hash.


By the way, a simpler and faster way to write

if (key in aa)
    return aa[key];
else
    return null;

is

auto pvalue = key in aa;
return pvalue ? *pvalue : null;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文