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 [source]Storage myStors[10][/source] 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!
Here is a link to the code (about 850 lines): http://dl.dropbox.com/u/78425/stor.h
Custom storage class - Look at my code?
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.
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.
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
Here's a summary of the functionality http://dl.dropbox.com/u/78425/stor.txt
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. [/quote]
Again, what are you trying to solve by doing so? What's wrong with a separate vector for each element type?
[quote name='PrestoChung' timestamp='1309199668' post='4828357']
But the problem is how to store references to std::vector elements?
It depends on whether you really need to, and if so, why.[/quote]
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?
[/quote]
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.
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. . .
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. . .
[quote name='Zahlman' timestamp='1309219911' post='4828466']
[quote name='PrestoChung' timestamp='1309199668' post='4828357']
But the problem is how to store references to std::vector elements?
It depends on whether you really need to, and if so, why.[/quote]
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.
[/quote]
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.
[quote name='PrestoChung' timestamp='1309303951' post='4828867']
[quote name='Zahlman' timestamp='1309219911' post='4828466']
[quote name='PrestoChung' timestamp='1309199668' post='4828357']
But the problem is how to store references to std::vector elements?
It depends on whether you really need to, and if so, why.[/quote]
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.
[/quote]
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.
[/quote]
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.
Isnt this why you create a class and have these items within the class? So, when you delete the class, those objects go too?
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement