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

摘自: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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值