browsercore/core/singleton.h
changeset 5 0f2326c2a325
equal deleted inserted replaced
1:b0dd75e285d2 5:0f2326c2a325
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
       
     8 * 
       
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 * GNU Lesser General Public License for more details.
       
    13 *
       
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not, 
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:
       
    19 *  This defines both Meyers Singleton and Gamma Singleton template.
       
    20 */
       
    21 
       
    22 
       
    23 #ifndef __SINGLETON_H
       
    24 #define __SINGLETON_H
       
    25 
       
    26 /* 
       
    27  * This is how a Gamma Singleton would instantiate its object.
       
    28  */
       
    29 template <class T> struct CreateGamma {
       
    30     static T* Create() { return new T; }
       
    31 };
       
    32 
       
    33 /* 
       
    34  * This is how a Meyers Singleton would instantiate its object.
       
    35  */
       
    36 template <class T> struct CreateMeyers {
       
    37     static T* Create() {
       
    38         static T _instance;
       
    39         return &_instance;
       
    40     }
       
    41 };
       
    42 
       
    43 /*
       
    44  * This Singleton class accepts different creation policies.
       
    45  */
       
    46 template <class T, template<class> class CreationPolicy=CreateMeyers>
       
    47 class Singleton {
       
    48 public:
       
    49     static T& Instance() {
       
    50         if (!m_pInstance)
       
    51             m_pInstance=CreationPolicy<T>::Create();
       
    52         return *m_pInstance;
       
    53 }
       
    54 
       
    55 private:
       
    56     Singleton();           // hide constrcutor
       
    57     ~Singleton();          // hide desctructor
       
    58     Singleton(Singleton const&);    // hide copy constructor
       
    59     Singleton& operator=(Singleton const&);  // hide assignment operator
       
    60 
       
    61     static T* m_pInstance;
       
    62 };
       
    63 
       
    64 template <class T, template<class> class C>
       
    65 T* Singleton<T,C>::m_pInstance=0;
       
    66 
       
    67 #endif // __SINGLETON_H