c++ RAII 小例子

直接看代码吧:


#ifndef ___UTIL_RAII_H__

#define ___UTIL_RAII_H__


#include <cstddef>
#include <functional>
#include <new>


namespace M{


    class IRAII
    {
    public:
        void Abolish()throw(){
            m_bAbolish = true;
        }
    protected:
        IRAII():m_bAbolish(false){}
        IRAII(IRAII&& other):m_bAbolish(other.m_bAbolish){
            other.m_bAbolish = true;
        }
    protected:
        bool m_bAbolish;

    };


   template <class T>
    class RAII : public IRAII
    {
    public:
        explicit RAII(const T& t)
            :m_t(t){}


        explicit RAII(T&& t)
            :m_t(std::move(t)){}


        RAII(RAII&& other)
            :IRAII(std::move(other)),
            m_t(std::move(other.m_t)){}
        
        ~RAII() throw(){
            if(!m_bAbolish){
                Invoke();
            }
        }
    private:
        void* operator new(size_t size){
            return ::operator new(size);
        }


        void operator delete(void *ptr) throw(){
            ::operator delete(ptr);
        }


        void Invoke() throw(){ m_t();}
        T m_t;
    };


 template <class T>
    RAII<typename std::decay<T>::type> MakeRAII(T&& t){
        return RAII<typename std::decay<T>::type>(std::forward<T>(t));
    }
    
    enum Finaly{};


    template <class T>
    RAII<typename std::decay<T>::type> operator+(Finaly, T&& t){
        return RAII<typename std::decay<T>::type>(std::forward<T>(t));
    }

#ifdef M_CATENATE_VAR_IMPL
#   undef MCSF_CATENATE_VAR_IMPL
#endif


#define M_CATENATE_VAR_IMPL(prefix, suffix)\
    prefix ## suffix


#ifdef M_CATENATE_VAR
#   undef MCSF_CATENATE_VAR
#endif


#define M_CATENATE_VAR(prefix, suffix)\
    MCSF_CATENATE_VAR_IMPL(prefix, suffix)


#ifdef M_ANONYMOUS_VAR
#   undef MCSF_ANONYMOUS_VAR
#endif


#ifdef __COUNTER__
#   define M_ANONYMOUS_VAR(VARIABLE)\
    M_CATENATE_VAR(VARIABLE, __COUNTER__)
#else
#   define M_ANONYMOUS_VAR(VARIABLE)\
    M_CATENATE_VAR(VARIABLE, __LINE__)
#endif


#ifdef M_RAII
#   undef M_RAII
#endif
#define M_RAII\
    auto M_ANONYMOUS_VAR(RAII)\
    = Finaly() + [&]

    // \brief: used to explicitly mark the return value of a function
    // as unused. If you are really sure you don't want to do anything
    // with the return value of a function.
    template<typename T>
    inline void ignore_result(const T&){}
}


#endif // ___UTIL_RAII_H__


使用:

IRAII&& donePipe = MakeRAII([this, &readInfo]()
{
    for each(Processor processor in readInfo.vProcessors)
    {
        if(nullptr != processor.first)
        {             }
    }
            });
donePipe;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值