True Axis Physics SDK 1.2.0.1 Beta Documentation
www.trueaxis.com

Singleton.h

00001 //---------------------------------------------------------------------------------
00002 // File Name: Singleton.h
00003 // Description: 
00004 //      Requires the class being made a singleton to have a default
00005 //      constructor and default Initialise function.
00006 //
00007 // Copyright (C) 2004 - 2006 True Axis Pty Ltd, Australia. 
00008 // All Rights Reserved.
00009 //
00010 // History:
00011 //      Created File.
00012 //---------------------------------------------------------------------------------
00013 
00014 #ifndef TA_SINGLETON_H
00015 #define TA_SINGLETON_H
00016 
00017 #ifndef TA_DEBUG_H
00018 #include "Debug.h"
00019 #endif // TA_DEBUG_H
00020 
00021 #ifndef TA_MEMORYMGR_H
00022 #include "MemoryMgr.h"
00023 #endif // TA_MEMORYMGR_H
00024 
00025 namespace TA
00026 {
00027     
00028 // Same as SINGLETON_DEFINE but leaves the implementer of the class to do the CreateInstance function
00029 #define SINGLETON_DEFINE_OVERRIDE_CREATE(Type)                      \
00030 public:                                                             \
00031     static void TAC_CALL DestroyInstance();                     \
00032     static Type& TAC_CALL GetInstance()                         \
00033     {                                                               \
00034         TA_ASSERT_MSG(s_p##Type, "Singleton("#Type")::GetInstance. Not initialised");   \
00035         return *s_p##Type;                                          \
00036     }                                                               \
00037     static Type* TAC_CALL GetInstancePtr() {    return s_p##Type; } \
00038 private:                                                            \
00039     static Type* s_p##Type;                                         \
00040 public:
00041 
00042 // Define a class as a singleton. Place inside the class definition. Users should reset public, private or protected after using this macro
00043 #define SINGLETON_DEFINE(Type)                                      \
00044 public:                                                             \
00045     static void TAC_CALL CreateInstance();                          \
00046     friend class MemoryMgr;                                         \
00047     SINGLETON_DEFINE_OVERRIDE_CREATE(Type)
00048 
00049 
00050 #define SINGLETON_IMPLEMENT_OVERRIDE_CREATE(Type)                   \
00051 Type* Type::s_p##Type;                                              \
00052 void Type::DestroyInstance()                                        \
00053 {                                                                   \
00054     if (!s_p##Type)                                                 \
00055         return;                                                     \
00056     TA_MEMORY_MGR_DELETE(s_p##Type, Type);                          \
00057     s_p##Type = 0;                                                  \
00058 }
00059 
00060 #define SINGLETON_IMPLEMENT(Type)                                   \
00061 SINGLETON_IMPLEMENT_OVERRIDE_CREATE(Type)                           \
00062 void Type::CreateInstance()                                         \
00063 {                                                                   \
00064     if (s_p##Type)                                                  \
00065         return;                                                     \
00066     TA_MEMORY_MGR_NEW(s_p##Type, Type);                             \
00067     s_p##Type->Initialise();                                        \
00068 }
00069 
00070 }
00071 
00072 #endif // TA_SINGLETON_H


© Copyright 2004-2006 TRUE AXIS PTY LTD Australia. All rights reserved.