www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - get a struct member pointer

reply Jeffry Nox <jfnoxville eastgnet.com> writes:
struct A {
	uint id=0;
	char[] name;
}

struct B {
	uint id=0;
	char[] title;
}

void lookup(T)(T[] s, ***)
{
  char[] txt = s[0].***;
}

as illustrated above, how can i get struct object property info as in *** so i
can manipulate it inside the function lookup above? in function lookup, i didnt
know what is the variable name for the char[], so *** pass in the 2nd
parameter, question is how to pass that member info or pointer so that it works
on different struct declarations?
Feb 15 2009
next sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
Jeffry Nox wrote:
 struct A {
 	uint id=0;
 	char[] name;
 }
 
 struct B {
 	uint id=0;
 	char[] title;
 }
 
 void lookup(T)(T[] s, ***)
 {
   char[] txt = s[0].***;
 }
 
 as illustrated above, how can i get struct object property info as in *** so i
can manipulate it inside the function lookup above? in function lookup, i didnt
know what is the variable name for the char[], so *** pass in the 2nd
parameter, question is how to pass that member info or pointer so that it works
on different struct declarations?
At runtime? You can't, really. The only thing you could do is something like this: void lookup(T)(T[] s, size_t offset) { char[] text = *cast(char[]*)(cast(void*)(&s[0]) + offset); } (Note: haven't tested that particular combination of casts; I don't bother memorising this :P) Or something to that effect. If possible, it'd be safer to do this: void lookup(T, char[] field)(T[] s) { char[] text = mixin(`s[0].`~field); } -- Daniel
Feb 15 2009
next sibling parent Jeffry Nox <jfnoxville eastgnet.com> writes:
ill try your suggestion, thx for reply.
Feb 15 2009
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Daniel Keep:
 void lookup(T)(T[] s, size_t offset)
 {
   char[] text = *cast(char[]*)(cast(void*)(&s[0]) + offset);
 }
I am learning still this topic, but can't this create an aliasing problem, as in C? http://www.cellperformance.com/mike_acton/2006/06/understanding_strict_aliasing.html Bye, bearophile
Feb 16 2009
next sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
bearophile wrote:
 Daniel Keep:
 void lookup(T)(T[] s, size_t offset)
 {
   char[] text = *cast(char[]*)(cast(void*)(&s[0]) + offset);
 }
I am learning still this topic, but can't this create an aliasing problem, as in C?
Oh I really doubt the above is safe. But it's the only way I can think of to access a given struct field at runtime.
 http://www.cellperformance.com/mike_acton/2006/06/understanding_strict_aliasing.html
Heads up, Google is flagging the above site as doing drive-by installation of malicious software. Report here: http://safebrowsing.clients.google.com/safebrowsing/diagnostic?client=Firefox&hl=en-GB&site=http://www.cellperformance.com/mike_acton/2006/06/understanding_strict_aliasing.html
 Bye,
 bearophile
Feb 16 2009
parent reply bearophile <bearophileHUGS lycos.com> writes:
Daniel Keep:
 Heads up, Google is flagging the above site as doing drive-by
 installation of malicious software.
My browser there has done nothing bad, and that page offers one of the best contents about this topic (I think that page is linked from Wikipedia too). Is that the kiss of death from Google for a page? :-) Bye, bearophile
Feb 16 2009
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
bearophile wrote:
 Daniel Keep:
 Heads up, Google is flagging the above site as doing drive-by
 installation of malicious software.
My browser there has done nothing bad, and that page offers one of the best contents about this topic (I think that page is linked from Wikipedia too). Is that the kiss of death from Google for a page? :-) Bye, bearophile
Well, when it lists the site as having performed drive-by installation WITHOUT having to prompt or notify the user, that's pretty much the end of it for me. Given the report itself, it's probably malware that got in via their advertiser. But that's still no excuse. I don't care HOW good the content is if I'm risking my machine to view it. That said, I actually read a bit of it via lynx; I don't think the code I listed violates strict aliasing. It creates a pointer of the same type to a piece of memory; it only creates one of a different type as an intermediary step, and doesn't store it. -- Daniel
Feb 16 2009
parent Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
Daniel Keep wrote:
 
 bearophile wrote:
 Daniel Keep:
 Heads up, Google is flagging the above site as doing drive-by
 installation of malicious software.
