Jump to content
  • Advertisement
Sign in to follow this  
OrangyTang

Name/pattern for generic attachable data?

This topic is 4681 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a Skeleton object manages a bunch of Bone objects. Now at a later point I have a bunch of BoneSprites objects, each of which are attached to a bone object. BoneSprites are in a different package and have lots of game-specific info so bones shouldn't know anything about them. Currently this means that each BoneSprite references it's attached Bone. But now I need a mapping of Bone objects to their relevant BoneSprite (the reverse relationship). I could use a hash map but I'm already holding a similar data structure so it would be unnessisary duplication of code and behaviour. So the next method would be a generic 'data' field in the Bone class to let the user attach arbitrary data along with it - in C that'd be a void*, in Java an Object. Obviously this isn't a new idea and I'm sure it's got a proper name, does anyone know what it's called?

Share this post


Link to post
Share on other sites
Advertisement
Proper name?

Don't think so. If you were going to attach a variable and generic amount of data to an object, if the data is not going to be altered, just use a char* and allocate a bunch of mem. This is probably what you meant by the void*, except using a char* is much nicer because a char is a byte long, and that makes reading select parts of the mem, soooo much easier, instead of doing a lot of casting.

If you do want to go with a map solution, there's always std::map for C++ (which I'm assuming that you're using, because you haven't specified, but you did talk about C).

I once wrote a FileManager class that would be able to load files into memory, and return an integer, which was used as the handle to access the file's data.

I did this: "map<int, File*> f_map;", which declares a map of integers as the key, and the File* as the data, so you can access the File* that you want like this: "f_map[0]", "f_map[5]", and so on and so forth. Both template parameters can be anything. You can have the key as a string object. This is how you might do it: "map<Bone*, BoneSprite*>", but, this only really works if you're not doing a lot of deleting of either.

Share this post


Link to post
Share on other sites
Quote:
Original post by Endar
Don't think so. If you were going to attach a variable and generic amount of data to an object

No no, I'm attaching a generic pointer to an existing data structure. Not just an arbitrary amount of data. Users are then free to point this at whatever game-specific data they want to.

Quote:
if the data is not going to be altered, just use a char* and allocate a bunch of mem. This is probably what you meant by the void*, except using a char* is much nicer because a char is a byte long, and that makes reading select parts of the mem, soooo much easier, instead of doing a lot of casting.

Use of void* in C like this is quite common. char* would have no benifit and only add an additional type where none is known.

Quote:
If you do want to go with a map solution, there's always std::map for C++ (which I'm assuming that you're using, because you haven't specified, but you did talk about C).

This is actually Java (hence this will actually be an additional Object reference). Lack of a suitable map is not a problem, the problem is avoiding duplicate code.

Quote:
I once wrote a FileManager class that would be able to load files into memory, and return an integer, which was used as the handle to access the file's data.

I did this: "map<int, File*> f_map;", which declares a map of integers as the key, and the File* as the data, so you can access the File* that you want like this: "f_map[0]", "f_map[5]", and so on and so forth. Both template parameters can be anything. You can have the key as a string object. This is how you might do it: "map<Bone*, BoneSprite*>", but, this only really works if you're not doing a lot of deleting of either.

Handles are a totally seperate area. I don't need a handle (the underlying object (in this case, Bone) can and will be used directly, I just need to attach additional data at run time.


Please, read before replying instead of just latching on to a few keywords. [rolleyes]

Share this post


Link to post
Share on other sites
My reflex in this kind of situation is to make the skeleton a template class, the parameter of which is the contents of the bones.

So, if you want your bones to contain objects of types A, B and C:

struct BoneContents { A a; B b; C c; };

typedef TSkeleton<BoneContents> Skeleton;
typedef TBone<BoneContents> Bone;

This is compile-time behavior though. I guess you could create a registering scheme for data, performing all necessary casts internally, and being type-safe by storing type information. But C++ does not have template virtual functions :( so to store type information you need RTTI.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
My reflex in this kind of situation is to make the skeleton a template class, the parameter of which is the contents of the bones.

Yes, that would work (other than the minor cavet that I'm working in Java). It does give additional type safety (which is the traditional problem with void*) but it's semantically the same otherwise. I could use 1.5's Generics, but for various reasons I'd like to avoid that. Given that its just for my use I'll probably be safe enough without the explicit type safety and instead rely on some runtime asserts.

But still I'm lacking a name for it. Considering how common this is I'm sure it's got one...

Share this post


Link to post
Share on other sites
OrangyTang:
Quote:

No no, I'm attaching a generic pointer to an existing data structure. Not just an arbitrary amount of data. Users are then free to point this at whatever game-specific data they want to.

That is a much better description of what you are trying to do than "So the next method would be a generic 'data' field in the Bone class to let the user attach arbitrary data along with it"
Quote:

Use of void* in C like this is quite common. char* would have no benifit and only add an additional type where none is known.

You are absolutely right. The char* would have had benefit if you had attatched an arbitrary amount of data.
Quote:

This is actually Java (hence this will actually be an additional Object reference). Lack of a suitable map is not a problem, the problem is avoiding duplicate code.

See, you didn't actually specify that you were programming in Java. You referred to both C and Java in your post, but never said.
Quote:

No no, I'm attaching a generic pointer to an existing data structure. Not just an arbitrary amount of data. Users are then free to point this at whatever game-specific data they want to.

This is actually Java (hence this will actually be an additional Object reference). Lack of a suitable map is not a problem, the problem is avoiding duplicate code.

If you had added these two pieces of info to your original post, you probably would have gotten a better answer from me.

Edit: Is the word you're looking for "variant"?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!