Menu

SmartPtr lacks a Delete Policy ?

Developers
2006-12-11
2013-04-08
  • Guillaume CHATELET

    I was thinking about improving the CachedFactory by providing smart pointers which could send back objects automatically to the cache.

    To do that, I would need a special smart pointer policy that could call the Cached Factory release function instead of delete.

    So I started looking closer at Richard's Strong&Weak pointers and remarked that SmartPtr didn't provide a DeletePolicy.

    If I'm right, in SmartPtr the deletion reponsibility is up to the StoragePolicy.
    So I'm sure it's feasable to add such a StoragePolicy (that calls a function instead of delete) but it seems to me the terms are a bit confusing now.
    DeletePolicy in Strong&Weak pointers, and StoragePolicy in SmartPtr.
    Moreover, the other policies are roughly the same ( OwnershipPolicy, ConversionPolicy, CheckingPolicy ).

    What do you think about that ?

    Guillaume

     
    • Richard Sposato

      Richard Sposato - 2006-12-12

      > If I'm right, in SmartPtr the deletion reponsibility is up to the StoragePolicy.

      There is no separate DeletePolicy in SmartPtr because the StoragePolicy has responsbility for deleting.  It also has the responsiblity for storage.  This implies if you need to delete an array, and want the LockedStorage policy, you have to make another StoragePolicy which looks just like LockedStorage but uses delete[] instead of delete.  Likewise, if you want to call a function which deletes the object for you, you have to write your own StoragePolicy for that.

      > So I'm sure it's feasable to add such a StoragePolicy (that calls a function instead
      > of delete) but it seems to me the terms are a bit confusing now.

      In StrongPtr, you can specify the deletion policy separate from anything else.  If you want to call a function which deletes the object for you, you only have to write a DeletePolicy for that.  You don't have to modify existing OwnershipPolicy class or make your own.

      I purposefully did not make a DeletePolicy to call a member function of class P.  The policy would not know the name of a function to call in class P.

      > Moreover, the other policies are roughly the same ( OwnershipPolicy,
      > ConversionPolicy, CheckingPolicy ).

      Yes, the terms are a bit confusing.  The OwnershipPolicies for SmartPtr don't work with StrongPtr and vice-versa.  Each host class requires different functions in the OwnershipPolicy classes.

      The ConversionPolicy, CheckingPolicy, and ConstnessPolicy classes are usable in both SmartPtr and StrongPtr.

      If you check the documentation comments near the start of StrongPtr.h, you'll see a list of requirements for some policy types used by StrongPtr.  Someday, we should add similar documentation to SmartPtr.h.

      Cheers,

      Rich

       

Log in to post a comment.