I need to generate something that can be used as a unique handle for a user defined type (struct or class) in the D programming language. Preferably this would be a compile time computable value. I want the handle to relate to the name of the type as well as change if the internal structure (data layout) of the type changes but remain the same for most other edits (including compiling the same type into a different app).
我需要生成一些东西,可以用作D编程语言中用户定义类型(结构或类)的唯一句柄。优选地,这将是编译时可计算值。我希望句柄与类型的名称相关,并且如果类型的内部结构(数据布局)发生更改,则更改,但对于大多数其他编辑(包括将相同类型编译到不同的应用程序中)保持相同。
This is not a security thing so it doesn't need to be hard to bypass or anything
这不是一个安全的东西,所以它不需要很难绕过或任何东西
My current thought is to use a string with something like an MD5 hash of the type name and member types and names.
我目前的想法是使用类似名称和成员类型和名称的MD5哈希字符串。
Any thoughts
4 个解决方案
#1
After thinking about this for a bit, I think this would be a feasible approach (note: this is just pseudo code):
在考虑了这一点之后,我认为这将是一种可行的方法(注意:这只是伪代码):
UniqueId(Type) = Type.stringof ~ MemberIds!(Type.tupleof)
UniqueId(Type) if( Type is builtin ) = Type.stringof
MemberIds(M, Ms...) = "," ~ UniqueId!(typeof(M))
~ "@" ~ ToString!(M.offsetof)
~ ":" ~ M.stringof
~ MemberIds!(Ms)
That is, construct the unique ID from the type's name (you might need to chop off the module and package, not sure), and each member's type's ID, offset and name.
也就是说,从类型名称构造唯一ID(您可能需要切断模块和包,不确定),以及每个成员的类型ID,偏移量和名称。
Depending on exactly what you want, you could remove the member name.
根据您的需要,您可以删除成员名称。
#2
The fully qualified name of a type should be unique. This is the same as typeid(T).toString. This is not the same as T.stringof -- T.stringof will erase any template instantiations and will not give the fully qualified name.
类型的完全限定名称应该是唯一的。这与typeid(T).toString相同。这与T.stringof不同 - T.stringof将删除任何模板实例化,并且不会提供完全限定名称。
The workaround is to use demangled(T.mangleof) at compiletime and typeid(T).toString at runtime.
解决方法是在编译时使用demangled(T.mangleof),在运行时使用typeid(T).toString。
#3
The typeid expression will return an unique instance of a TypeInfo object. In theory, you should be able to use the address of the TypeInfo object as the type's unique identifier value.
typeid表达式将返回TypeInfo对象的唯一实例。理论上,您应该能够使用TypeInfo对象的地址作为类型的唯一标识符值。
#4
You know, you can just hardcode a revision into the type, like "const REV = 173; ", then update it every time you change the layout, then mix that with the type name to produce your identifier.
您知道,您可以将修订版硬编码到类型中,例如“const REV = 173;”,然后在每次更改布局时更新它,然后将其与类型名称混合以生成您的标识符。
This is a bit of a hassle because it requires manual updates, but you could script it to be updated automatically on commit when svn diff recognizes a change in that class. And it's probably the easiest solution.
这有点麻烦,因为它需要手动更新,但是当svn diff识别出该类中的更改时,您可以编写脚本以在提交时自动更新。这可能是最简单的解决方案。
#1
After thinking about this for a bit, I think this would be a feasible approach (note: this is just pseudo code):
在考虑了这一点之后,我认为这将是一种可行的方法(注意:这只是伪代码):
UniqueId(Type) = Type.stringof ~ MemberIds!(Type.tupleof)
UniqueId(Type) if( Type is builtin ) = Type.stringof
MemberIds(M, Ms...) = "," ~ UniqueId!(typeof(M))
~ "@" ~ ToString!(M.offsetof)
~ ":" ~ M.stringof
~ MemberIds!(Ms)
That is, construct the unique ID from the type's name (you might need to chop off the module and package, not sure), and each member's type's ID, offset and name.
也就是说,从类型名称构造唯一ID(您可能需要切断模块和包,不确定),以及每个成员的类型ID,偏移量和名称。
Depending on exactly what you want, you could remove the member name.
根据您的需要,您可以删除成员名称。
#2
The fully qualified name of a type should be unique. This is the same as typeid(T).toString. This is not the same as T.stringof -- T.stringof will erase any template instantiations and will not give the fully qualified name.
类型的完全限定名称应该是唯一的。这与typeid(T).toString相同。这与T.stringof不同 - T.stringof将删除任何模板实例化,并且不会提供完全限定名称。
The workaround is to use demangled(T.mangleof) at compiletime and typeid(T).toString at runtime.
解决方法是在编译时使用demangled(T.mangleof),在运行时使用typeid(T).toString。
#3
The typeid expression will return an unique instance of a TypeInfo object. In theory, you should be able to use the address of the TypeInfo object as the type's unique identifier value.
typeid表达式将返回TypeInfo对象的唯一实例。理论上,您应该能够使用TypeInfo对象的地址作为类型的唯一标识符值。
#4
You know, you can just hardcode a revision into the type, like "const REV = 173; ", then update it every time you change the layout, then mix that with the type name to produce your identifier.
您知道,您可以将修订版硬编码到类型中,例如“const REV = 173;”,然后在每次更改布局时更新它,然后将其与类型名称混合以生成您的标识符。
This is a bit of a hassle because it requires manual updates, but you could script it to be updated automatically on commit when svn diff recognizes a change in that class. And it's probably the easiest solution.
这有点麻烦,因为它需要手动更新,但是当svn diff识别出该类中的更改时,您可以编写脚本以在提交时自动更新。这可能是最简单的解决方案。