Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 07, 2007 Reset class member variables | ||||
---|---|---|---|---|
| ||||
Hi, I have some big classes with lots of member variables that need to be reset to theire initial value. Therefore I thought about a more reliable way to accomplish this, because it's hard to keep track of variables I have added. It also looks bloated to assign all values manually. class Foo { uint x; char[] name = "world"; //problematic: const uint y; char[1024] buffer; void reset() { scope tmp = new typeof(this); foreach(i, x; tmp.tupleof) { this.tupleof[i] = x; } } } The problem is that I have to avoid to try to set const values and static arrays. How can this be done? |
September 07, 2007 Re: Reset class member variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to mandel | mandel wrote: > Hi, > > I have some big classes with lots of member variables > that need to be reset to theire initial value. > Therefore I thought about a more reliable way to > accomplish this, because it's hard to keep track > of variables I have added. > It also looks bloated to assign all values manually. > > class Foo > { > uint x; > char[] name = "world"; > //problematic: > const uint y; > char[1024] buffer; > > void reset() > { > scope tmp = new typeof(this); > foreach(i, x; tmp.tupleof) > { > this.tupleof[i] = x; > } > } > } > > The problem is that I have to avoid > to try to set const values and static arrays. > > How can this be done? Does "this = new typeof(this);" work? -- E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi |
September 07, 2007 Re: Reset class member variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to mandel | mandel wrote:
> Hi,
>
> I have some big classes with lots of member variables
> that need to be reset to theire initial value.
> Therefore I thought about a more reliable way to
> accomplish this, because it's hard to keep track
> of variables I have added.
> It also looks bloated to assign all values manually.
>
> class Foo
> {
> uint x;
> char[] name = "world";
> //problematic:
> const uint y;
> char[1024] buffer;
>
> void reset()
> {
> scope tmp = new typeof(this);
> foreach(i, x; tmp.tupleof)
> {
> this.tupleof[i] = x;
> }
> }
> }
>
> The problem is that I have to avoid
> to try to set const values and static arrays.
>
> How can this be done?
It's a bit horrifying, but this should work:
class Foo
{
uint x;
char[] name = "world";
//problematic:
const uint y;
char[1024] buffer;
void reset()
{
(cast(byte*) this)[0 .. this.classinfo.init.length] =
this.classinfo.init[0 .. $];
}
}
Sean
|
September 07, 2007 Re: Reset class member variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | Yes ,that works (and looks horrific, too). :P Sean Kelly Wrote: > mandel wrote: > > Hi, [..] > > class Foo > > { > > uint x; > > char[] name = "world"; > > //problematic: > > const uint y; > > char[1024] buffer; > > > > void reset() > > { > > scope tmp = new typeof(this); > > foreach(i, x; tmp.tupleof) > > { > > this.tupleof[i] = x; > > } > > } > > } > > > > The problem is that I have to avoid > > to try to set const values and static arrays. > > > > How can this be done? > > It's a bit horrifying, but this should work: > > class Foo > { > uint x; > char[] name = "world"; > //problematic: > const uint y; > char[1024] buffer; > > void reset() > { > (cast(byte*) this)[0 .. this.classinfo.init.length] = > this.classinfo.init[0 .. $]; > } > } > > > Sean |
September 07, 2007 Re: Reset class member variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | Ok, I jumped to early. :/
This method ignores const values that I have set in the ctor:
So, "this() { y = 123; }" would be reset to 0.
Sean Kelly Wrote:
>
> It's a bit horrifying, but this should work:
>
> class Foo
> {
> uint x;
> char[] name = "world";
> //problematic:
> const uint y;
> char[1024] buffer;
>
> void reset()
> {
> (cast(byte*) this)[0 .. this.classinfo.init.length] =
> this.classinfo.init[0 .. $];
> }
> }
>
>
> Sean
|
September 07, 2007 Re: Reset class member variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to mandel | mandel wrote: > Ok, I jumped to early. :/ > This method ignores const values that I have set in the ctor: > So, "this() { y = 123; }" would be reset to 0. Hrm... to deal with that I think you'd have to use foreach and tupleof, but I don't think D 1.0 provides a means for detecting the storage type of a declaration. Ideally, you'd want to do something like this: foreach( pos, inout val; this.tupleof ) { static if( !is( typeof( val ) : const ) ) val = this.classinfo.init.tupleof[pos]; } |
September 07, 2007 Re: Reset class member variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to mandel | mandel schrieb:
> Hi,
>
> I have some big classes with lots of member variables
> that need to be reset to theire initial value.
> Therefore I thought about a more reliable way to
> accomplish this, because it's hard to keep track
> of variables I have added.
> It also looks bloated to assign all values manually.
what about creating a new object everytime you need a fresh one?
i think that new is faster than reinitalise all members - and you get rid of your track problems
(btw: the need of reseting an object could be a design problem)
ciao dennis
|
September 07, 2007 Re: Reset class member variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to dennis luehring | > (btw: the need of reseting an object could be a design problem)
examples:
1# bad example:
int result[];
auto obj = new TheObject();
for( ... )
{
result ~= obj.do_some_thing( some_data );
obj.reset();
}
possible errors: you forgot the .reset() or the reset isn't correct implemented
....
2# better, cleaner way:
int result[];
for( ... )
{
auto obj = new TheObject();
result ~= obj.do_some_thing( some_data );
}
in this case you don't have to safe/repair your object state
possible errors: none
--- design blabla
the obj.reset is (maybe) an hard optimizing step (if the newing IS expensive) or your obj is doing the wrong stuff (in the wrong context?)
|
September 07, 2007 Re: Reset class member variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to dennis luehring | I agree, in many cases it is a design problem
to reset an Object because the problems
introduced by it often outweights the
speed/memory overhead.
But it's nice when the initializing problem
can be done in a foolproof way.
It's nice not to have to sacrifice speed for reliability.
dennis luehring Wrote:
> mandel schrieb:
> > Hi,
> >
> > I have some big classes with lots of member variables
> > that need to be reset to theire initial value.
> > Therefore I thought about a more reliable way to
> > accomplish this, because it's hard to keep track
> > of variables I have added.
> > It also looks bloated to assign all values manually.
>
> what about creating a new object everytime you need a fresh one?
> i think that new is faster than reinitalise all members - and you get
> rid of your track problems
> (btw: the need of reseting an object could be a design problem)
>
> ciao dennis
|
September 07, 2007 Re: Reset class member variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to mandel | mandel wrote:
> It's nice not to have to sacrifice speed for reliability.
In a garbage collected language like D, allocations are extremely fast, potentially several times faster than heap based languages like C/C++.
Using a new instance of the object for each iteration of the loop is really what you're doing /conceptually/, anyway...so, I wouldn't worry about the allocation performance too much unless you've profiled the app and established that it is a bottleneck.
And if that's the case you might want to think about making the object a struct (so a value-type, allocated on the stack) anyway.
Thanks,
Nathan Reed
|
Copyright © 1999-2021 by the D Language Foundation