Liquid XML Data Binder (C++, Java, VB6) / Reference / C++ / Reference / C++ - CSmartPtr
In This Topic
    C++ - CSmartPtr
    In This Topic
    CSmartPtr Template
    Description
    Base Classes:
    Implemented interfaces:
      Members Description  
        CSmartPtr Constructor  
        Detach Removes the contained object from the control of the smart pointer  
        operator & Allows the smart pointer to be referenced  
        operator < determines if this pointer is greater than another  
        operator > determines if this pointer is less than another  
        operator * Allows the smart pointer to be dereferenced  
        operator T* Cast operator, automatically casts the smart pointer to the type of object T.  
        operator ! true if the pointer to an object is NULL  
        operator != false if the pointer pT is the same as the pointer held within this smart pointer  
        operator -> Allows access to the underlying object  
        operator = Copy operator  
        operator == Compares the value of the pointers.  
        Ptr A pointer to the object wrapped by this Smart pointer  
        Release Releases the underlying object.  
        _PtrClass typedef T _PtrClass;  

    Class Definition

    template <class T>
    class CSmartPtr
    {
    public:
            typedef T _PtrClass;
            CSmartPtr()
            {
                    p = NULL;
            }
            CSmartPtr(T* lp)
            {
                    if ((p = lp) != NULL)
                            p->AddRef();
            }
            CSmartPtr(const CSmartPtr<T>& lp)
            {
                    if ((p = lp.p) != NULL)
                            p->AddRef();
            }
            ~CSmartPtr()
            {
                    if (p)
                            p->Release();
            }
            void Release()
            {
                    if (p)
                    {
                            p->Release();
                            p = NULL;
                    }
            }
            operator T*()
            {
                    return p;
            }
            operator const T*() const
            {
                    return p;
            }
            T* operator->()
            {
                    ASSERT(p!=NULL);
                    return p;
            }
            const T* operator->() const
            {
                    return p;
            }
            operator T*() const
            {
                    return p;
            }
            T& operator*() const
            {
                    ASSERT(p!=NULL);
                    return *(p);
            }
            //The assert on operator& usually indicates a bug.  If this is really
            //what is needed, however, take the address of the p member explicitly.
            T** operator&()
            {
                    ASSERT(p==NULL);
                    return &(p);
            }
            T* operator=(T* lp)
            {
                    if (lp != NULL)
                            lp->AddRef();
                    if (p)
                            p->Release();
                    p = lp;
                    return p;
            }
            T* operator=(const CSmartPtr<T>& lp)
            {
                    T* pTmp = p;
                    p = lp.p;
                    if (p != NULL)
                            p->AddRef();
                    if (pTmp != NULL)
                            pTmp->Release();
                    return p;
            }
            bool operator!() const
            {
                    return (p == NULL);
            }
            bool operator<(T* pT) const
            {
                    return p < pT;
            }
            bool operator==(T* pT) const
            {
                    return p == pT;
            }
            bool operator!=(T* pT) const
            {
                    return p != pT;
            }
            T* Ptr()
            {
                    return dynamic_cast<T*>(p);
            }
            void Attach(T* p2)
            {
                    if (p)
                            p->Release();
                    p = p2;
            }
            T* Detach()
            {
                    T* pt = p;
                    p = NULL;
                    return pt;
            }
    protected:
            T* p;
    };