Nested templates c++... Possible?

Started by
17 comments, last by me22 14 years, 9 months ago
Hello, I'm trying to wrap my head (and my various objectmanagers ) around a nested template problem... Does anyone know how to solve the following?



template <class T>
class cSingleton : private T
{
   //...
}; 

template <typename A>
class ObjectManager : public cSingleton< ObjectManager >  // <--- !!! "Expected a type, got 'Manager' "  ? 
{
  //...
  std::vector<boost::shared_ptr<A> > items; 
}

struct Object
{
    string name;
};

ObjectManager <Object> manager;  // or should this become  "ObjectManager<Object>::get()->add() etc?


// This on the other hand is allowed...
class MyClass : public cSingleton<MyClass> 
{
  //...
}

//and works like so:
MyClass::get()->add();


The whole idea of my code was to avoid having multiple versions of the singleton class, and simply create a resourceManager class that can contain anything... Is it possible or am I doing it wrong? :D Thanks for your time!
"Game Maker For Life, probably never professional thou." =)
Advertisement
1) Try class ObjectManager : public cSingleton< ObjectManager<A> >
2) Oh god singletons
3) Oh god managers
Quote:Original post by Rasmadrak
template <class T>class cSingleton : private T{   //...}; template <typename A>class ObjectManager : public cSingleton< ObjectManager >  // <--- !!! "Expected a type, got 'Manager' "  ? {  //...  std::vector<boost::shared_ptr<A> > items; }
You can't do that! You have ObjectManager<A> inheriting from cSingleton< ObjectManer<A> >, which in turn inherits from ObjectManager<A>...

Why on earth does your singleton class want to inherit from its derived class? Not to mention, singletons are evil [wink]

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:

Singeltons are evil...


So I've heard... ;)

Anyway, the following did the trick:

Quote:
template < typename A >
class M
Manager : public cSingleton< Manager < A> >


I'm trying to create a threadsafe library and figure singletons was the best (most convenient?) way to go since there's only one interface with the world.

What's so terrible wrong with having...

TextureManager::get()->getTexture("sometexture.jpg");
ModelManager::get()->getModel("somemodel.3ds");

They're also mutex'ed so that only one thread can access it at the same time.
Maybe it's the coffee talking, but how would one handle smooth multithreaded loading/handling otherwise?
"Game Maker For Life, probably never professional thou." =)
What is multithreaded about your code? Does the FooManager load Foo instances in a background thread? Or can the code be called safely from multiple threads? Or something else?
Quote:Original post by Rasmadrak
Quote:Singeltons are evil...

I'm trying to create a threadsafe library and figure singletons was the best (most convenient?) way to go since there's only one interface with the world.

What's so terrible wrong with having...
Apart from the usual arguments against Singletons, you have created a nasty synchronization point by mutex'ing the singleton accessors.

What happens when thread A requests a resource which thread B is still loading? At best you stall thread A until thread B finishes loading, neatly erasing any benefit of multi-threaded loading.
Quote:Maybe it's the coffee talking, but how would one handle smooth multithreaded loading/handling otherwise?
Usually, with message passing, or a futures mechanism.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by rip-off
What is multithreaded about your code? Does the FooManager load Foo instances in a background thread? Or can the code be called safely from multiple threads? Or something else?


Both.

for example, in pseudo code as I'm developing this as we speak:

mainthread:      modelManager.add"box.3ds"); //Return immediatly.      modelManager.getItem("box.3ds")->isPhysical(true);     if (modelManager->isLoaded("box.3ds") )         modelManager->render("box.3ds") ;   //renders without textures if textures aren't loaded yet etc...      worker thread:    modelManager->loadItems();    textureManager->loadItems();phyicsThread:    "check if new physical object requested"    if (modelManager->isLoaded("box.3ds") )           physicsManager->insert("box.3ds");


Each manager automatically unloads objects that's havn't been used for a while, and automatically loads new items.

"Game Maker For Life, probably never professional thou." =)
Quote:Original post by Rasmadrak
I'm trying to create a threadsafe library and figure singletons was the best (most convenient?) way to go since there's only one interface with the world.


Singletons are often one of the worst solutions for creating a threadsafe library as there's only one instance that all threads must share, whereas it's often saner and more efficient for each thread to have have their own copies of things. And unless you're targeting D3D11+ only, your graphics APIs aren't thread safe anyways.
So one would triple the memory requirement and synchronize all the data each loop? That doesn't makes sense to me. :)
Again, I havn't much experience in the multithreading field (yet)...
"Game Maker For Life, probably never professional thou." =)
Quote:Original post by Rasmadrak
So one would triple the memory requirement and synchronize all the data each loop?
Don't think about loops in multithreading - each thread has its own main loop, and they are running at different speeds. In general, synchronizing them kills most of the performance benefit of multiple threads.

Instead you probably want to pass messages between threads, such as LoadObject and ObjectFinishedloading.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement