/* attempt to deal with prototype, bootstrap, jquery conflicts */ /* for dropdown menus */

Free!

Image of the Day Submit

IOTD | Top Screenshots

Custom storage class - Look at my code?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

#1PrestoChung  Members

Posted 27 June 2011 - 12:34 PM

Hi
I have created a storage class to store arbitrary objects in linear memory. I think this is essentially a memory allocator but it stores a little information about the object allocated (size in bytes, number of objects)

This was originally meant to be the method of storing Components in an Entity-System.

Usually I hear that the STL is a better idea.
The obvious STL analogue is the std::vector. But the problem is how to store references to std::vector elements?
If you store a pointer to an element, that pointer may become invalid by re-sizing of the vector.
If you store an index, that index may also change.

The other problem is storing generic collections of std::vectors. Because it is a template you cannot have a homogenous collection of std::vectors: each one is a different type.
That is why this storage class is not a template: if it was a template I could not create a structure such as
Storage myStors[10]
if Storage is a template<>

There are two supplemental classes that are to help manage the allocation and ownership of the elements: Range and Rangeset.

Range is just what it sounds like, it is a minimum and maximum value. A Rangeset is a sorted linked-list of Ranges so that ownership of elements need not always be 100% contiguous.

The Storage keeps track of which elements are available with it's own Rangeset, which should be disjoint with the Rangesets of the Entities that have ownership of the objects.

Again I have found that a situation such as this will be necessary to hold object Entities themselves if I want them to be contiguous in memory: they will require references to parents and children which can't be stored as pointers to std::vector elements because of the aforesaid considerations.

Please correct me if I am causing myself too much trouble!

#2ApochPiQ  Moderators

Posted 27 June 2011 - 01:42 PM

Your code is, unfortunately, pretty hard to read; the formatting aside (whitespace is your friend!) I find it full of magic numbers, cryptic conditionals and variable names, and no documentation at all. I don't even know for sure what invariants your classes are meant to uphold, let alone whether or not they manage to do so.

I'd be willing to look through it and clean it up a bit, but unfortunately I already have a job ;-) But if you have some time to make it a bit more readable and document it a bit, I'd be happy to take another glance.
Wielder of the Sacred Wands

#3PrestoChung  Members

Posted 27 June 2011 - 03:25 PM

Thanks for looking. I suppose the lack of whitespace (I'm assuming you mean between functions?) is probably due to my reliance on Visual Studio outlining. Sorry for that.

Here's a summary of the functionality http://dl.dropbox.com/u/78425/stor.txt

#4Zahlman  Members

Posted 27 June 2011 - 06:11 PM

But the problem is how to store references to std::vector elements?

It depends on whether you really need to, and if so, why.

The other problem is storing generic collections of std::vectors.

Again, what are you trying to solve by doing so? What's wrong with a separate vector for each element type?

#5PrestoChung  Members

Posted 28 June 2011 - 05:32 PM

But the problem is how to store references to std::vector elements?

It depends on whether you really need to, and if so, why.

I wish to be able to traverse the data linearly in memory but I also want to be able to have parent/child references to specific elements of that array or other arrays. I think that essentially is what I am trying to do though the two might be opposed.

The other problem is storing generic collections of std::vectors.

Again, what are you trying to solve by doing so? What's wrong with a separate vector for each element type?

I want it this way to make it easy to write managers (I think they are usually called) for types of objects (game Entities)
This way they can derive from a common manager base class and I only need to write a single function for things like 'Clear all entities' even though each derived class might manage different entities, I won't have to re-write the same functionality for each one.

#6smasherprog  Members

Posted 28 June 2011 - 07:28 PM

I am confused, what is it that this code offers? You say you want to be able to traverse the container linearly, but why then not use a std::vector, like the above poster suggests?

Are you creating a flat tree? i.e. creating a tree, and using an array for the tree? If so, the elements will not be contiguous . .

i dunno. . .
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.

#7Zahlman  Members

Posted 29 June 2011 - 04:33 PM

But the problem is how to store references to std::vector elements?

It depends on whether you really need to, and if so, why.

I wish to be able to traverse the data linearly in memory but I also want to be able to have parent/child references to specific elements of that array or other arrays. I think that essentially is what I am trying to do though the two might be opposed.

Ok, so why do you want to be able to have parent/child references? What do you mean by parent/child in this context? I thought we were looking at a sequence, not a tree.

#8PrestoChung  Members

Posted 29 June 2011 - 11:44 PM

But the problem is how to store references to std::vector elements?

It depends on whether you really need to, and if so, why.

I wish to be able to traverse the data linearly in memory but I also want to be able to have parent/child references to specific elements of that array or other arrays. I think that essentially is what I am trying to do though the two might be opposed.

Ok, so why do you want to be able to have parent/child references? What do you mean by parent/child in this context? I thought we were looking at a sequence, not a tree.

Sorry, I did mention it in my first post but it was towards the bottom.

The first case where I have this is:

I have a manager (Subsystem) called Overlay. (Basically it is to display 2d graphics directly to screen space)

I have an Image object (Entity) that has a texture component.

Now, I create a couple of Graphic objects (Entities) that have vertex components and the use of these Graphic objects is to display the original Image object in a couple different places on the screen.

When I decide at some point that I need to get rid of the Image object, I don't want to worry about what Graphic objects are currently displaying it. I would like to simply remove the Image and have the Graphics to be removed implicitly because they are children of the Image.

I haven't had a case where relation in the other direction is needed but an example off the top of my head would be an interaction with one of the children affecting a change in the Parent.

That sort of thing.

#9smasherprog  Members

Posted 30 June 2011 - 12:27 AM

Isnt this why you create a class and have these items within the class? So, when you delete the class, those objects go too?
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.

#10PrestoChung  Members

Posted 30 June 2011 - 12:38 AM

Isnt this why you create a class and have these items within the class? So, when you delete the class, those objects go too?

Don't want to restrict to hard-coded relations such as this where it can be avoided. I can't foresee all the possible combinations of elements esp. if I want to update them dynamically.

Also if the sub-objects are to be processed independently you don't want the cache taken up by the containing class data.

#11ApochPiQ  Moderators

Posted 30 June 2011 - 10:59 AM

I still really don't understand what the need for a custom container is here?

Just set up a ptr_vector<Foo> and be done with it...
Wielder of the Sacred Wands

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.