Extend Poco::SharedPtr functionnalities

Discussion of ideas for features and new projects based on POCO.
lgm42
Posts: 11
Joined: 13 Aug 2014, 10:45

Extend Poco::SharedPtr functionnalities

Postby lgm42 » 19 May 2015, 10:42

In a class I would to inject dependance of my class to an other. I don't want to use pointer but SharedPtr.
If I use the following code, it wouldn't work because the Dtor of B will also destroy the instance of A and I don't want !!

Code: Select all

class IA
{
    ....
}

class A : public IA
{
public:
    A() {}

    void init()
    {
        _b = new B(this);
    }
private:
    SharedPtr<B> _b;
}

class B
{
public:
    B(SharedPtr<IA> ia)  : _ia(ia)
    {}

    ....

private:
    SharedPtr<IA> _ia;
}


The only mean I've found if to create a NullReleasePolicy and create a SharedPtr which don't destroy it.
My first proposal is to add NullReleasePolicy with ReleasePolicy and ArrayReleasePolicy in SharedPtr.h file

Code: Select all

template <class C>
class NullReleasePolicy
   /// The null release policy for SharedPtr, which don't delete pointer
{
public:
   static void release(C* pObj)
      /// DON'T delete the object.
   {
      //nothing to do
   }
};


With that code I can make the following A::init() code

Code: Select all

    void init()
    {
        _b = new B(SharedPtr<IA, ReferenceCounter, NullReleasePolicy>(this));
    }


My second request is to have a class name like SharedPtrFromThis or something like that which permit to make :

Code: Select all

    void init()
    {
        _b = new B(SharedPtrFromThis(this));
    }


I would like to create it but the Dtor of SharedPtr isn't virtual so I can't inherit it.

What do you think about my proposals ?

Return to “Wishlist”

Who is online

Users browsing this forum: No registered users and 2 guests

cron