⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 safecoll.h

📁 pwlib源码库
💻 H
📖 第 1 页 / 共 3 页
字号:
       and there is no read only or read/write lock done. In addition any       associated collection is removed so this becomes a non enumerating       safe pointer.     */    PSafePtr & operator=(T * obj)      {        Assign(obj);        return *this;      }    /**Set the new pointer to a collection index.       This will set the pointer to the new object to the index entry in the       colelction that the pointer was created with. The old object pointed to       will be unlocked and dereferenced and the new object referenced and set       to the same locking mode as the previous pointer value.       If the idx'th object is not in the collection, then the safe pointer       is set to NULL.     */    PSafePtr & operator=(PINDEX idx)      {        Assign(idx);        return *this;      }  //@}  /**@name Operations */  //@{    /**Return the physical pointer to the object.      */    operator T*()    const { return  (T *)currentObject; }    /**Return the physical pointer to the object.      */    T & operator*()  const { return *(T *)PAssertNULL(currentObject); }    /**Allow access to the physical object the pointer is pointing to.      */    T * operator->() const { return  (T *)PAssertNULL(currentObject); }    /**Post-increment the pointer.       This requires that the pointer has been created with a PSafeCollection       object so that it can enumerate the collection.      */    T * operator++(int)      {        T * previous = (T *)currentObject;        Next();        return previous;      }    /**Pre-increment the pointer.       This requires that the pointer has been created with a PSafeCollection       object so that it can enumerate the collection.      */    T * operator++()      {        Next();        return (T *)currentObject;      }    /**Post-decrement the pointer.       This requires that the pointer has been created with a PSafeCollection       object so that it can enumerate the collection.      */    T * operator--(int)      {        T * previous = (T *)currentObject;        Previous();        return previous;      }    /**Pre-decrement the pointer.       This requires that the pointer has been created with a PSafeCollection       object so that it can enumerate the collection.      */    T * operator--()      {        Previous();        return (T *)currentObject;      }  //@}  /**Cast the pointer to a different type. The pointer being cast to MUST     be a derived class or NULL is returned.    */      /*  template <class Base>  static PSafePtr<T> DownCast(const PSafePtr<Base> & oldPtr)  {    PSafePtr<T> newPtr;    Base * realPtr = oldPtr;    if (realPtr != NULL && PIsDescendant(realPtr, T))      newPtr.Assign(oldPtr);    return newPtr;  }  */};/**Cast the pointer to a different type. The pointer being cast to MUST    be a derived class or NULL is returned.  */template <class Base, class Derived>PSafePtr<Derived> PSafePtrCast(const PSafePtr<Base> & oldPtr){//  return PSafePtr<Derived>::DownCast<Base>(oldPtr);    PSafePtr<Derived> newPtr;    Base * realPtr = oldPtr;    if (realPtr != NULL && PIsDescendant(realPtr, Derived))      newPtr.Assign(oldPtr);    return newPtr;}/** This class defines a thread-safe collection of objects.  This is part of a set of classes to solve the general problem of a  collection (eg a PList or PDictionary) of objects that needs to be a made  thread safe. Any thread can add, read, write or remove an object with both  the object and the database of objects itself kept thread safe.  See the PSafeObject class for more details. Especially in regard to  enumeration of collections. */template <class Coll, class Base> class PSafeColl : public PSafeCollection{    PCLASSINFO(PSafeColl, PSafeCollection);  public:  /**@name Construction */  //@{    /**Create a safe list collection wrapper around the real collection.      */    PSafeColl()      : PSafeCollection(new Coll)      { }  //@}  /**@name Operations */  //@{    /**Add an object to the collection.       This uses the PCollection::Append() function to add the object to the       collection, with full mutual exclusion locking on the collection.      */    virtual PSafePtr<Base> Append(      Base * obj,       /// Object to add to safe collection.      PSafetyMode mode = PSafeReference    ) {        PWaitAndSignal mutex(collectionMutex);        if (!obj->SafeReference())          return NULL;        return PSafePtr<Base>(*this, mode, collection->Append(obj));      }    /**Remove an object to the collection.       This function removes the object from the collection itself, but does       not actually delete the object. It simply moves the object to a list       of objects to be garbage collected at a later time.       As for Append() full mutual exclusion locking on the collection itself       is maintained.      */    virtual BOOL Remove(      Base * obj          /// Object to remove from safe collection    ) {        return SafeRemove(obj);      }    /**Remove an object to the collection.       This function removes the object from the collection itself, but does       not actually delete the object. It simply moves the object to a list       of objects to be garbage collected at a later time.       As for Append() full mutual exclusion locking on the collection itself       is maintained.      */    virtual BOOL RemoveAt(      PINDEX idx     /// Index to remove    ) {        return SafeRemoveAt(idx);      }    /**Get the instance in the collection of the index.       The returned safe pointer will increment the reference count on the       PSafeObject and lock to the object in the mode specified. The lock       will remain until the PSafePtr goes out of scope.      */    virtual PSafePtr<Base> GetAt(      PINDEX idx,      PSafetyMode mode = PSafeReadWrite    ) {        return PSafePtr<Base>(*this, mode, idx);      }    /**Find the instance in the collection of an object with the same value.       The returned safe pointer will increment the reference count on the       PSafeObject and lock to the object in the mode specified. The lock       will remain until the PSafePtr goes out of scope.      */    virtual PSafePtr<Base> FindWithLock(      const Base & value,      PSafetyMode mode = PSafeReadWrite    ) {        collectionMutex.Wait();        PSafePtr<Base> ptr(*this, PSafeReference, collection->GetValuesIndex(value));        collectionMutex.Signal();        ptr.SetSafetyMode(mode);        return ptr;      }  //@}};/** This class defines a thread-safe array of objects.  See the PSafeObject class for more details. Especially in regard to  enumeration of collections. */template <class Base> class PSafeArray : public PSafeColl<PArray<Base>, Base>{};/** This class defines a thread-safe list of objects.  See the PSafeObject class for more details. Especially in regard to  enumeration of collections. */template <class Base> class PSafeList : public PSafeColl<PList<Base>, Base>{};/** This class defines a thread-safe sorted array of objects.  See the PSafeObject class for more details. Especially in regard to  enumeration of collections. */template <class Base> class PSafeSortedList : public PSafeColl<PSortedList<Base>, Base>{};/** This class defines a thread-safe dictionary of objects.  This is part of a set of classes to solve the general problem of a  collection (eg a PList or PDictionary) of objects that needs to be a made  thread safe. Any thread can add, read, write or remove an object with both  the object and the database of objects itself kept thread safe.  See the PSafeObject class for more details. Especially in regard to  enumeration of collections. */template <class Coll, class Key, class Base> class PSafeDictionaryBase : public PSafeCollection{    PCLASSINFO(PSafeDictionaryBase, PSafeCollection);  public:  /**@name Construction */  //@{    /**Create a safe dictionary wrapper around the real collection.      */    PSafeDictionaryBase()      : PSafeCollection(new Coll) { }  //@}  /**@name Operations */  //@{    /**Add an object to the collection.       This uses the PCollection::Append() function to add the object to the       collection, with full mutual exclusion locking on the collection.      */    virtual void SetAt(const Key & key, Base * obj)      {        collectionMutex.Wait();        SafeRemove(((Coll *)collection)->GetAt(key));        if (obj->SafeReference())          ((Coll *)collection)->SetAt(key, obj);        collectionMutex.Signal();      }    /**Remove an object to the collection.       This function removes the object from the collection itself, but does       not actually delete the object. It simply moves the object to a list       of objects to be garbage collected at a later time.       As for Append() full mutual exclusion locking on the collection itself       is maintained.      */    virtual BOOL RemoveAt(      const Key & key   /// Key to fund object to delete    ) {        PWaitAndSignal mutex(collectionMutex);        return SafeRemove(((Coll *)collection)->GetAt(key));      }    /**Determine of the dictionary contains an entry for the key.      */    virtual BOOL Contains(      const Key & key    ) {        PWaitAndSignal lock(collectionMutex);        return ((Coll *)collection)->Contains(key);      }    /**Get the instance in the collection of the index.       The returned safe pointer will increment the reference count on the       PSafeObject and lock to the object in the mode specified. The lock       will remain until the PSafePtr goes out of scope.      */    virtual PSafePtr<Base> GetAt(      PINDEX idx,      PSafetyMode mode = PSafeReadWrite    ) {        return PSafePtr<Base>(*this, mode, idx);      }    /**Find the instance in the collection of an object with the same value.       The returned safe pointer will increment the reference count on the       PSafeObject and lock to the object in the mode specified. The lock       will remain until the PSafePtr goes out of scope.      */    virtual PSafePtr<Base> FindWithLock(      const Key & key,      PSafetyMode mode = PSafeReadWrite    ) {        collectionMutex.Wait();        PSafePtr<Base> ptr(*this, PSafeReference, ((Coll *)collection)->GetAt(key));        collectionMutex.Signal();        ptr.SetSafetyMode(mode);        return ptr;      }  //@}};/** This class defines a thread-safe array of objects.  See the PSafeObject class for more details. Especially in regard to  enumeration of collections. */template <class Key, class Base> class PSafeDictionary : public PSafeDictionaryBase<PDictionary<Key, Base>, Key, Base>{};#endif // _SAFE_COLLECTION_H/////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -