setTypeInfo???
Denis Koroskin
2korden at gmail.com
Sun Aug 31 02:32:35 PDT 2008
On Sun, 31 Aug 2008 01:45:55 +0400, bearophile <bearophileHUGS at lycos.com>
wrote:
> dsimcha Wrote:
>
>> I've noticed something strange with the setTypeInfo() function in gc.d.
>> Ran
>> into it trying to write some custom allocation functions for dynamic
>> arrays.
>> writefln(typeid(uint).flags); //Returns 0.
>> writefln(typeid(float).flags); //Returns 0.
>> writefln(typeid(uint*).flags); //Returns 1.
>> writefln(typeid(void*).flags); //Returns 1.
>
> Ah, for that purpose I have used the following monster (take a look at
> my last post too
> http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=75811
> ):
>
>
> import std.traits: FieldTypeTuple;
>
> template IsType(T, Types...) {
> // Original idea by Burton Radons, modified
> static if (Types.length == 0)
> const bool IsType = false;
> else
> const bool IsType = is(T == Types[0]) || IsType!(T, Types[1 ..
> $]);
> }
>
> template ArrayType1(T: T[]) {
> alias T ArrayType1;
> }
>
> template IsReferenceType(Types...) {
> static if (Types.length == 0) {
> const bool IsReferenceType = false;
> } else static if (Types.length == 1) {
> static if (IsType!(Types[0], bool, byte, ubyte, short, ushort,
> int, uint,
> long, ulong, float, double, real, ifloat,
> idouble,
> ireal, cfloat, cdouble, creal, char, dchar,
> wchar) ) {
> const bool IsReferenceType = false;
> } else static if ( is(Types[0] == struct) ) {
> const bool IsReferenceType =
> IsReferenceType!(FieldTypeTuple!(Types[0]));
> } else static if (IsStaticArray!(Types[0])) {
> const bool IsReferenceType =
> IsReferenceType!(ArrayType1!(Types[0]));
> } else
> const bool IsReferenceType = true;
> } else
> const bool IsReferenceType = IsReferenceType!(Types[0]) |
> IsReferenceType!(Types[1 .. $]);
> } // end IsReferenceType!()
>
>
> template IsArray(T) {
> const bool IsArray = is(typeof(T.length)) && is(typeof(T.sort)) &&
> is(typeof(T.reverse)) && is(typeof(T.dup));
> }
>
> template IsDynamicArray(T) {
> // Adapted from the module tango.core.Traits, (C) 2005-2006 Sean
> Kelly, BSD style licence
> const bool IsDynamicArray = is( typeof(T.init[0])[] == T );
> }
>
> template IsStaticArray(T) {
> const bool IsStaticArray = IsArray!(T) && (!IsDynamicArray!(T));
> }
>
> Bye,
> bearophile
Looks like my message class is a static array:
class Message
{
this(char[] message) { this.message = message; }
size_t length() { return message.length; }
void sort() { message.sort(); }
void reverse() { message.reverse(); }
Message dup() { return new Message(message.dup()); }
char[] toString() { return message; }
private char[] message;
}
assert(IsStaticArray!(Message) == false); // fails
More information about the Digitalmars-d
mailing list