A little of coordination for Rosettacode
bearophile
bearophileHUGS at lycos.com
Mon Jun 17 20:00:53 PDT 2013
Adam D. Ruppe:
> and win the code golf every time! :P
Some Rosettacode D entries are a bit compressed, but that site is
not for code golfing. It's just preferred to not write long
programs, for several reasonable reasons.
> code:
> http://arsdnet.net/dcode/rpc-example.d
>
> library:
> https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/rpc.d
I have reformatted your code a little for the D standard of
Rosettacode (72 columns max, etc):
http://codepad.org/lAxAJwoG
With your code I have found a dmd compiler bug, are you able and
willing to further reduce this?
import std.conv: to;
import std.traits: ParameterTypeTuple;
mixin template NetworkServer(Interface) {
void callByNumber(int functionNumber, int sequenceNumber,
const(ubyte)[] buffer) {
string callCode() {
string code;
foreach(memIdx, member; __traits(allMembers, Interface)) {
code ~= "\t\tcase " ~ to!string(memIdx + 1) ~ ":\n";
alias mem = PassThrough!(__traits(getMember, Interface,
member));
foreach(i, arg; ParameterTypeTuple!mem) {
auto istr = to!string(i);
code ~= "\t\t\t" ~ arg.stringof ~ " arg" ~ istr ~ ";\n";
code ~= "\t\t\tbuffer = deserializeInto(buffer, arg" ~
istr ~ ");\n";
}
code ~= "\t\tbreak;\n";
}
return code;
}
switch(functionNumber) {
mixin(callCode());
}
}
}
template PassThrough(alias a) {
alias PassThrough = a;
}
void deserializeInto(T)(inout(ubyte)[] buffer, ref T s) {
s.length = 1;
}
mixin template NetworkClient(Interface) {
private static void createClass() {}
mixin(createClass());
}
interface ExampleNetworkFunctions {
void sayHello(in string);
}
class ExampleServer : ExampleNetworkFunctions {
override void sayHello(in string) {}
mixin NetworkServer!ExampleNetworkFunctions;
}
void main() {}
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list