www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - struct or class

reply "nikki" <nikkikoole gmail.com> writes:
I come from languages that don't offer structs, I have this json 
load function that has to keep some data and intuitively I've 
written a struct, I've read about the differences, heap vs stack, 
value vs reference, but know I think i am overthinking it.

Is this decent:
	bool loadFromFile (string path)
	{
		auto data = readText(path);
	        JSONValue parsed = parseJSON(data);
		
		struct AtlasSpriteData
		{
			SDL_Rect clipRectangle;
			int xOffset;
			int yOffset;
		}
		AtlasSpriteData[string] dict;

	        foreach( string name, value; parsed["frames"] ){
			SDL_Rect clipRectangle;
                         auto spriteSourceSize = 
value["spriteSourceSize"];
			clipRectangle.x = to!int(frame["x"].toString());
			clipRectangle.y = to!int(frame["y"].toString());
			clipRectangle.w = to!int(frame["w"].toString());
			clipRectangle.h = to!int(frame["h"].toString());
			int xOffset = to!int(spriteSourceSize["x"].toString());
			int yOffset = to!int(spriteSourceSize["y"].toString());
			auto data = AtlasSpriteData(clipRectangle, xOffset, yOffset);
			dict[name] = data;
		}

Or should I use a class for that AtlasSpriteData?
reading about it I get the impression everytime I'll look up data 
from that dictionary data will get copied ?
Aug 24 2014
next sibling parent Rikki Cattermole <alphaglosined gmail.com> writes:
On 24/08/2014 11:56 p.m., nikki wrote:
 I come from languages that don't offer structs, I have this json load
 function that has to keep some data and intuitively I've written a
 struct, I've read about the differences, heap vs stack, value vs
 reference, but know I think i am overthinking it.
Here's a simple way of working it out. If you're using arrays of a type, use classes. If you're storing lots of data inside it, classes. If you need inheritance, classes. If you have simple data for returning/argument passing then struct. Basically, small, short lived allocations stack. Long lived, large allocations, classes. At least that's my opinion.
Aug 24 2014
prev sibling next sibling parent "FreeSlave" <freeslave93 gmail.com> writes:
On Sunday, 24 August 2014 at 11:56:44 UTC, nikki wrote:
 I come from languages that don't offer structs, I have this 
 json load function that has to keep some data and intuitively 
 I've written a struct, I've read about the differences, heap vs 
 stack, value vs reference, but know I think i am overthinking 
 it.

 Is this decent:
 	bool loadFromFile (string path)
 	{
 		auto data = readText(path);
 	        JSONValue parsed = parseJSON(data);
 		
 		struct AtlasSpriteData
 		{
 			SDL_Rect clipRectangle;
 			int xOffset;
 			int yOffset;
 		}
 		AtlasSpriteData[string] dict;

 	        foreach( string name, value; parsed["frames"] ){
 			SDL_Rect clipRectangle;
                         auto spriteSourceSize = 
 value["spriteSourceSize"];
 			clipRectangle.x = to!int(frame["x"].toString());
 			clipRectangle.y = to!int(frame["y"].toString());
 			clipRectangle.w = to!int(frame["w"].toString());
 			clipRectangle.h = to!int(frame["h"].toString());
 			int xOffset = to!int(spriteSourceSize["x"].toString());
 			int yOffset = to!int(spriteSourceSize["y"].toString());
 			auto data = AtlasSpriteData(clipRectangle, xOffset, yOffset);
 			dict[name] = data;
 		}

 Or should I use a class for that AtlasSpriteData?
 reading about it I get the impression everytime I'll look up 
 data from that dictionary data will get copied ?
Your struct instance will occupy only 24 bytes. It's ok even if you will copy it. I would avoid heap allocation in this case. Also what is 'frame' variable? I don't see local declaration of it. Or you just forgot to replace 'value' with 'frame'. Does not JSONValue.integer fit in this case instead of to!int(JSONValue.toString()) ? Reading does not perform copy if you access struct directly as dict[name].some_field. Copying is performed only if you pass struct by value or assign it to variable.
Aug 24 2014
prev sibling next sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sun, 24 Aug 2014 11:56:42 +0000
nikki via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

 Or should I use a class for that AtlasSpriteData?
 reading about it I get the impression everytime I'll look up data=20
 from that dictionary data will get copied ?
this will copy: auto sd =3D dict[0]; this will copy: foreach (sd; dict) { ... } this will not: const *sd =3D &dict[0]; this will not: foreach (ref sd; dict) { ... } hope you got the idea.
Aug 24 2014
prev sibling next sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sun, 24 Aug 2014 11:56:42 +0000
nikki via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

 Or should I use a class for that AtlasSpriteData?
 reading about it I get the impression everytime I'll look up data=20
 from that dictionary data will get copied ?
p.s. this will not copy: auto sd =3D "mysprite00" in dict; 'sd' is of type 'AtlasSpriteData*' here.
Aug 24 2014
prev sibling parent "Kiith-Sa" <kiithsacmp gmail.com> writes:
On Sunday, 24 August 2014 at 11:56:44 UTC, nikki wrote:
 I come from languages that don't offer structs, I have this 
 json load function that has to keep some data and intuitively 
 I've written a struct, I've read about the differences, heap vs 
 stack, value vs reference, but know I think i am overthinking 
 it.

 Is this decent:
 	bool loadFromFile (string path)
 	{
 		auto data = readText(path);
 	        JSONValue parsed = parseJSON(data);
 		
 		struct AtlasSpriteData
 		{
 			SDL_Rect clipRectangle;
 			int xOffset;
 			int yOffset;
 		}
 		AtlasSpriteData[string] dict;

 	        foreach( string name, value; parsed["frames"] ){
 			SDL_Rect clipRectangle;
                         auto spriteSourceSize = 
 value["spriteSourceSize"];
 			clipRectangle.x = to!int(frame["x"].toString());
 			clipRectangle.y = to!int(frame["y"].toString());
 			clipRectangle.w = to!int(frame["w"].toString());
 			clipRectangle.h = to!int(frame["h"].toString());
 			int xOffset = to!int(spriteSourceSize["x"].toString());
 			int yOffset = to!int(spriteSourceSize["y"].toString());
 			auto data = AtlasSpriteData(clipRectangle, xOffset, yOffset);
 			dict[name] = data;
 		}

 Or should I use a class for that AtlasSpriteData?
 reading about it I get the impression everytime I'll look up 
 data from that dictionary data will get copied ?
In this case class makes sense (assuming AtlasSpriteData has few instances that will be shared around a lot). But you could also use a pointer to a struct, especially if you manually allocate it and want to avoid the GC. Also, you can read data from the associative array by reference (basic example, no error checking): ref AtlasSpriteData spriteData(string name) { return dict[name]; }
Aug 24 2014