smartptr.h

来自「C++封装的视频采集代码」· C头文件 代码 · 共 1,203 行 · 第 1/3 页

H
1,203
字号
    	SmartPtr& operator=(const SmartPtr<T1, OP1, CP1, KP1, SP1>& rhs)    	{    	    SmartPtr temp(rhs);    	    temp.Swap(*this);    	    return *this;    	}    	        template        <            typename T1,            template <class> class OP1,            class CP1,            template <class> class KP1,            template <class> class SP1        >    	SmartPtr& operator=(SmartPtr<T1, OP1, CP1, KP1, SP1>& rhs)    	{    	    SmartPtr temp(rhs);    	    temp.Swap(*this);    	    return *this;    	}    	    	void Swap(SmartPtr& rhs)    	{    	    OP::Swap(rhs);    	    CP::Swap(rhs);    	    KP::Swap(rhs);    	    SP::Swap(rhs);    	}    	    	~SmartPtr()    	{    	    if (OP::Release(GetImpl(*static_cast<SP*>(this))))    	    {    	        SP::Destroy();    	    }    	}    	    	friend inline void Release(SmartPtr& sp, typename SP::StoredType& p)    	{    	    p = GetImplRef(sp);    	    GetImplRef(sp) = SP::Default();    	}    	    	friend inline void Reset(SmartPtr& sp, typename SP::StoredType p)    	{ SmartPtr(p).Swap(sp); }        T* operator->() //### BCB PointerType causes internal compiler crash        {            KP::OnDereference(GetImplRef(*this));            return SP::operator->();        }        T* operator->() const //### BCB PointerType causes internal compiler crash        {            KP::OnDereference(GetImplRef(*this));            return SP::operator->();        }        T& operator*() //### BCB ReferenceType causes internal compiler crash        {            KP::OnDereference(GetImplRef(*this));            return SP::operator*();        }        T& operator*() const //### BCB ReferenceType causes internal compiler crash        {            KP::OnDereference(GetImplRef(*this));            return SP::operator*();        }    	        bool operator!() const // Enables "if (!sp) ..."        { return GetImpl(*this) == 0; }        inline friend bool operator==(const SmartPtr& lhs,            const T* rhs)        { return GetImpl(lhs) == rhs; }                inline friend bool operator==(const T* lhs,            const SmartPtr& rhs)        { return rhs == lhs; }                inline friend bool operator!=(const SmartPtr& lhs,            const T* rhs)        { return !(lhs == rhs); }                inline friend bool operator!=(const T* lhs,            const SmartPtr& rhs)        { return rhs != lhs; }        // Ambiguity buster        template        <            typename T1,            template <class> class OP1,            class CP1,            template <class> class KP1,            template <class> class SP1        >        bool operator==(const SmartPtr<T1, OP1, CP1, KP1, SP1>& rhs) const        { return *this == GetImpl(rhs); }        // Ambiguity buster        template        <            typename T1,            template <class> class OP1,            class CP1,            template <class> class KP1,            template <class> class SP1        >        bool operator!=(const SmartPtr<T1, OP1, CP1, KP1, SP1>& rhs) const        { return !(*this == rhs); }        // Ambiguity buster        template        <            typename T1,            template <class> class OP1,            class CP1,            template <class> class KP1,            template <class> class SP1        >        bool operator<(const SmartPtr<T1, OP1, CP1, KP1, SP1>& rhs) const        { return *this < GetImpl(rhs); }    private:        // Helper for enabling 'if (sp)'        struct Tester        {            Tester() {}        private:            void operator delete(void*);        };            public:        // enable 'if (sp)'        operator Tester*() const        {            if (!*this) return 0;            static Tester t;            return &t;        }    private:        // Helper for disallowing automatic conversion        struct Insipid        {            Insipid(PointerType) {}        };        enum { aux2 = CP::allow }; // ### BCB, PointerType must be also removed        typedef typename Select<aux2, T*, Insipid>::Result            AutomaticConversionResult;    public:        operator AutomaticConversionResult() const        { return GetImpl(*this); }    };////////////////////////////////////////////////////////////////////////////////// free comparison operators for class template SmartPtr////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// operator== for lhs = SmartPtr, rhs = raw pointer////////////////////////////////////////////////////////////////////////////////    template    <        typename T,        template <class> class OP,        class CP,        template <class> class KP,        template <class> class SP,        typename U    >    inline bool operator==(const SmartPtr<T, OP, CP, KP, SP>& lhs,        const U* rhs)    { return GetImpl(lhs) == rhs; }    ////////////////////////////////////////////////////////////////////////////////// operator== for lhs = raw pointer, rhs = SmartPtr////////////////////////////////////////////////////////////////////////////////    template    <        typename T,        template <class> class OP,        class CP,        template <class> class KP,        template <class> class SP,        typename U    >    inline bool operator==(const U* lhs,        const SmartPtr<T, OP, CP, KP, SP>& rhs)    { return rhs == lhs; }////////////////////////////////////////////////////////////////////////////////// operator!= for lhs = SmartPtr, rhs = raw pointer////////////////////////////////////////////////////////////////////////////////    template    <        typename T,        template <class> class OP,        class CP,        template <class> class KP,        template <class> class SP,        typename U    >    inline bool operator!=(const SmartPtr<T, OP, CP, KP, SP>& lhs,        const U* rhs)    { return !(lhs == rhs); }    ////////////////////////////////////////////////////////////////////////////////// operator!= for lhs = raw pointer, rhs = SmartPtr////////////////////////////////////////////////////////////////////////////////    template    <        typename T,        template <class> class OP,        class CP,        template <class> class KP,        template <class> class SP,        typename U    >    inline bool operator!=(const U* lhs,        const SmartPtr<T, OP, CP, KP, SP>& rhs)    { return rhs != lhs; }////////////////////////////////////////////////////////////////////////////////// operator< for lhs = SmartPtr, rhs = raw pointer -- NOT DEFINED////////////////////////////////////////////////////////////////////////////////    template    <        typename T,        template <class> class OP,        class CP,        template <class> class KP,        template <class> class SP,        typename U    >    inline bool operator<(const SmartPtr<T, OP, CP, KP, SP>& lhs,        const U* rhs);        ////////////////////////////////////////////////////////////////////////////////// operator< for lhs = raw pointer, rhs = SmartPtr -- NOT DEFINED////////////////////////////////////////////////////////////////////////////////    template    <        typename T,        template <class> class OP,        class CP,        template <class> class KP,        template <class> class SP,        typename U    >    inline bool operator<(const U* lhs,        const SmartPtr<T, OP, CP, KP, SP>& rhs);        ////////////////////////////////////////////////////////////////////////////////// operator> for lhs = SmartPtr, rhs = raw pointer -- NOT DEFINED////////////////////////////////////////////////////////////////////////////////    template    <        typename T,        template <class> class OP,        class CP,        template <class> class KP,        template <class> class SP,        typename U    >    inline bool operator>(const SmartPtr<T, OP, CP, KP, SP>& lhs,        const U* rhs)    { return rhs < lhs; }        ////////////////////////////////////////////////////////////////////////////////// operator> for lhs = raw pointer, rhs = SmartPtr////////////////////////////////////////////////////////////////////////////////    template    <        typename T,        template <class> class OP,        class CP,        template <class> class KP,        template <class> class SP,        typename U    >    inline bool operator>(const U* lhs,        const SmartPtr<T, OP, CP, KP, SP>& rhs)    { return rhs < lhs; }  ////////////////////////////////////////////////////////////////////////////////// operator<= for lhs = SmartPtr, rhs = raw pointer////////////////////////////////////////////////////////////////////////////////    template    <        typename T,        template <class> class OP,        class CP,        template <class> class KP,        template <class> class SP,        typename U    >    inline bool operator<=(const SmartPtr<T, OP, CP, KP, SP>& lhs,        const U* rhs)    { return !(rhs < lhs); }        ////////////////////////////////////////////////////////////////////////////////// operator<= for lhs = raw pointer, rhs = SmartPtr////////////////////////////////////////////////////////////////////////////////    template    <        typename T,        template <class> class OP,        class CP,        template <class> class KP,        template <class> class SP,        typename U    >    inline bool operator<=(const U* lhs,        const SmartPtr<T, OP, CP, KP, SP>& rhs)    { return !(rhs < lhs); }////////////////////////////////////////////////////////////////////////////////// operator>= for lhs = SmartPtr, rhs = raw pointer////////////////////////////////////////////////////////////////////////////////    template    <        typename T,        template <class> class OP,        class CP,        template <class> class KP,        template <class> class SP,        typename U    >    inline bool operator>=(const SmartPtr<T, OP, CP, KP, SP>& lhs,        const U* rhs)    { return !(lhs < rhs); }        ////////////////////////////////////////////////////////////////////////////////// operator>= for lhs = raw pointer, rhs = SmartPtr////////////////////////////////////////////////////////////////////////////////    template    <        typename T,        template <class> class OP,        class CP,        template <class> class KP,        template <class> class SP,        typename U    >    inline bool operator>=(const U* lhs,        const SmartPtr<T, OP, CP, KP, SP>& rhs)    { return !(lhs < rhs); }} // namespace Loki////////////////////////////////////////////////////////////////////////////////// specialization of std::less for SmartPtr////////////////////////////////////////////////////////////////////////////////namespace std{    template    <        typename T,        template <class> class OP,        class CP,        template <class> class KP,        template <class> class SP    >    struct less< Loki::SmartPtr<T, OP, CP, KP, SP> >        : public binary_function<Loki::SmartPtr<T, OP, CP, KP, SP>,            Loki::SmartPtr<T, OP, CP, KP, SP>, bool>    {        bool operator()(const Loki::SmartPtr<T, OP, CP, KP, SP>& lhs,            const Loki::SmartPtr<T, OP, CP, KP, SP>& rhs) const        { return less<T*>()(GetImpl(lhs), GetImpl(rhs)); }    };}////////////////////////////////////////////////////////////////////////////////// Change log:// June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!!// December 09, 2001: Included <cassert>// July     16, 2002: Ported by Terje Sletteb� and Pavel Vozenilek to BCC 5.6////////////////////////////////////////////////////////////////////////////////#endif // SMARTPTR_INC_

⌨️ 快捷键说明

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