📄 ncbiobj.hpp
字号:
/// a non-null value. bool NotEmpty(void) const THROWS_NONE { return m_Ptr != 0; } /// Check if pointer is null -- same affect as Empty(). /// /// @sa /// Empty() bool IsNull(void) const THROWS_NONE { return m_Ptr == 0; } /// Operator to test object. /// /// @return /// TRUE if there is a pointer to object; FALSE, otherwise. operator bool(void) THROWS_NONE { return NotEmpty(); } /// Operator to test object -- const version. /// /// @return /// TRUE if there is a pointer to object; FALSE, otherwise. operator bool(void) const THROWS_NONE { return NotEmpty(); } /// Operator to test object. /// /// @return /// TRUE if there is a null pointer to object; FALSE, otherwise. bool operator!(void) const THROWS_NONE { return Empty(); } /// Reset reference object. /// /// This sets the pointer to object to null, and removes reference /// count to object and deletes the object if this is the last reference /// to the object. /// @sa /// Reset(TObjectType*) inline void Reset(void) { TObjectType* ptr = m_Ptr; if ( ptr ) { m_Ptr = 0; CRefBase<C>::RemoveReference(ptr); } } /// Reset reference object to new pointer. /// /// This sets the pointer to object to the new pointer, and removes /// reference count to old object and deletes the old object if this is /// the last reference to the old object. /// @sa /// Reset() inline void Reset(TObjectType* newPtr) { TObjectType* oldPtr = m_Ptr; if ( newPtr != oldPtr ) { if ( newPtr ) CRefBase<C>::AddReference(newPtr); m_Ptr = newPtr; if ( oldPtr ) CRefBase<C>::RemoveReference(oldPtr); } } /// Release a reference to the object and return a pointer to the object. /// /// Releasing a reference means decreasing the reference count by "1". A /// pointer to the existing object is returned, unless this pointer is /// already null(0), in which case a null(0) is returned. /// /// Similar to Release(), except that this method returns a null, /// whereas Release() throws a null pointer exception. /// /// @sa /// Release() inline TObjectType* ReleaseOrNull(void) { TObjectType* ptr = m_Ptr; if ( !ptr ) return 0; m_Ptr = 0; CRefBase<C>::ReleaseReference(ptr); return ptr; } /// Release a reference to the object and return a pointer to the object. /// /// Releasing a reference means decreasing the reference count by "1". A /// pointer to the existing object is returned, unless this pointer is /// already null(0), in which the null pointer exception (eNullPtr) is /// thrown. /// /// Similar to ReleaseOrNull(), except that this method throws an exception /// whereas ReleaseOrNull() does not. /// /// @sa /// ReleaseOrNull() inline TObjectType* Release(void) { TObjectType* ptr = m_Ptr; if ( !ptr ) { CObject::ThrowNullPointerException(); } m_Ptr = 0; CRefBase<C>::ReleaseReference(ptr); return ptr; } /// Reset reference object to new pointer. /// /// This sets the pointer to object to the new pointer, and removes /// reference count to old object and deletes the old object if this is /// the last reference to the old object. /// The new pointer is got from ref argument. /// Operation is atomic on this object, so that AtomicResetFrom() and /// AtomicReleaseTo() called from different threads will work properly. /// Operation is not atomic on ref argument. /// @sa /// AtomicReleaseTo(CRef& ref); inline void AtomicResetFrom(const CRef& ref) { TObjectType* ptr = ref.m_Ptr; if ( ptr ) CRefBase<C>::AddReference(ptr); // for this TObjectType* old_ptr = AtomicSwap(ptr); if ( old_ptr ) CRefBase<C>::RemoveReference(old_ptr); } /// Release referenced object to another CRef<> object. /// /// This copies the pointer to object to the argument ref, /// and release reference from this object. /// Old reference object held by argument ref is released and deleted if /// necessary. /// Operation is atomic on this object, so that AtomicResetFrom() and /// AtomicReleaseTo() called from different threads will work properly. /// Operation is not atomic on ref argument. /// @sa /// AtomicResetFrom(const CRef& ref); inline void AtomicReleaseTo(CRef& ref) { TObjectType* old_ptr = AtomicSwap(0); if ( old_ptr ) { ref.Reset(old_ptr); CRefBase<C>::RemoveReference(old_ptr); } else { ref.Reset(); } } /// Assignment operator for references. CRef<C>& operator=(const CRef<C>& ref) { Reset(ref.m_Ptr); return *this; } /// Assignment operator for references with right hand side set to /// a pointer. CRef<C>& operator=(TObjectType* ptr) { Reset(ptr); return *this; } /// Assignment operator with right hand side set to ENull. CRef<C>& operator=(ENull /*null*/) { Reset(0); return *this; } /// Get pointer value and throw a null pointer exception if pointer /// is null. /// /// Similar to GetPointerOrNull() except that this method throws a null /// pointer exception if pointer is null, whereas GetPointerOrNull() /// returns a null value. /// /// @sa /// GetPointerOrNull(), GetPointer(), GetObject() inline TObjectType* GetNonNullPointer(void) { TObjectType* ptr = m_Ptr; if ( !ptr ) { CObject::ThrowNullPointerException(); } return ptr; } /// Get pointer value. /// /// Similar to GetNonNullPointer() except that this method returns a null /// if the pointer is null, whereas GetNonNullPointer() throws a null /// pointer exception. /// /// @sa /// GetNonNullPointer() inline TObjectType* GetPointerOrNull(void) THROWS_NONE { return m_Ptr; } /// Get pointer, /// /// Same as GetPointerOrNull(). /// /// @sa /// GetPointerOrNull() inline TObjectType* GetPointer(void) THROWS_NONE { return GetPointerOrNull(); } /// Get object. /// /// Similar to GetNonNullPointer(), except that this method returns the /// object whereas GetNonNullPointer() returns a pointer to the object. /// /// @sa /// GetNonNullPointer() inline TObjectType& GetObject(void) { return *GetNonNullPointer(); } /// Dereference operator returning object. /// /// @sa /// GetObject() inline TObjectType& operator*(void) { return *GetNonNullPointer(); } /// Reference operator. /// /// @sa /// GetPointer() inline TObjectType* operator->(void) { return GetNonNullPointer(); } /// Dereference operator returning pointer. /// /// @sa /// GetPointer() inline operator TObjectType*(void) { return GetPointerOrNull(); } // Const getters. /// Get pointer value and throw a null pointer exception if pointer /// is null -- constant version. /// /// Similar to GetPointerOrNull() except that this method throws a null /// pointer exception if pointer is null, whereas GetPointerOrNull() /// returns a null value. /// /// @sa /// GetPointerOrNull(), GetPointer(), GetObject() const TObjectType* GetNonNullPointer(void) const { const TObjectType* ptr = m_Ptr; if ( !ptr ) { CObject::ThrowNullPointerException(); } return ptr; } /// Get pointer value -- constant version. /// /// Similar to GetNonNullPointer() except that this method returns a null /// if the pointer is null, whereas GetNonNullPointer() throws a null /// pointer exception. /// /// @sa /// GetNonNullPointer() const TObjectType* GetPointerOrNull(void) const THROWS_NONE { return m_Ptr; } /// Get pointer -- constant version, /// /// Same as GetPointerOrNull(). /// /// @sa /// GetPointerOrNull() inline const TObjectType* GetPointer(void) const THROWS_NONE { return GetPointerOrNull(); } /// Get object -- constant version. /// /// Similar to GetNonNullPointer(), except that this method returns the /// object whereas GetNonNullPointer() returns a pointer to the object. /// /// @sa /// GetNonNullPointer() inline const TObjectType& GetObject(void) const { return *GetNonNullPointer(); } /// Dereference operator returning object -- constant version. /// /// @sa /// GetObject() inline const TObjectType& operator*(void) const { return *GetNonNullPointer(); } /// Reference operator -- constant version. /// /// @sa /// GetPointer() inline const TObjectType* operator->(void) const { return GetNonNullPointer(); } /// Dereference operator returning pointer -- constant version. /// /// @sa /// GetPointer() inline operator const TObjectType*(void) const { return GetPointerOrNull(); }private: TObjectType* AtomicSwap(TObjectType* ptr) { // MIPSpro won't accept static_cast for some reason. return reinterpret_cast<TObjectType*> (SwapPointers(const_cast<void*volatile*>( reinterpret_cast<void**>(&m_Ptr)), ptr)); } TObjectType* m_Ptr; ///< Pointer to object};/////////////////////////////////////////////////////////////////////////////////// CConstRef --////// Define a template class that stores a pointer to an object and defines/// methods for constant referencing of object. template<class C>class CConstRef {public: typedef C element_type; ///< Define alias element_type typedef const element_type TObjectType; ///< Define alias TObjectType /// Constructor for null pointer. inline CConstRef(void) THROWS_NONE : m_Ptr(0) { } /// Constructor for ENull pointer.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -