• Advertisement
  • entries
    707
  • comments
    1173
  • views
    435238

I give up...

Sign in to follow this  

84 views

I give up. I've got poison ivy again -_-. I've washed all of my clothes, blankets, pillow covers, pillows, etc. so I have no clue how I keep getting it (it goes away and I have it again in less than a week.)

Thanks to JohnHattan for pointing out that my header is unreadable in the default color scheme, so I'm going to fix that ASAP.

I really need to do some in-depth studying of C++. I know the language pretty much like English - I know a lot/almost all of it but I have no clue what half of it means. The v-table is a good example. I can do a lot of stuff with template programming, but I have no idea how the v-table works; so I have no clue if my design is a good idea or a horrid idea. With the knowledge that I have of the v-table (which is next to nil), I'm thinking my Singleton class may not be the best solution (I know, singletons are evil; whatever), but I'm not sure. Here's the code for it:


namespace dft
{
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[DESCRIPTION]
[MEMBERS]
[REMARKS]
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

template class Singleton
{
class ObjAccessor
{
static TYPE* Object;
public:
ObjAccessor()
{
Object = 0;
}

~ObjAccessor()
{
delete Object;
}

TYPE* operator->()
{
if(!Object)
{
Object = new TYPE;
if(!Object)
return 0;
if(!Object->Created)
{
if(!Object->OnFirstAccess())
{
delete Object;
return 0;
}
Object->Created = true;
}
}
return Object;
}
};

protected:
bool Created;

Singleton()
{
Created = false;
}
public:
static ObjAccessor Accessor;

virtual ~Singleton()
{
}

virtual bool OnFirstAccess() { return true; }
};

template typename Singleton::ObjAccessor Singleton::Accessor;
template TYPE* Singleton::ObjAccessor::Object = 0;
}





And an example:

#include
#include "dftSingleton.h"
#include "dftMessageHandler.h"

class Engine;
class Engine : public dft::Singleton
{
HWND WndHandle;
public:
dft::EventHandlerDelegate MessageHandlers;

Engine(){}
~Engine(){}

void Test()
{
MessageBox(WndHandle, "ERROAR!!!", "", MB_OK);
}

bool OnFirstAccess()
{
WNDCLASS WndClass;
ZeroMemory(&WndClass, sizeof(WNDCLASS));
WndClass.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
WndClass.hCursor = LoadCursor(0, IDC_ARROW);
WndClass.hIcon = LoadIcon(0, IDI_APPLICATION);
WndClass.hInstance = GetModuleHandle(0);
WndClass.lpfnWndProc = DefWindowProc;
WndClass.lpszClassName = "DragonForgeTechnology[2DGE]";
WndClass.style = CS_DBLCLKS;
if(!RegisterClass(&WndClass))
return false;

WndHandle = CreateWindow(WndClass.lpszClassName, "DragonForge Technology - 2D Game Engine", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 800, 600, 0, 0, GetModuleHandle(0), 0);
if(!WndHandle)
return false;
return true;
}
};

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
Engine::Accessor->Test();
return 0;
}





This is obviously just example code. The window will be created (OnFirstAccess() is called before Test() is) and then the messagebox is shown.

I also added a conditional to my OnFirstAccess() (checking to make sure it hasn't been created) so that calling OnFirstAccess() several times in a row won't cause any issues.

Also, I realize there's a cyclic thing there (where you could continue to do Accessor->Accessor->Accessor->...), but I don't think that will be a problem (even if you didn't know the system you should be able to figure out soon enough what's going on; if you can't, ask your buddies to take you snipe hunting.)

As always, questions, comments, and suggestions are always welcome.

I take back what I said here; I didn't report him or anything and they can't be watching constantly.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement