分享一个不错的C++版单例模式封装类

来源:互联网 发布:医学数据库检索方法 编辑:程序博客网 时间:2024/05/22 00:48

摘自:http://www.cnblogs.com/ldcsaa

/*
 * Copyright Bruce Liang (ldcsaa@gmail.com)
 *
 * Version : 2.1.1
 * Author : Bruce Liang
 * Porject : https://code.google.com/p/ldcsaa
 * Bolg  : http://www.cnblogs.com/ldcsaa
 * WeiBo : http://weibo.com/u/1402935851
 * QQ Group : http://qun.qq.com/#jointhegroup/gid/75375912
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
/******************************************************************************
Module:  Singleton.h
Notices: Copyright (c) 2006 Bruce Liang
Purpose: 用于简化 Singleton 模式的定义.
Desc:
******************************************************************************/

#pragma once

#define SINGLETON_THIS(ClassName)  ClassName::GetThis()
#define SINGLETON_INSTANCE(ClassName) ClassName::GetInstance()
#define SINGLETON_OBJECT(ObjName)  SINGLETON_INSTANCE(C##ObjName)

#define DEFINE_SINGLETON(ClassName)           \
 ClassName* ClassName::m_pThis = NULL;

#define DEFINE_P_THIS(ClassName)           \
  DEFINE_SINGLETON(ClassName)

#define DECLARE_SINGLETON_INTERFACE(ClassName)        \
public:                  \
 static ClassName* GetThis()  {return m_pThis;}      \
 static ClassName& GetInstance() { return *m_pThis;}      \
protected:                 \
 static ClassName* m_pThis;

#define DECLARE_SINGLETON_CREATE_INSTANCE(ClassName)      \
public:                  \
 static BOOL CreateInstance()           \
 {                  \
  if(!m_pThis)              \
   m_pThis = new ClassName;          \
                   \
  return m_pThis != NULL;           \
 }                  \
                   \
 static BOOL DeleteInstance()           \
 {                  \
  if(m_pThis)               \
  {                 \
   delete m_pThis;             \
   m_pThis = NULL;            \
  }                 \
                   \
  return m_pThis == NULL;           \
 }

#define DECLARE_PRIVATE_DEFAULT_CONSTRUCTOR(ClassName)      \
private:                 \
 ClassName(){}

#define DECLARE_PRIVATE_COPY_CONSTRUCTOR(ClassName)       \
private:                 \
 ClassName(const ClassName&);           \
 ClassName& operator = (const ClassName&);

#define DECLARE_NO_COPY_CLASS(className)         \
  DECLARE_PRIVATE_COPY_CONSTRUCTOR(className)


#define DECLARE_SINGLETON_IMPLEMENT_NO_CREATE_INSTANCE(ClassName)   \
 DECLARE_SINGLETON_INTERFACE(ClassName)         \
 DECLARE_PRIVATE_DEFAULT_CONSTRUCTOR(ClassName)       \
 DECLARE_PRIVATE_COPY_CONSTRUCTOR(ClassName)        

#define DECLARE_SINGLETON_IMPLEMENT_NO_DEFAULT_CONSTRUCTOR(ClassName)  \
 DECLARE_SINGLETON_CREATE_INSTANCE(ClassName)       \
 DECLARE_PRIVATE_COPY_CONSTRUCTOR(ClassName)

#define DECLARE_SINGLETON_IMPLEMENT(ClassName)        \
 DECLARE_SINGLETON_IMPLEMENT_NO_DEFAULT_CONSTRUCTOR(ClassName)   \
 DECLARE_PRIVATE_DEFAULT_CONSTRUCTOR(ClassName)

#define DECLARE_SINGLETON_NO_DEFAULT_CONSTRUCTOR(ClassName)     \
 DECLARE_SINGLETON_INTERFACE(ClassName)         \
 DECLARE_SINGLETON_IMPLEMENT_NO_DEFAULT_CONSTRUCTOR(ClassName)

#define DECLARE_SINGLETON(ClassName)          \
 DECLARE_SINGLETON_NO_DEFAULT_CONSTRUCTOR(ClassName)      \
 DECLARE_PRIVATE_DEFAULT_CONSTRUCTOR(ClassName)


template<class T>
class CSingleObject
{
public:
 CSingleObject () {T::CreateInstance();}
 ~CSingleObject () {T::DeleteInstance();}
 T* GetPointer () {return T::GetThis();}
 T& GetObject () {return T::GetInstance();}
 BOOL IsValid () {return GetPointer() != NULL;}
};

#define DECLARE_SINGLE_OBJECT(ClassName) CSingleObject<ClassName> obj##ClassName##Instance;

 

 

用法:

class A{

public:
  void test()
  {
    printf("I'm A!\n");
  }

  DECLARE_SINGLETON(A)

};

DEFINE_SINGLETON(A)

int main( void )
{

  DECLARE_SINGLE_OBJECT(A)
  objAInstance.GetObject().test();

  return 0;

}

 

参考资料:

拷贝构造函数:http://baike.baidu.com/view/1266959.htm

原创粉丝点击