Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Zipster

Generic Container

This topic is 5758 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

What I''m looking for is a container that can hold any type of data. However, unlike a vector, list, and other templated containers, I need it to hold all the different types of data at once. And it needs to be type-safe The first idea that came to my head was that this container class would actually wrap a vector (or actually be templated itself to accept what type of container to wrap) and use this to hold a list of pointers. The pointers would point to a base object, from which all other object types will be derived. However, I don''t want to implement a custom derived class everytime we have a new object type we wish to maintain. I want these derived classes to be templated so that we can pass the different types "on-the-fly" (via a templated function). These derived classes would wrap an instance of that type and contain simple Set and Get functions. My problems arise when I want to access these objects from the base class pointer I''m storing. I can''t have any virtual functions in the base class, because the derived classes actually use templated functions and variables. I can''t template the base class, because then I don''t have any method of storing a generic pointer to any type. My only solution would be to downcast from the base pointer to the derived class type and call a function, but now it isn''t type-safe. Am I completely missing something or is this more complicated than it looks? Technically I could implement the whole thing without templates or type-safety and make it "dirty", but I don''t want to do that. Hopefully I''m not completely off base here

Share this post


Link to post
Share on other sites
Advertisement
When you''re accessing an element in that container, will you know what type it is? If so, is there perhaps a better design for this?

I suppose you could have a vector of any, though.

Share this post


Link to post
Share on other sites
Technically you should , but who knows. I want it to be type-safe though, so if you try to access an element and assign it or set it to a type that doesn't match or it can't cast to, it lets you know with a compile error.

On second thought, I beginning to doubt templates can handle this the way I want :| It's like a mixture between templates and polymorphism...

And for now I'm just trying to avoid other libraries.

[edited by - Zipster on September 14, 2002 12:59:30 AM]

Share this post


Link to post
Share on other sites
Why do you need to have all the types of data in one container? How would you know which elements are which types? I''d suggest thinking about your design, or sharing your situation so we can help.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
your problem will never be solved in any programming language ever. Think about this, you are deciding the type at run time (based on what you put into the container), yet you want it to check the type at compile time? The compiler can''t predict the future. Plus even if it could, what kind of code would it generate? What if different user interactions changed the types of the objects stored. How would your compiler that can see the future code that without resorting to a basically near infinite number of special cases? (if it is sept 17th 6:30 am and user equals Tom and he just clicked "ok" then type equals int, else if...)

Ok so it is pretty clear that you need to rethink your design. This question is commonly asked when you have a situation where you have a bunch of things to call one function on and only some of them should have a certain other function called. Simple, keep two lists of pointers. Just put a little work into keeping it synched (perhaps when an object is destroyed have its destructor remove it from all lists)

Share this post


Link to post
Share on other sites
OLE Automation Variant is your new buddy. No clue what language you''re using, but at least in VB, it''d be easy to define

Dim Something(50) as Variant

Now, Variants in C++ would be more of a pain in the ass, and I''d suggest using the MFC wrapper class COleVariant(Or write your own). But I''d definitely suggest that you find some other way to store your data, because OLE Variants are definitely _not_ efficient, and making a general "hold-everything" array could introduce more complications than it''s worth.

Share this post


Link to post
Share on other sites
We need more context. This is probably a bad idea or unnecessary.

Btw, you can freely cast between pointers(void* is usually used as an intermediate, but reinterpret_cast will convert any pointer to any other), but not regular types. Then you make the dereferencing transparent, and it looks like you''re storing the variables themselves, rather than their pointers.

If you really need this, implement something similar to a vector that stores void pointers, and the calls to retrieve something from the container casts and then dereferences the pointer. Perhaps storage in the container copies the object into allocated space, and stores a reference to this new object.

Perhaps you could store some sort of type id with the object itself, and then convert only to that type, but this is not really necessary, unless you really need or want type safety.

You probably don''t need type safety in this container, only after you''ve cast the members of this container to their respective types. You''re gonna know beforehand what types each member is, right? If not, you''re probably asking the wrong question. If so, you can be sure that your typecasts from the container are meaningful. As long as you take especial care to make sure these casts are correct, you shouldn''t need typesafety.

Share this post


Link to post
Share on other sites
I've been thinking about this, and I decided to drop the type-safety requirement. Type-safety would require the program to know the type at compile-time, but I'm trying to store things through base pointers... wouldn't work. It's like trying to use the advantages of templates with the advantages of polymorphism... I guess it is a crazy idea

So as it stands, I'm probably just going to implement a templated member function of the container class that downcasts the base pointer to the passed types (so BaseClass* to DerivedClass<T>*, where T is the type passed) using something like a dynamic_cast, and then call the derived class's Get function. If we try to downcast to the wrong type, then the worst that could happen is a crash, right?

Just in case anyone is curious, I wanted to have a container that mapped a string to a variable - so in essence, I wouldn't even be wrapping a vector or list but really a map. Ideally, I wanted to be able to do something like this:

float var = MyContainer["ParticleSpeed"];

And have it check the types as well. In retrospect, I was being kinda naive - How the hell is it supposed to do that?! So now I'm just going to have to do something like this:

float var = MyContainer.Get<float>("ParticleSpeed");

And if the types don't match, then so be it

[edited by - Zipster on September 15, 2002 5:58:12 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I don''t know: if you limited the types before hand that it could contain, then you could do

  
float x = ScriptVars["x"];

but that''s only if you limit beforehand the types that it can contain.
The real reason you can''t do above with any type is because the compiler wouldn''t be able to figure out what type you mean..coerision (sp?) to different types etc ..

if you could live with:

  
vector<multi_type<typelist_cons<float,char,int> > > v;

then you could do it.

Share this post


Link to post
Share on other sites

  • 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!