How to uniquely identify a user-defined type in D?

I need to generate something that can be used as a unique tag for a user-defined type (or class structure) in D . Preferably, this will calculate the compilation time value. I want the descriptor to refer to the type name, and also change if the internal structure (data layout) of the type changes, but remains unchanged for most other edits (including compiling the same type into another application).

This is not a security thing, so you don’t just have to get around it or do something.

My current thought is to use a string with something like an MD5 hash of type and types and member names.

Any thoughts

+3
source share
4 answers

After thinking about this a bit, I think this would be an acceptable 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, build a unique identifier from the type name (you may need to cancel the module and package, not sure) and each element type identifier, offset and name.

Depending on what you want, you can delete the member name.

+1
source

A valid type name must be unique. This is the same as typeid (T) .toString. This is not the same as T.stringof - T.stringof erases any template instances and does not give a full name.

The workaround is to use demangled (T.mangleof) at compiletime and typeid (T) .toString at runtime.

+1
source

, , , "const REV = 173;", , , .

, , script , svn diff . , , .

0

Source: https://habr.com/ru/post/1708609/


All Articles