Kukatz 3D  0.1
Török Attila szakdolgozata
projects/Kukatz 3D/include/singleton.hpp
00001 /*
00002  * singleton.hpp
00003  * Copyright (c) 2011 - TÖRÖK Attila (torokati44@gmail.com)
00004  *
00005  * This software is provided 'as-is', without any express or implied
00006  * warranty. In no event will the authors be held liable for any damages
00007  * arising from the use of this software.
00008  *
00009  * Permission is granted to anyone to use this software for any purpose,
00010  * including commercial applications, and to alter it and redistribute it
00011  * freely, subject to the following restrictions:
00012  *
00013  *   1. The origin of this software must not be misrepresented; you must not
00014  *      claim that you wrote the original software. If you use this software
00015  *      in a product, an acknowledgment in the product documentation would be
00016  *      appreciated but is not required.
00017  *
00018  *   2. Altered source versions must be plainly marked as such, and must not be
00019  *      misrepresented as being the original software.
00020  *
00021  *   3. This notice may not be removed or altered from any source
00022  *      distribution.
00023  */
00024 
00025 // This file is based on OGRE's OgreSingleton.h which can be found here:
00026 // http://ogre.svn.sourceforge.net/viewvc/ogre/branches/v1-7/OgreMain/include/OgreSingleton.h?revision=9527&view=markup
00027 // Thanks!
00028 
00029 /*
00030 This is a basic easy-to use template base class for the singleton design pattern.
00031 
00032 Usage:
00033 
00034 class MySingleton : public Singleton< MySingleton > // <- important!
00035 {
00036         // your members, functions, etc
00037         // DESTRUCTOR MUST BE VIRTUAL!
00038 };
00039 
00040 You have to instantiate one (and only one) object of MySingleton class.
00041 Preferably with the new operator, because that will create it in the heap,
00042 and with normal declaration it might get out of scope and get destructed.
00043 Also, you will have to delete it when you are done.
00044 Declaring in the main() and in the global scope are OK, as long as MySingleton
00045 in the stack is OK.
00046 These singletons can be instantiated and deleted any number of times, but at most
00047 one instance can exist at any time.
00048 */
00049 
00050 #ifndef SINGLETON_HPP_INCLUDED
00051 #define SINGLETON_HPP_INCLUDED
00052 
00053 template< class T >
00054 class Singleton
00055 {
00056 private:
00057         // These two are intentionally undefined, because they are forbidden on a singleton.
00058         // Maybe I could define them and assert a constant there, but this is just fine, too.
00059         Singleton(const Singleton< T >&);
00060         Singleton& operator = (const Singleton< T >&);
00061         
00062         // msp means my static pointer ...
00063         static T* msp_instance;
00064         
00065 protected:
00066         Singleton();
00067         virtual ~Singleton();
00068         
00069 public:
00070         inline static T& instance();
00071         inline static T* instance_ptr();
00072         
00073         inline T& operator () ();
00074 };
00075 
00076 //template functions have to be defined in every compilation unit...
00077 #include "singleton.inl"
00078 
00079 #endif //SINGLETON_HPP_INCLUDED
 Összes Osztályok