Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
change API for templated aaLiteral
Browse files Browse the repository at this point in the history
- let the compiler pass keys and values as slices
  like for the existing _d_assocarrayliteralTX call

- add _aaGetZ which is the same as _aaGetX but also
  get's passed an additional pointer to the typed
  aaLiteral!(Key, Value) function
  • Loading branch information
MartinNowak committed Nov 26, 2014
1 parent a4b40a7 commit 5b9bbd8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 58 deletions.
31 changes: 2 additions & 29 deletions src/object.di
Original file line number Diff line number Diff line change
Expand Up @@ -393,36 +393,9 @@ extern (C)
void* _d_assocarrayliteralTX(const TypeInfo_AssociativeArray ti, void[] keys, void[] values) pure;
}

auto aaLiteral(Key, Value, T...)(auto ref T args) if (T.length % 2 == 0)
void* aaLiteral(Key, Value)(Key[] keys, Value[] values) @trusted pure
{
static if(!T.length)
{
return cast(void*)null;
}
else
{
import core.internal.traits;
Key[] keys;
Value[] values;
keys.reserve(T.length / 2);
values.reserve(T.length / 2);

foreach (i; staticIota!(0, args.length / 2))
{
keys ~= args[2*i];
values ~= args[2*i + 1];
}

void[] key_slice;
void[] value_slice;
void *ret;
() @trusted {
key_slice = *cast(void[]*)&keys;
value_slice = *cast(void[]*)&values;
ret = _d_assocarrayliteralTX(typeid(Value[Key]), key_slice, value_slice);
}();
return ret;
}
return _d_assocarrayliteralTX(typeid(Value[Key]), *cast(void[]*)&keys, *cast(void[]*)&values);
}

alias AssociativeArray(Key, Value) = Value[Key];
Expand Down
31 changes: 2 additions & 29 deletions src/object_.d
Original file line number Diff line number Diff line change
Expand Up @@ -1989,36 +1989,9 @@ extern (C)
void* _d_assocarrayliteralTX(const TypeInfo_AssociativeArray ti, void[] keys, void[] values) pure;
}

auto aaLiteral(Key, Value, T...)(auto ref T args) if (T.length % 2 == 0)
void* aaLiteral(Key, Value)(Key[] keys, Value[] values) @trusted pure
{
static if(!T.length)
{
return cast(void*)null;
}
else
{
import core.internal.traits;
Key[] keys;
Value[] values;
keys.reserve(T.length / 2);
values.reserve(T.length / 2);

foreach (i; staticIota!(0, args.length / 2))
{
keys ~= args[2*i];
values ~= args[2*i + 1];
}

void[] key_slice;
void[] value_slice;
void *ret;
() @trusted {
key_slice = *cast(void[]*)&keys;
value_slice = *cast(void[]*)&values;
ret = _d_assocarrayliteralTX(typeid(Value[Key]), key_slice, value_slice);
}();
return ret;
}
return _d_assocarrayliteralTX(typeid(Value[Key]), *cast(void[]*)&keys, *cast(void[]*)&values);
}

alias AssociativeArray(Key, Value) = Value[Key];
Expand Down
8 changes: 8 additions & 0 deletions src/rt/aaA.d
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ Lret:
return cast(void*)(e + 1) + aligntsize(keytitsize);
}


/// Same as above but with a function pointer to aaLiteral!(Key, Value) for creating a typed AA instance.
void* _aaGetZ(AA* aa, const TypeInfo keyti, in size_t valuesize, in void* pkey,
void *function(void[], void[]) @trusted pure aaLiteral)
{
return _aaGetX(aa, keyti, valuesize, pkey);
}

// bug 13748
pure nothrow unittest
{
Expand Down

0 comments on commit 5b9bbd8

Please sign in to comment.