浏览代码

Merge pull request #11615 from jakesmith/hpcc-20425

HPCC-20425 Add OwnedPtr variety with custom free

Reviewed-by: Gavin Halliday <ghalliday@hpccsystems.com>
Gavin Halliday 6 年之前
父节点
当前提交
dfd650e510
共有 2 个文件被更改,包括 43 次插入55 次删除
  1. 43 24
      system/jlib/jbuff.hpp
  2. 0 31
      system/jlib/jscm.hpp

+ 43 - 24
system/jlib/jbuff.hpp

@@ -57,48 +57,67 @@ private:
 
 //--------------------------------------------------------------------------------------------------------------------
 
-template <class CLASS> class OwnedMalloc
+template <class CLASS, void (*FREE_FUNC)(CLASS *)> class OwnedPtrCustomFree
 {
+    typedef OwnedPtrCustomFree<CLASS, FREE_FUNC> SELF;
+    void safeFree(CLASS *_ptr) { if (_ptr) FREE_FUNC(_ptr); }
+protected:
+    CLASS *ptr = nullptr;
+
 public:
-    inline OwnedMalloc()                        { ptr = NULL; }
-    inline OwnedMalloc(CLASS * _ptr)            { ptr = _ptr; }
-    explicit inline OwnedMalloc(unsigned n, bool clearMemory = false) { doAllocate(n, clearMemory); }
-    inline ~OwnedMalloc()                       { free(ptr); }
+    OwnedPtrCustomFree<CLASS, FREE_FUNC>() { }
+    OwnedPtrCustomFree<CLASS, FREE_FUNC>(CLASS *_ptr) : ptr(_ptr) { }
+    OwnedPtrCustomFree<CLASS, FREE_FUNC>(SELF &&_ptr) { ptr = _ptr.getClear(); }
+    ~OwnedPtrCustomFree<CLASS, FREE_FUNC>() { safeFree(ptr); }
+
+    void operator = (CLASS * _ptr)
+    {
+        safeFree(ptr);
+        ptr = _ptr;
+    }
+    inline CLASS * operator -> () const { return ptr; }
+    inline operator CLASS *() const     { return ptr; }
 
-    inline CLASS * operator -> () const         { return ptr; }
-    inline operator CLASS *() const             { return ptr; }
+    inline void clear()                 { CLASS *temp=ptr; ptr=nullptr; safeFree(temp); }
+    inline CLASS *get() const           { return ptr; }
+    inline CLASS *getClear()            { CLASS * temp = ptr; ptr=nullptr; return temp; }
+    inline void setown(CLASS *_ptr)     { safeFree(ptr); ptr = _ptr; }
+
+    void operator = (const OwnedPtrCustomFree<CLASS, FREE_FUNC> & other) = delete;
+    void setown(const OwnedPtrCustomFree<CLASS, FREE_FUNC> &other) = delete;
+};
 
-    inline void clear()                         { CLASS *temp=ptr; ptr=NULL; free(temp); }
-    inline CLASS * get() const                  { return ptr; }
-    inline CLASS * getClear()                   { CLASS * temp = ptr; ptr = NULL; return temp; }
-    inline void setown(CLASS * _ptr)            { CLASS * temp = ptr; ptr = _ptr; free(temp); }
+//A simple object container/smart pointer
+template <typename CLASS> inline void ownedPtrDoDelete(CLASS *o) { delete o; }
+template <class CLASS>
+using OwnedPtr = OwnedPtrCustomFree<CLASS, ownedPtrDoDelete<CLASS>>;
 
+template <typename CLASS> void inline ownedMallocDoFree(CLASS *o) { free(o); }
+template <class CLASS> class OwnedMalloc : public OwnedPtrCustomFree<CLASS, ownedMallocDoFree<CLASS>>
+{
+    typedef OwnedPtrCustomFree<CLASS, ownedMallocDoFree<CLASS>> PARENT;
+public:
+    inline OwnedMalloc() : PARENT() { }
+    inline OwnedMalloc(CLASS * _ptr) : PARENT(_ptr) { }
+    explicit inline OwnedMalloc(unsigned n, bool clearMemory = false) { doAllocate(n, clearMemory); }
     inline void allocate(bool clearMemory = false)   { allocateN(1, clearMemory); }
     inline void allocateN(unsigned n, bool clearMemory = false)
     {
-        clear();
+        PARENT::clear();
         doAllocate(n, clearMemory);
     }
 
+    void allocate(unsigned n, bool clearMemory = false) = delete;
+    void operator = (CLASS * _ptr) = delete;
 private:
-    inline OwnedMalloc(const OwnedMalloc<CLASS> & other);
-
     inline void doAllocate(unsigned n, bool clearMemory = false)
     {
         void * mem = clearMemory ? calloc(n, sizeof(CLASS)) : malloc(n * sizeof(CLASS));
-        ptr = static_cast<CLASS *>(mem);
+        PARENT::ptr = static_cast<CLASS *>(mem);
     }
-    void allocate(unsigned n, bool clearMemory = false);
-    void operator = (CLASS * _ptr);
-    void operator = (const OwnedMalloc<CLASS> & other);
-    void set(CLASS * _ptr);
-    void set(const OwnedMalloc<CLASS> &other);
-    void setown(const OwnedMalloc<CLASS> &other);
-
-private:
-    CLASS * ptr;
 };
 
+
 #define MEMBUFFER_MAXLEN UINT_MAX // size32_t
 
 class jlib_decl MemoryBuffer

+ 0 - 31
system/jlib/jscm.hpp

@@ -47,37 +47,6 @@ template <class X> inline void Release(X * ptr) { if (ptr) ptr->Release(); }
 
 #define QUERYINTERFACE(ptr, TYPE)   (dynamic_cast<TYPE *>(ptr))
 
-//A simple object container/smart pointer
-template <class CLASS> class OwnedPtr
-{
-public:
-    inline OwnedPtr()                        { ptr = NULL; }
-    inline OwnedPtr(CLASS * _ptr)            { ptr = _ptr; }
-    inline ~OwnedPtr()                       { delete ptr; }
-
-    void operator = (CLASS * _ptr)
-    {
-        if (ptr)
-            delete ptr;
-        ptr = _ptr;
-    }
-    inline CLASS * operator -> () const         { return ptr; }
-    inline operator CLASS *() const             { return ptr; }
-
-    inline void clear()                         { CLASS *temp=ptr; ptr=NULL; delete temp; }
-    inline CLASS * get() const                  { return ptr; }
-    inline CLASS * getClear()                   { CLASS * temp = ptr; ptr = NULL; return temp; }
-    inline void setown(CLASS * _ptr)            { CLASS * temp = ptr; ptr = _ptr; delete temp; }
-
-private:
-    inline OwnedPtr(const OwnedPtr<CLASS> & other);
-    void operator = (const OwnedPtr<CLASS> & other);
-    void setown(const OwnedPtr<CLASS> &other);
-
-private:
-    CLASS * ptr;
-};
-
 //This base class implements a shared pointer based on a link count held in the object.
 //The two derived classes Owned and Linked should be used as the concrete types to construct a shared object
 //from a pointer.