Ref< Item > Class Template Reference

This is a ref-count token object that works with any instantiation type that is a subclass of RefCountable. More...

#include <RefCount.h>

List of all members.

Public Types

typedef ObjectPool< Item > ItemPool
 type of an ObjectPool of user data structures

Public Member Functions

 Ref ()
 Default constructor.
 Ref (Item *item, bool doRefCount=true)
 Creates a new reference-count for the given item.
 Ref (const Ref &copyMe)
 Copy constructor.
 Ref (const GenericRef &ref, bool junk)
 Attempts to set this reference by downcasting the reference in the provided GenericRef.
 ~Ref ()
 Unreferences the held data item.
void SetRef (Item *item, bool doRefCount=true)
 Convenience method; unreferences any item this Ref is currently referencing; and causes this Ref to reference the given Item instead.
Refoperator= (const Ref &rhs)
 Assigment operator.
bool operator== (const Ref &rhs) const
 Returns true iff both Refs are referencing the same data.
bool operator!= (const Ref &rhs) const
 Returns true iff both Refs are not referencing the same data.
Item * GetItemPointer () const
 Returns the ref-counted data item.
Item * operator() () const
 Convenience synonym for GetItemPointer().
void Reset ()
 Unreferences our held data item (if any), and turns this object back into a NULL reference.
void Neutralize ()
 Equivalent to Reset(), except that this method will not delete or recycle the held object under any circumstances.
void SwapContents (Ref &swapWith)
 Swaps this Ref's contents with those of the specified Ref.
bool IsRefCounting () const
 Returns true iff we are refcounting our held object, or false if we are merely pointing to it (see constructor documentation for details).
GenericRef GetGeneric () const
 Convenience method: Returns a GenericRef object referencing the same RefCountable as this typed ref.
status_t SetFromGeneric (const GenericRef &genericRef)
 Convenience method; attempts to set this typed Ref to be referencing the same item as the given GenericRef.
void SetFromGenericUnchecked (const GenericRef &genericRef)
 Same as SetFromGeneric(), but uses a static_cast instead of a dynamic_cast to do the type conversion.
bool IsRefPrivate () const
 Returns true only if we are certain that no other Refs are pointing at the same RefCountable object that we are.
status_t EnsureRefIsPrivate ()
 This method will check our referenced object to see if there is any chance that it is shared by other Ref objects.


Detailed Description

template<class Item>
class Ref< Item >

This is a ref-count token object that works with any instantiation type that is a subclass of RefCountable.

Definition at line 70 of file RefCount.h.


Constructor & Destructor Documentation

template<class Item>
Ref< Item >::Ref (  )  [inline]

Default constructor.

Creates a NULL reference (suitable for later initialization with SetRef(), or the assignment operator)

Definition at line 79 of file RefCount.h.

template<class Item>
Ref< Item >::Ref ( Item *  item,
bool  doRefCount = true 
) [inline]

Creates a new reference-count for the given item.

Once referenced, (item) will be automatically deleted (or recycled) when the last Ref that references it goes away.

Parameters:
item A dynamically allocated object that the Ref class will assume responsibility for deleting. May be NULL.
doRefCount If set false, then this Ref will not do any ref-counting on (item); rather it just acts as a fancy version of a normal C++ pointer. Specifically, it will not modify the object's reference count, nor will it ever delete the object. Setting this argument to false can be useful if you want to supply a Ref to an item that wasn't dynamically allocated from the heap. But if you do that, it allows the possibility of the object going away while other Refs are still using it, so be careful!

Definition at line 93 of file RefCount.h.

template<class Item>
Ref< Item >::Ref ( const Ref< Item > &  copyMe  )  [inline]

Copy constructor.

Creates an additional reference to the object referenced by (copyMe). The referenced object won't be deleted until ALL Refs that reference it are gone.

Definition at line 98 of file RefCount.h.

template<class Item>
Ref< Item >::Ref ( const GenericRef< Item > &  ref,
bool  junk 
) [inline]

Attempts to set this reference by downcasting the reference in the provided GenericRef.

If the downcast cannot be done (via dynamic_cast) then we become a NULL reference.

Parameters:
ref The generic reference to set ourselves from.
junk This parameter is ignored; it is just here to disambiguate constructors.

Definition at line 105 of file RefCount.h.

template<class Item>
Ref< Item >::~Ref (  )  [inline]

Unreferences the held data item.

If this is the last Ref that references the held data item, the data item will be deleted or recycled at this time.

Definition at line 110 of file RefCount.h.


Member Function Documentation

template<class Item>
void Ref< Item >::SetRef ( Item *  item,
bool  doRefCount = true 
) [inline]

Convenience method; unreferences any item this Ref is currently referencing; and causes this Ref to reference the given Item instead.

Very similar to the assignment operator. (in fact the assignment operator just calls this method)

