Boost logo

Boost :

From: SeskaPeel (seskapeel_at_[hidden])
Date: 2006-04-26 13:41:53


> It's really hard to parse your requirements, Could you out-line the
> datastructure in code, please?

Then I'll explain what I have today, and how I want to improve it.

First of all, to add a bit of context, I'm working on a 3D engine. There are
a lot of "resources", being Mesh, Character, Camera, Material, etc.

I have a very classic class hierarchy around these:
* Resource: base class, holds the ID of the element (std::string)
* Material: directly derives from Resource
* GeomElem: derives from Resource, that is the base class for geometric
elements, mainly holds the PRS matrix
* Camera: derives from GeomElem, the PRS matrix is the point of view for the
current frame
* RenderElem: derives from GeomElem, intermediate class for geometric
elements that will be rendered, holds a pointer to a Material and a bounding
sphere because from this class we're talking of "volumetric elements"
* Mesh: derives from RenderElem, holds triangles, points, etc.
* Character: derives from RenderElem, has a bone hierarchy for skeletal
animations

And so on. As you understood, Resource is my class A, Material my class B,
RenderElem my class C, Mesh my class D, and Character my class E. On these
resource, I want to be able to query them by ID, delete them, add them, etc.
And I want to be able to provide the final class type (like
GetResource<Mesh>("MyMeshName") to have a faster lookup, or an intermediate
class, like RenderElem, and have a lookup only in the RenderElem derived
classes. That last feature with the intermediate class is what I am missing
at this point.

For the implementation details of my current version:

* I have a simple wrapper to holds all my strongly typed resources, called
ResourceHolder, that contains a std::map<const std::string *, Resource *>.
The key is a pointer to the std::string contained in the value, that is the
actual name of the Resource.

* At startup, I "register" all my resources types using a std::string as
type ID. I have a singleton class:
template <class T> struct TypeToString { std::string m_Name ; } ;
and I instanciate that template for all my Resource derived classes.
This is useful for the next point along with another structure that helps me
construct my correctly typed Resources at loading time.

* Then I have a container of ResourceHolder, that is called ResourceManager,
that holds every resource for the application.

Everything the developer can do is implemented in ResourceManager. Mainly
the GetResource function has a template override that accepts the type as
template argument. This way, I can use the TypeToString singleton to get the
std::string corresponding to the type, and thus find the ResourceHolder
containing all the resources of this type. Then I lookup using the name of
the resource in the correct ResourceHolder to find the desired Resource. If
I don't use the template version of GetResource, then I will iterate over
ALL the ResourceHolders contained in the ResourceManager.

So, I want an über-container that will perform all these lookups using no
type, the final type, or possibly the intermediate type. And I'll need
access to views of these sub-containers, because often I need to use some
for_each algorithm on a specific type (ask all Materials to load their
textures, ask all RenderElems to prepare for rendering,...). This is
something I can perform only on final types at this point.

I don't like my structure, and would be pleased to get rid of it in benefit
of a better one. But if I don't find a way, I'll implement some kind of
TypeToString hierarchy so that I'll be able to know all derived types from
an intermediate one.

As a future improvement, I'll have to use some kind of "scope" in this
structure to sort Resources by "levels" (and I'll have to get rid of the
std::string as IDs too). Any clue on that improvement would be mostly
appreciated too.

Hope this clears things up,

        SeskaPeel.

-----Message d'origine-----
De : boost-bounces_at_[hidden] [mailto:boost-bounces_at_[hidden]] De
la part de Thorsten Ottosen
Envoyé : mercredi 26 avril 2006 18:29
À : boost_at_[hidden]
Objet : Re: [boost] Class views on an associative container

SeskaPeel wrote:
> Hi list,
>
>
>
> I'm looking for a "multi-class container", with access to sub-containers
of
> elements depending on their class. All elements are derived from the same
> base class, and I want to have "views" of containers of final classes, or
> even intermediate classes. This container would be associative, like a
map.
>
> As instance if I have
>
>
>
> // Base class
>
> class A {} ;
>
>
>
> // Intermediate classes
>
> class B : public A {} ;
>
>
>
> // Final classes
>
> class C : public B {} ;
>
> class D : public B {} ;
>
> class E : public A {} ;
>
>
>
> I want a container that can hold any A-derived class,

That is easy.

>on which I could have
> views of sub containers of class C, D or E, and even a sub container of
> intermediate class B.

-Thorsten
_______________________________________________
Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk