singleton.hpp

来自「"More for C++" is a class library that p」· HPP 代码 · 共 94 行

HPP
94
字号
////  This file is part of the "More for C++" library////  Copyright (c) 1999-2003 by Thorsten Goertz (thorsten@morefor.org)////  The "More for C++" library is free software; you can redistribute it and/or//  modify it under the terms of the license that comes with this package.////  Read "license.txt" for more details.////  THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED//  WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES//  OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.////////////////////////////////////////////////////////////////////////////////#ifndef MORE_UTIL_SINGLETON_HPP#define MORE_UTIL_SINGLETON_HPP//////////////////////////////////////////////////////////////////////////#include <more/pointer.hpp>////////////////////////////////////////////////////////////////////////////////namespace more{  namespace util  {    class SingletonBase    {      protected:        static p<SingletonBase> access( SingletonBase*& rpInstance,                                        SingletonBase*  pNewInstance,                                        bool            bUpdateInstance );        virtual ~SingletonBase( );    };    template<typename Type>    class Singleton: public SingletonBase    {      public:        static p<Type> getInstance( );      protected:        static void setInstance( const p<Type>& );      private:        static p<Type> access( Type* pNewInstance, bool bUpdateInstance );    };  }}////////////////////////////////////////////////////////////////////////////////template<typename Type>inline more::p<Type> more::util::Singleton<Type>::getInstance( ){  return Singleton<Type>::access( 0, false );}////////////////////////////////////////////////////////////////////////////////template<typename Type>inline void more::util::Singleton<Type>::setInstance(  const p<Type>& pNewInstance){  Singleton<Type>::access( pNewInstance, true );}////////////////////////////////////////////////////////////////////////////////template<typename Type>inline more::p<Type> more::util::Singleton<Type>::access(  Type* pNewInstance,  bool  bUpdateInstance){  static SingletonBase* pInstance = 0;  p<SingletonBase> pResult = SingletonBase::access( pInstance,                                                    pNewInstance,                                                    bUpdateInstance );  return pResult == 0 ? 0 : cast<Type>( pResult );}////////////////////////////////////////////////////////////////////////////////#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?