views:

360

answers:

1

Many of my c++ objects implement rerfrence counting through AddRef and FreeRef methods. If FreeRef reduces the refrence count to 0 then the object deletes its self.

All methods which return a refrence counted object dont increment the refrence. This makes it simple since a smart pointer can then simply increment the count apon recieving a pointer and decrement the count when they no longer refrence it. eg:

template<class T> FlPtr
{
    T *p;
public:
    FlPtr(T *p=0):p(p){if(p)p->AddRef();}
    ~FlPtr(){if(p)p->FreeRef();}

    FlPtr<T>& operator =(T *newP)
    {
        if(newP)newP->AddRef();
        if(p)p->FreeRef();
        p = newP;
        return *this;
    }
    operator T*(){return p;}
};

I was thinking mayby if I could tell SWIG to just use my existing smart pointer internally for these objects that may work however im not sure how to do that either :(

+1  A: 

Take a look at the SWIG documentation. Basically, if you have a class Foo and want to wrap it with a smart pointer Bar, you do this:

%ignore Foo;
class Foo {
    ...
};

class Bar {
public:
    Foo *operator->();
    ...
}

Now when you try to access members of a instance of Bar, it will internally use Foo, but this will be completely transparent.

Zifre