Thread overview |
---|
April 04, 2009 Format.convert problme | ||||
---|---|---|---|---|
| ||||
Hi All, tango.text.convert.Format provides a nice function to convert anything to string. It works perfect except the argument is a pointer type. It will print the address of a pointer instead of its value For instance: -------------------------------- int* i = new int; *i = 10; Format.convert("{}", i); // <- the address of the pointer ---------------------------------------------- How to let it print the value instead of the address? Because I wanna write a dump function to dump the value of any data type (also void, null) -- Xu, Qian (stanleyxu) http://stanleyxu2005.blogspot.com |
April 04, 2009 Re: Format.convert problme | ||||
---|---|---|---|---|
| ||||
Posted in reply to Qian Xu | Qian Xu wrote:
> Hi All,
>
> tango.text.convert.Format provides a nice function to convert anything to string.
>
> It works perfect except the argument is a pointer type.
> It will print the address of a pointer instead of its value
>
> For instance: --------------------------------
> int* i = new int;
> *i = 10;
> Format.convert("{}", i); // <- the address of the pointer
> ----------------------------------------------
>
> How to let it print the value instead of the address?
> Because I wanna write a dump function to dump the value of any data type (also void, null)
Check if the variable is a pointer, and if yes, dereference it:
alias typeof(i) T;
static if (is(T T2 : T2*)) {
T2 i2 = *i;
Format.convert("{}", i2);
} else {
Format.convert("{}", i);
}
|
April 05, 2009 Re: Format.convert problme | ||||
---|---|---|---|---|
| ||||
Posted in reply to Qian Xu | Qian Xu schrieb:
> Hi All,
>
> tango.text.convert.Format provides a nice function to convert anything to string.
>
> It works perfect except the argument is a pointer type.
> It will print the address of a pointer instead of its value
>
> For instance: --------------------------------
> int* i = new int;
> *i = 10;
> Format.convert("{}", i); // <- the address of the pointer
> ----------------------------------------------
>
> How to let it print the value instead of the address?
> Because I wanna write a dump function to dump the value of any data type
> (also void, null)
>
>
>
>
This is not really an exact answer to your question... :)
It seems you want to output trace information. Stdout (which seems you
are using) is not thread safe and will generate AV/segf if accessed
concurrently.
Specially for printing trace info, there is
tango.util.log.Trace
it is a Stdout replacement that is synchronized, and it also has a
memory() function, that lets you print raw memory as a dump.
|
April 06, 2009 Re: Format.convert problme | ||||
---|---|---|---|---|
| ||||
Posted in reply to grauzone | grauzone wrote:
>
> Check if the variable is a pointer, and if yes, dereference it:
>
> alias typeof(i) T;
> static if (is(T T2 : T2*)) {
> T2 i2 = *i;
> Format.convert("{}", i2);
> } else {
> Format.convert("{}", i);
> }
Hi again,
I cannot compile this code
|
April 06, 2009 Re: Format.convert problme | ||||
---|---|---|---|---|
| ||||
Posted in reply to Qian Xu | Qian Xu wrote:
> grauzone wrote:
>
>> Check if the variable is a pointer, and if yes, dereference it:
>>
>> alias typeof(i) T;
>> static if (is(T T2 : T2*)) {
>> T2 i2 = *i;
>> Format.convert("{}", i2);
>> } else {
>> Format.convert("{}", i);
>> }
>
> Hi again,
>
> I cannot compile this code
What exactly are you doing? What do you want to do? Without knowing this, we can only guess. My first reply to you was also just a guess. I thought maybe you had a templated function, and wanted to dump a templated variable, or so.
Anyway, here's a complete version of my example above. It uses Stdout.formatln instead of Format.convert, but this shouldn't matter at all.
import tango.io.Stdout;
void main() {
int v = 55;
int *pv = &v;
//pv (an int pointer) can be exchanged with v (an int),
//and it still works
auto i = pv;
alias typeof(i) T;
static if (is(T T2 : T2*)) {
T2 i2 = *i;
Stdout.formatln("{}", i2);
} else {
Stdout.formatln("{}", i);
}
}
|
April 06, 2009 Re: Format.convert problme | ||||
---|---|---|---|---|
| ||||
Posted in reply to grauzone | grauzone wrote:
> mport tango.io.Stdout;
>
> void main() {
>
> int v = 55;
> int *pv = &v;
>
> //pv (an int pointer) can be exchanged with v (an int),
> //and it still works
> auto i = pv;
>
> alias typeof(i) T;
> static if (is(T T2 : T2*)) {
> T2 i2 = *i;
> Stdout.formatln("{}", i2);
> } else {
> Stdout.formatln("{}", i);
> }
>
> }
Thanks. Now I understood your code. ^^)
|
Copyright © 1999-2021 by the D Language Foundation