Two Singleton modes for derivation (take TextureMgr as an example)

Source: Internet
Author: User

Two Singleton modes for derivation (take TextureMgr as an example)

SingletonAs the name implies, the singleton mode is literally the singleton mode, which is in the C ++ program.

A commonly used design mode, especially the entire software such as file manager and Texture Manager.

When you only need a unique instance to manage all resources, the value of this model can be reflected.

The following describes two Singleton modes for derived management:

The first is Singleton of the Gof version. The Code is as follows:

// [Singleton_Gof.h] # pragma oncetemplate
 
  
Class Singleton_Gof {protected: static T * ms_Singleton; Singleton_Gof () {assert (! Ms_Singleton );}~ Terminate () {assert (ms_Singleton); ms_Singleton = NULL;} private: Singleton_Gof (const success &); // prevents const Singleton_Gof & operator = (const Singleton_Gof &); public: static T * getSingletonPtr () {if (! Ms_Singleton) ms_Singleton = new T (); return ms_Singleton;} static T & getSingleton () {if (! Ms_Singleton) ms_Singleton = new T (); return (* ms_Singleton) ;}}; template
  
   
T * Singleton_Gof
   
    
: Ms_Singleton = NULL;
   
  
 

The principle is to direct the static Singleton instance to the T object of the derived class when Initialization is required,

Similarly, there is a second Singleton. The Code is as follows:

 

//【Singleton.h】#pragma once#include 
 
  template
  
   class Singleton{private:Singleton(const Singleton
   
    &);Singleton& operator=(const Singleton
    
      &);protected:static T* ms_Singleton;Singleton(){assert(!ms_Singleton);#if defined( _MSC_VER ) && _MSC_VER < 1200  int offset = (int)(T*)1 - (int)(Singleton 
     
      *)(T*)1;ms_Singleton = (T*)((int)this + offset);#elsems_Singleton = static_cast< T* >( this );#endif};~Singleton(){assert(ms_Singleton); ms_Singleton = NULL;}public:static T& getSingleton(){if(!ms_Singleton)new T(); return (*ms_Singleton);}static T* getSingletonPtr(){if(!ms_Singleton)new T(); return ms_Singleton;}};template
      
       T* Singleton
       
        ::ms_Singleton = NULL;
       
      
     
    
   
  
 

 

The code in Singleton () has appeared in both OGRE and game programming essence. Here the memory alignment of the VC compiler is involved, so I will not explain it in detail,

 

Then we can use the Singleton () code above to derive a Texture Manager. Here I roughly wrote:

 

// [TextureMgr. h] # include
 
  
# Include
  # Include
   
    
# Include "Singleton. h "# include" Singleton_Gof.h "using namespace std; // Textureclass Texture {private: string m_Name; public: Texture (const string name): m_Name (name) {load ();};~ Texture () {unload ();} const string & getName () const {return m_Name;} bool load () {cout <"loads a Texture:" + m_Name <
    
     
TexMap; typedef texMap: iterator texMapIter; # define SAFE_DELETE (p) {if (p) {delete p; p = NULL ;}// TextureMgrBaseclass TextureMgrBase {texMap m_TiMap; public: texture * getTexture (const string name) {texMapIter iter = m_TiMap.find (name); if (iter = m_TiMap.end () {Texture * tex = new Texture (name ); m_TiMap.insert (std: make_pair (name, tex); return tex;} else {return iter-> second ;}} TextureMgrBase () {cout <"TextureMgrBase constructor" <
     
      
Second);} cout <"TextureMgrBase destructor" <
      
        {Public: TextureMgr () {cout <"TextureMgr constructor" <
       
         {Public: TextureMgr_Gof () {cout <"TextureMgr_Gof constructor" <
        
         

 

Because assertions are added to ensure that only one call is made, they can be globally unique. Therefore, we can use the Texture Manager as follows (manual deletion is required ):

 

// [Main. cpp] # include "TextureMgr. h" # include
          
           
Using namespace std; # define g_TexMgr TextureMgr: getSingleton () # define g_TexMgr_Gof TextureMgr_Gof: getSingleton () int main (char argc, char * argv []) {// TextureMgrTexture * tex1 = g_TexMgr.getTexture ("Texture 1"); Texture * tex2 = Texture ("Texture 2"); Texture * _ tex1 = g_TexMgr.getTexture ("Texture 1 "); cout <
           
            
GetName () <
            
             
GetName () <
             
              
GetName () <
              
               
DoSomeThing (); tex2-> doSomeThing (); _ tex1-> doSomeThing (); delete & g_TexMgr; cout <
               
                 GetName () <
                
                  GetName () <
                 
                   GetName () <
                  
                    DoSomeThing (); tex2-> doSomeThing (); _ tex3-> doSomeThing (); delete & g_TexMgr_Gof; system ("pause"); return 0 ;}
                  
                 
                
               
              
             
            
           
          

 

The final result is as follows:

 

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.