Passing a pointer w/o an instance.

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
MrRage
Knows some wx things
Knows some wx things
Posts: 35
Joined: Fri Mar 11, 2005 3:43 am

Passing a pointer w/o an instance.

Post by MrRage »

I
upCASE
Moderator
Moderator
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE »

Hi!
Not really sure what you want to do, but would a singleton pattern be a solution? (http://gethelp.devx.com/techtips/cpp_pr ... in0200.asp)
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
MrRage
Knows some wx things
Knows some wx things
Posts: 35
Joined: Fri Mar 11, 2005 3:43 am

Post by MrRage »

Thanks for the link upCASE,

A singleton pattern is pretty much what I am trying to do. My problem is I just don
upCASE
Moderator
Moderator
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE »

Hi!
I'm not sure if I got the problem correctly figured out..
[quote="MrRage"]
A singleton pattern is pretty much what I am trying to do. My problem is I just don
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
MrRage
Knows some wx things
Knows some wx things
Posts: 35
Joined: Fri Mar 11, 2005 3:43 am

Post by MrRage »

Sorry for the confusion, it took me a while to figure out how to word this and I wrote a simple working example so you can see what I
upCASE
Moderator
Moderator
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE »

Hi!
Sorry for not posting earlier, but I had a day off yesterday.

I had a look at the code and I made up something on my mind which might work. It's not actually "better" or easier as the stuff that you wrote, but maybe it gives you some new ideas. I modifed you code a bit (corrected the singleton stuff, as in a singleton the ctor is always private).

MyApp.cpp

Code: Select all

#include <MyLib/MyLib.h>

using namespace myLib::sys;

class MyApp : public myLib::sys::Application {
public:
  // Simple constructor... not really used
  MyApp() : Application(){
	Application::setApp(this);
  }

  // Called to set the state
  bool init(){
    returnState = 64;
    return true;
  }
};
MyLib.h

Code: Select all

#ifndef _MY_LIB_DEF_
#define _MY_LIB_DEF_

// Include Files
#include <windows.h>

#define SETUP_APP(appname) \
	new appname;

// Namespace and Class
namespace myLib{
  namespace sys{
    class Application{
    public:
      static Application* getInstance();        //
      virtual bool init() = 0;                  // Init Rutine
      int  run();                               // Processes
    private:
	Application(Application* newApp);
        static Application* pInstance;
    protected:
      int returnState;
    };
  }
}


// Windows MainLoop
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow);

#endif 
MyLib.cpp

Code: Select all

#include "MyLib.h"

using namespace myLib::sys;

Application* Application::pInstance = NULL;

Application::Application(Application* newApp){
  if(pInstance == NULL) pInstance = newApp;
  returnState = -2;
}

int Application::run(){
  return returnState;
}

Application* Application::getInstance(){
  return pInstance;
}

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{                                                           
  SETUP_APP(MyApp)
  myLib::sys::Application* app = myLib::sys::Application::getInstance();
  if(app->init() != true) return -1;
  return app->run();
}   
Note that I did not really try this out.
I replaced the extern method with a simple macro that creates a new instance of MyApp. When calling the ctor of MyApp, it should set itself as the instance for Application. Again, I didn't test it.

One thing noticed is that this might not be waht you want in a lib. You have a WinMain in your lib, which should be fine, but you declare an instance of some Application derived class in it. So when using the lib the derive class will allways have to use the name you used in WinMain. But I guess what you want is that you have a class called Application in your lib, write some Application derived class like MyApp and a concrete class and then call a macro from a third file to actually start WinMain using the Application derived class. In this case it would be more useful to define a macro for the whole WinMain() stuff and pass it the name of the Application derived class you want to use.
Maybe this works with a macro like this in MyLib.h

Code: Select all

#define SETUP_MAIN(appname) \
int WINAPI WinMain(HINSTANCE hInstance,  HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) \
{ \
  new appname;  \
  myLib::sys::Application* app = myLib::sys::Application::getInstance(); \
  if(app->init() != true) return -1; \
  return app->run(); \
}   
You could then use SETUP_MAIN() with any class you like as long as it is known. That way WinMain() will be not be defined in your Appliction class, but in a concrete code context. In that case it could be more wise to use setApp() again and call it in WinMain(). It would be transparent to the user of the lib.

I hope I made myself clear on this. Hope that I could help a little. For comments or questions: Please post! :D
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
MrRage
Knows some wx things
Knows some wx things
Posts: 35
Joined: Fri Mar 11, 2005 3:43 am

Post by MrRage »

Thanks a lot for all the help upCASE. I
Post Reply