开发者

C++/W32 Sharing Class - code design question

Here is what i am trying to do.

I have Three Classes:

1) CEngine

2) CLogManager

3) CWindowGL

Ad1. This class 'does' the tricky things to get the game engine going, an application utilizing it, can call only few public members to get the game going -

class CEngine
{
  public:
   CEngine();
   ~CEngine();  // should this go to private?
    bool Init(width,height,...);
    void Destroy();
    void Run();
    bool LoadMap(...);
  private:
    CLogManager *m_pLogManager;    
    CWindowGL   *m_pWindowManager

}


// Example usage
CEngine *Engine=new CEngine;

Engine->Initialize(...)
Engine->LoadMap(...)
Engine->Run()
Engine->Destroy()
delete(Engine)

Ad2. This class controls the logging facility it just allows me to dump some log into the log data file:

class CLogManager
{
  public:
   CLogManager();
   ~CLogManager();
   void Write(const char *fmt,...);
  private:
   FILE *fp;
   std::string m_sFileName; // unique filename generated at the constructor
   SYSTEMTIME m_tSystemTime;

}

Ad3. This class handles the window creation, and pixel format settings, and few other things related to the window itself - nothing else, but it also needs to utilize CLogManager - to dump few informations for debug purposes.

Now the question is:

When a CLogManager constructor is called, class generates a unique filename that is:

m_sFileName="data/logs/enginelog_%i%i%i.txt"; // hour, minute, second

CEngine class in the Init method does:

m_pLogManager = new CLogManager;

and later on it uses it with m_pLogManager->Write(....) to log events. That's ok for CEngine, but i would like to use the same functionality in CWindowGL class and here is the question.

I would like my code to share CLogManager across :

 CEngine
 CWindowGL
 ...
 ...
 ...

and few others that i'll implement.

I can't do this by adding "Instance()" type of method like:

   static CLogManager &am开发者_JS百科p;Instance()
   {
    static CLogManager s_instance; 
    return s_instance;
   }

and calling:

CLogManager::Instance().Write(" LOG MESSAGE ");

As this would cause my CLogManager to generate new filename each time when a constructor is called.

Do i have to

extern CEngine *Engine; 

somewhere to call

Engine->Log(" LOG MESSAGE ")

wrapper everytime or there is something else i can stick to?

I know it is more like a 'code-design' question, but i would like to see how do you guys handle such things.

Normally i would do this with extern, but that would require me to check m_pLogManager!=NULL within a wrapper function to a private member - and just don't know if that's OK.

Maybe there's some other - better approach?

I will be adding few other classes like. TexturesManager - and would like this class to store the actual size of textures loaded and so on, so this would also require me to not to call Instance() to class each time the texture is called - as this would create/destruct the class without storing the needed size / array of textures already loaded...

Uff..

Thanks, hope this is clear.


I can't do this by adding "Instance()" type of method as this would cause my CLogManager to generate new filename each time when a constructor is called.

Actually no, the constructor would be called only once during your program lifetime. The singleton pattern is what you most likely want for your logging class.


What you'll generally find in these situations is a static set of methods that use a singleton underneath. All consumers call the static method which returns the one, single, instance of your logger, which you then call methods on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