Projects > cpp > Issues > Refactor #2518

We are migrating issue tracker of Cocos2d-x Project to Github, please create new issue there. Thanks.

Create Issue on Github

Hybrid approach: Include cocos2d SmartPtr in the code

Refactor #2518 [Rejected]
ricardo 2013-08-10 00:24 . Updated over 11 years ago

Include this code in cocos2d-x:

Code based on this code:
http://www.codeproject.com/Articles/64111/Building-a-Quick-and-Handy-Reference-Counting-Clas

The license should be this one:
http://www.codeproject.com/info/cpol10.aspx

and the author is: Ahmed S. Hefny

template < class T >
class RefCountPtr
{
public:
    //Construct using a C pointer
    //e.g. RefCountPtr< T > x = new T();
    RefCountPtr(T* ptr = NULL)
    : _ptr(ptr)
    {
        if(ptr != NULL) {ptr->retain();}
    }

    //Copy constructor
    RefCountPtr(const RefCountPtr &ptr)
    : _ptr(ptr._ptr)
    {
        if(_ptr != NULL) {_ptr->retain();}
    }

    ~RefCountPtr()
    {
        if(_ptr != NULL)
            _ptr->release();
    }

    //Assign a pointer
    //e.g. x = new T();
    RefCountPtr &operator=(T* ptr)
    {
        //The following grab and release operations have to be performed
        //in that order to handle the case where ptr == _ptr
        //(See comment below by David Garlisch)
        if(ptr != NULL) {ptr->retain();}
        if(_ptr != NULL) {_ptr->release();}
        _ptr = ptr;
        return (*this);
    }

    //Assign another RefCountPtr
    RefCountPtr &operator=(const RefCountPtr &ptr)
    {
        return (*this) = ptr._ptr;
    }

    //Retrieve actual pointer
    T* get() const
    {
        return _ptr;
    }

    //Some overloaded operators to facilitate dealing with an RefCountPtr
    //as a conventional C pointer.
    //Without these operators, one can still use the less transparent
    //get() method to access the pointer.
    T* operator->() const {return _ptr;}        //x->member
    T &operator*() const {return *_ptr;}        //*x, (*x).member
    explicit operator T*() const {return _ptr;}     //T* y = x;
    explicit operator bool() const {return _ptr != NULL;}   //if(x) {/*x is not NULL*/}
    bool operator==(const RefCountPtr &ptr) {return _ptr == ptr._ptr;}
    bool operator==(const T *ptr) {return _ptr == ptr;}

private:
    T *_ptr;    //Actual pointer
};

class HelloWorld : public Node {
public:
    typedef RefCountPtr ptr;

    virtual ~HelloWorld() {
        log("HelloWorld: Destructor");
    }

    static ptr create() {
        auto var = ptr( new HelloWorld);
        var->release();
        return var;
    }
};
ricardo 2013-08-10 00:26
  • Tracker changed from Bug to Refactor
  • Priority changed from Normal to High
ricardo 2013-08-13 04:00
  • Status changed from New to Rejected

Hybrid approach won’t be used.

Atom PDF

Status:Rejected
Start date:2013-08-10
Priority:High
Due date:
Assignee:-
% Done:

0%

Category:-
Target version:3.0-alpha0