Jump to content

Componentmanager design C++


ParaToxic
 Share

Recommended Posts

Hei there I have here a little problem with the design of a componentmanager.

My idea is it to make a componentmanager, which loads components and add them to a map.Then the component can be login to the mainthread or to a extern thread, which will be created.

 

Now the problem is, that I use Polymorphism to make a baseclass ( here IComponent ) and inherit this from my first component , the IConsole. When I write ISmartPointer<IComponent> _console(new IConsole); I can add that to the map, because I have always this IComponent as baseclass.So I add it to a map,lets say std::map<std::string,IComponent*> _map;

 

Now I have virtual Functions in the IComponent class like Update,Initialize and Terminate. This virtual functions are defined in the component itself.

But now the problem is that I wan't to call functions from the IConsole itself.There is for example a function LoginVar(...), but I can't write _console.get()->LoginVar(), because I have IComponent as the baseclass and this class haven't got a virtual function LoginVar.....

 

This is a big problem.I want that I can add the Components to a map ( I think that the best way is to make it with Polymorphim), but also call the specific functions...

 

I hope you can help me

Thanks

Link to comment
Share on other sites

Look at that:

#ifndef ICOMPONENTMANAGER_H_
#define ICOMPONENTMANAGER_H_
#pragma once
#include "engine.h"
#include "GlobalDefine.h"
#include "IComponent.h"
#include "IConsole.h"
#include <stdlib.h>
#include <map>
class IComponentManager
{
public:
   IComponentManager(void);
   ~IComponentManager(void);
   IResult Initialize(HINSTANCE hinst);
   IResult Terminate();
   void Update();
   //--------------Console---------------//

   template<typename T>
   inline void Console_LoginVar(std::string const& name, T& ref) { _debugConsole.get()->LoginVar(name,ref); } // That doesn't work because the baseclass hasn't got a function LoginVar, only the IConsole class
   //------------------------------------//

private:
   ISmartPointer<IComponent> _debugConsole;
   std::map<std::string,IComponent*> _mainThread;
   std::map<std::string,IComponent*>::iterator _mainItr;
   std::map<std::string,std::pair<IComponent*,HANDLE>> _externThread;

   void LoginMainThread(std::string s,IComponent* comp);
   void LoginExternThread(std::string s,IComponent* comp);

};

#endif

Link to comment
Share on other sites

If that works, you might want to make a function that will cast it for you, such as

 

iConsole* get_iConsole( BaseClass baseclassIn )
{
// Check for type
if ( baseClassIn->type == TYPE_iCONSOLE )
// return
Return (iConsole *)baseClassIn;
else
// Return
return NULL;
}

 

which will simplify the casting for you and check the inherited item is the right type before trying to call ->LoginVar on it?

 

I wrote a whole gui system based on inheritance and it turned out to be more hassle than it's worth because of all this casting stuff, good luck!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...