
#ifndef __WFX_LANG_SINGLETON_H__
#define __WFX_LANG_SINGLETON_H__

#include <WFX/WFXIncludes.h>

namespace WFX {

    namespace lang {

        /**
         * pretty standard singleton template
         */
        template<typename T>
        class Singleton {
            public:
                Singleton() {
                    assert(!Instance);
                    //use a cunning trick to get the singleton pointing to the start of the whole, rather than
                    //the start of the Singleton part of the object
                    int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1;
                    Instance = (T*)((int)this + offset);
                }

                ~Singleton() {
                    assert(Instance);
                    Instance = NULL;
                }

                static T& getInstance() {
                    assert(Instance);
                    return *Instance;
                }

                static T* getInstancePtr() {
                    assert(Instance);
                    return Instance;
                }
            private:
                /** pointer to the instance */
                static T* Instance;
        };
        template <typename T> T* Singleton <T>::Instance = NULL;

    }

}

#endif //include guard