My browser there has done nothing bad, and that page offers one of the best contents about this topic (I think that page is linked from Wikipedia too). Is that the kiss of death from Google for a page? :-) Bye, bearophile
Well, when it lists the site as having performed drive-by installation WITHOUT having to prompt or notify the user, that's pretty much the end of it for me. Given the report itself, it's probably malware that got in via their advertiser. But that's still no excuse. I don't care HOW good the content is if I'm risking my machine to view it. That said, I actually read a bit of it via lynx; I don't think the code I listed violates strict aliasing. It creates a pointer of the same type to a piece of memory; it only creates one of a different type as an intermediary step, and doesn't store it. -- Daniel
Firefox/3.1 + NoScript + AdBlock-Plus prevents almost everything. That, and running Linux in the first place... ;) -- Chris Nicholson-Sauls
Feb 17 2009
prev sibling parent reply grauzone <none example.net> writes:
bearophile wrote:
 Daniel Keep:
 void lookup(T)(T[] s, size_t offset)
 {
   char[] text = *cast(char[]*)(cast(void*)(&s[0]) + offset);
 }
I am learning still this topic, but can't this create an aliasing problem, as in C? http://www.cellperformance.com/mike_acton/2006/06/understanding_strict_aliasing.html
My theory is, that Walter's code generator is too primitive to care about aliasing. But I guess it's possible, that aliasing rules will be added later to the language, when LDC (hopefully) gets big? By the way, wtf is Daniel's code doing at all?
 Bye,
 bearophile
Feb 16 2009
parent Daniel Keep <daniel.keep.lists gmail.com> writes:
grauzone wrote:
 bearophile wrote:
 Daniel Keep:
 void lookup(T)(T[] s, size_t offset)
 {
   char[] text = *cast(char[]*)(cast(void*)(&s[0]) + offset);
 }
I am learning still this topic, but can't this create an aliasing problem, as in C? http://www.cellperformance.com/mike_acton/2006/06/understanding_strict_aliasing.html
My theory is, that Walter's code generator is too primitive to care about aliasing. But I guess it's possible, that aliasing rules will be added later to the language, when LDC (hopefully) gets big? By the way, wtf is Daniel's code doing at all?
 Bye,
 bearophile
Unless I cocked it up (which is entirely possible, mind you) I'm trying to get a pointer to the struct, cast to void* because I can never remember if (ptr + int) multiplies the offset by (*ptr).sizeof or not, casting THAT to a pointer to a char[], then dereferencing it to get the value. It is also, in a round-about way, trying to demonstrate that trying to do this is really just not pretty and Jeffry might like to investigate alternate ways of getting those fields. :P -- Daniel
Feb 16 2009
prev sibling parent Christopher Wright <dhasenan gmail.com> writes:
Jeffry Nox wrote:
 struct A {
 	uint id=0;
 	char[] name;
 }
 
 struct B {
 	uint id=0;
 	char[] title;
 }
 
 void lookup(T)(T[] s, ***)
 {
   char[] txt = s[0].***;
 }
 
 as illustrated above, how can i get struct object property info as in *** so i
can manipulate it inside the function lookup above? in function lookup, i didnt
know what is the variable name for the char[], so *** pass in the 2nd
parameter, question is how to pass that member info or pointer so that it works
on different struct declarations?
 
Use tupleof. You need to specify a compile-time constant value as the index to tupleof, but looping also works: import tango.io.Stdout; struct S { int i; char[] b; } void getItem (S s, int index) { foreach (i, n; s.tupleof) { Stdout.formatln("{} {}", i, typeid(typeof(s.tupleof[i]))); if (i == index) { auto value = s.tupleof[i]; Stdout.formatln("Value is `{}`", value); } } } void main (char[][] args) { S s; getItem(s, 0); getItem(s, 1); }
Feb 16 2009