Parameters:
item A dynamically allocated object that this Ref object will assume responsibility for deleting. May be NULL, in which case the effect is just to unreference the current item.
doRefCount If set false, then this Ref will not do any ref-counting on (item); rather it just acts as a fancy version of a normal C++ pointer. Specifically, it will not modify the object's reference count, nor will it ever delete the object. Setting this argument to false can be useful if you want to supply a Ref to an item that wasn't dynamically allocated from the heap. But if you do that, it allows the possibility of the object going away while other Refs are still using it, so be careful!

Definition at line 125 of file RefCount.h.

Referenced by Ref< Socket >::EnsureRefIsPrivate(), Ref< Socket >::operator=(), Ref< Socket >::SetFromGeneric(), and Ref< Socket >::SetFromGenericUnchecked().

template<class Item>
Ref& Ref< Item >::operator= ( const Ref< Item > &  rhs  )  [inline]

Assigment operator.

Unreferences the previous held data item, and adds a reference to the the data item of (rhs).

Definition at line 158 of file RefCount.h.

template<class Item>
Item* Ref< Item >::GetItemPointer (  )  const [inline]

Returns the ref-counted data item.

The returned data item is only guaranteed valid for as long as this RefCount object exists.

Definition at line 169 of file RefCount.h.

template<class Item>
void Ref< Item >::Reset (  )  [inline]

Unreferences our held data item (if any), and turns this object back into a NULL reference.

(equivalent to *this = Ref();)

Definition at line 177 of file RefCount.h.

Referenced by Ref< Socket >::SetFromGeneric(), XorDataIO::Shutdown(), UDPSocketDataIO::Shutdown(), TCPSocketDataIO::Shutdown(), QSocketDataIO::Shutdown(), PacketizedDataIO::Shutdown(), and ByteBufferDataIO::Shutdown().

template<class Item>
void Ref< Item >::Neutralize (  )  [inline]

Equivalent to Reset(), except that this method will not delete or recycle the held object under any circumstances.

Use with caution.

Definition at line 182 of file RefCount.h.

Referenced by QSocketDataIO::ReleaseSocket().

template<class Item>
void Ref< Item >::SwapContents ( Ref< Item > &  swapWith  )  [inline]

Swaps this Ref's contents with those of the specified Ref.

Parameters:
swapWith Ref to swap state with.

Definition at line 187 of file RefCount.h.

template<class Item>
status_t Ref< Item >::SetFromGeneric ( const GenericRef< Item > &  genericRef  )  [inline]

Convenience method; attempts to set this typed Ref to be referencing the same item as the given GenericRef.

If the conversion cannot be done, our state will remain unchanged.

Parameters:
genericRef The GenericRef to set ourselves from.
Returns:
B_NO_ERROR if the conversion was successful, or B_ERROR if the GenericRef's item type is incompatible with our own item type (as dictated by dynamic_cast)

Definition at line 207 of file RefCount.h.

Referenced by Message::FindFlat(), and Ref< Socket >::Ref().

template<class Item>
void Ref< Item >::SetFromGenericUnchecked ( const GenericRef< Item > &  genericRef  )  [inline]

Same as SetFromGeneric(), but uses a static_cast instead of a dynamic_cast to do the type conversion.

This is faster that SetFromGeneric(), but it is up to the user's code to guarantee that the conversion is valid. If (genericRef) references an object of the wrong type, undefined behaviour (read: crashing) will likely occur.

Definition at line 227 of file RefCount.h.

Referenced by Message::AddFlat(), Message::PrependFlat(), and Message::ReplaceFlat().

template<class Item>
bool Ref< Item >::IsRefPrivate (  )  const [inline]

Returns true only if we are certain that no other Refs are pointing at the same RefCountable object that we are.

If this Ref's do-reference-counting flag is false, then this method will always return false, since we can't be sure about sharing unless we are reference counting. If this Ref is a NULL Ref, then this method will return true.

Definition at line 238 of file RefCount.h.

Referenced by Ref< Socket >::EnsureRefIsPrivate().

template<class Item>
status_t Ref< Item >::EnsureRefIsPrivate (  )  [inline]

This method will check our referenced object to see if there is any chance that it is shared by other Ref objects.

If it is, it will make a copy of the referenced object and set this Ref to reference the copy instead of the original. The upshot of this is that once this method returns B_NO_ERROR, you can safely modify the referenced object without having to worry about race conditions caused by sharing data with other threads. This method is thread safe -- it may occasionally make a copy that wasn't strictly necessary, but it will never fail to make a copy when making a copy is necessary.

Returns:
B_NO_ERROR on success (i.e. the object was successfully copied, or a copy turned out to be unnecessary), or B_ERROR on failure (i.e. out of memory)

Definition at line 256 of file RefCount.h.


The documentation for this class was generated from the following file:
Generated on Mon Apr 28 13:59:31 2008 for MUSCLE by  doxygen 1.5.1