digitalmars.D - proposal: add bytesof Property, reduce pointer/cast requires
- redsea (11/11) Nov 17 2007 I found D's array and slice is very useful, most time I can use byte [] ...
- Jarrett Billingsley (8/22) Nov 17 2007 byte[] bytesOf(T)(ref T t)
- redsea (23/59) Nov 17 2007 This help some, but it don't work at static array, and literal integer, ...
- Janice Caron (4/10) Nov 17 2007 That's the way to do it!
- Janice Caron (3/6) Nov 17 2007 Nah. Bugger, I'm wrong. Sorry, retract that.
- Robert Fraser (2/3) Nov 17 2007 It's not a bad idea, but I can see it being used rarely enough it wouldn...
- redsea (5/10) Nov 17 2007 I don't care endians in most case, because most of my application need h...
- Lionello Lunesu (7/7) Nov 18 2007 The cast to void[] is implicit:
- redsea (22/35) Nov 18 2007 This seems work with static arary, the following code give result 36.
- Henning Hasemann (8/8) Nov 18 2007 Maybe this helps you:
- redsea (2/16) Nov 19 2007
I found D's array and slice is very useful, most time I can use byte [] to finish my work without using pointer. But when I need to copy from/to a struct I need to use code like this: byteary[i..i+structvar.sizeof] = (cast (byte *)&structvar)[0..structvar.sizeof) the cast, the &, looking bad, and code is complex. if d can supply a internal property, let's say bytes of, can give us the correctly byte [] slice, then I can write: byteary[i..i+struct.var.sizeof] = structvar.bytesof and, when we write a function send struct to network, we can simple write: void sendPacket(byte[] content); and we can use as that: sendPacket(structvar.bytesof); any suggestions ?
Nov 17 2007
"redsea" <redsea 163.com> wrote in message news:fho4go$1r6t$1 digitalmars.com...I found D's array and slice is very useful, most time I can use byte [] to finish my work without using pointer. But when I need to copy from/to a struct I need to use code like this: byteary[i..i+structvar.sizeof] = (cast (byte *)&structvar)[0..structvar.sizeof) the cast, the &, looking bad, and code is complex. if d can supply a internal property, let's say bytes of, can give us the correctly byte [] slice, then I can write: byteary[i..i+struct.var.sizeof] = structvar.bytesof and, when we write a function send struct to network, we can simple write: void sendPacket(byte[] content); and we can use as that: sendPacket(structvar.bytesof); any suggestions ?byte[] bytesOf(T)(ref T t) { return (cast(byte*)&t)[0 .. t.sizeof]; } byteArray[i .. i + struct.var.sizeof] = bytesOf(structVar); sendPacket(bytesOf(structVar));
Nov 17 2007
This help some, but it don't work at static array, and literal integer, read following, the first call is ok, next 2 call is not ok, change ref to in cause other problems, still not ok. (d 1.022) module test; import tango.io.Stdout; alias Stdout od; byte[] bytesof(T)(ref T t) { return (cast(byte*)&t)[0 .. t.sizeof]; } struct SS { int a; int b; } int main() { SS [10] as; SS s; od(bytesof(s).length).newline; od(bytesof(as).length).newline; od(bytesof(3).length).newline; return 0; } Jarrett Billingsley Wrote:"redsea" <redsea 163.com> wrote in message news:fho4go$1r6t$1 digitalmars.com...I found D's array and slice is very useful, most time I can use byte [] to finish my work without using pointer. But when I need to copy from/to a struct I need to use code like this: byteary[i..i+structvar.sizeof] = (cast (byte *)&structvar)[0..structvar.sizeof) the cast, the &, looking bad, and code is complex. if d can supply a internal property, let's say bytes of, can give us the correctly byte [] slice, then I can write: byteary[i..i+struct.var.sizeof] = structvar.bytesof and, when we write a function send struct to network, we can simple write: void sendPacket(byte[] content); and we can use as that: sendPacket(structvar.bytesof); any suggestions ?byte[] bytesOf(T)(ref T t) { return (cast(byte*)&t)[0 .. t.sizeof]; } byteArray[i .. i + struct.var.sizeof] = bytesOf(structVar); sendPacket(bytesOf(structVar));
Nov 17 2007
byte[] bytesOf(T)(ref T t) { return (cast(byte*)&t)[0 .. t.sizeof]; } byteArray[i .. i + struct.var.sizeof] = bytesOf(structVar); sendPacket(bytesOf(structVar));That's the way to do it! But since bytesOf() operates on an array, one could presumably also write sendPacket(structVar.bytesOf); which is what the orignal poster wanted. (Isn't D great?)
Nov 17 2007
On 11/18/07, Janice Caron <caron800 googlemail.com> wrote:But since bytesOf() operates on an array, one could presumably also write sendPacket(structVar.bytesOf); which is what the orignal poster wanted. (Isn't D great?)Nah. Bugger, I'm wrong. Sorry, retract that. Anyway I still think Jarrett's idea is the perfect solution.
Nov 17 2007
redsea Wrote:if d can supply a internal property, let's say bytes of, can give us the correctly byte [] sliceIt's not a bad idea, but I can see it being used rarely enough it wouldn't be necessary. Plus, if you're really serializing something for cross-platform use, you should consider endianness, which neither your method nor bytesof would capture.
Nov 17 2007
Robert Fraser Wrote:redsea Wrote:I don't care endians in most case, because most of my application need high performance, I don't want to waste CPU time when I use X86 servers (&clients). X86 serve is powerfull and cheap. At china, here are many mass online game providers, theire program would also direct send struct for simplify and efficiency, don't care about endians too. At, I use share memory to do comunications between processes, between a D program and C++ program, even send struct via netlink interface to linux kernel, here we don't have endians problem -- but the align problem want to be check carefully, we write a public .inc head file, include in c and mixin(import) in d, and a program to verify their size is match. En, perhaps later, I would care about endians, because I maybe would use MIPS network processor (hope gdc stable enough to write the management program on the chip at that time), but there still more high bandwidth internal communication don't need to care about endians.if d can supply a internal property, let's say bytes of, can give us the correctly byte [] sliceIt's not a bad idea, but I can see it being used rarely enough it wouldn't be necessary. Plus, if you're really serializing something for cross-platform use, you should consider endianness, which neither your method nor bytesof would capture.
Nov 17 2007
The cast to void[] is implicit: void[] bytesof = (&structvar)[0..1]; and there's no need for the .sizeof if you leave the pointer of type struct*. It also makes sense to make the cast to byte[] NOT implicit, since the resulting array might not be portable on a system with different endianess. L.
Nov 18 2007
This seems work with static arary, the following code give result 36. Not very simple, but typesafe. Thanks, everybody! struct SS { int i; int j; int k; } byte[] tobytes(void [] v) { return cast(byte []) v; } int main() { SS[3] structvar; byte[] bytesof = tobytes((&structvar)[0..1]); Stdout(bytesof.length).newline; return 0; } Lionello Lunesu Wrote:The cast to void[] is implicit: void[] bytesof = (&structvar)[0..1]; and there's no need for the .sizeof if you leave the pointer of type struct*. It also makes sense to make the cast to byte[] NOT implicit, since the resulting array might not be portable on a system with different endianess. L.
Nov 18 2007
Maybe this helps you: http://www.leetless.de/indiana-parts/s11n.d I also have a more up-to-date version of that code available but currently not uploaded, so mail me if you're interested in that one. Henning -- GPG Public Key: http://gpg-keyserver.de/pks/lookup?op=get&search=0xDDD6D36D41911851
Nov 18 2007
Thanks very much, I download it, and maybe use somewhere else :) Henning Hasemann Wrote:Maybe this helps you: http://www.leetless.de/indiana-parts/s11n.d I also have a more up-to-date version of that code available but currently not uploaded, so mail me if you're interested in that one. Henning -- GPG Public Key: http://gpg-keyserver.de/pks/lookup?op=get&search=0xDDD6D36D41911851
Nov 19 2007