casacore
|
Copy-On-Write-Pointer class - allows control of copy based on constness. More...
#include <COWPtr.h>
Public Member Functions | |
COWPtr () | |
The default constructor: used to create a null pointer which is delete-able by the destructor. More... | |
COWPtr (T *obj, Bool deleteIt=True, Bool readOnly=False) | |
The dynamic "pointer to object" constructor: default behavior is to delete the allocated memory when this instance's of COWPtr is destructed. More... | |
COWPtr (const COWPtr< T > &other) | |
copy ctor with reference semantics More... | |
COWPtr & | operator= (const COWPtr< T > &other) |
assignment operator with reference semantics More... | |
const T * | operator-> () const |
return a pointer to a const object. More... | |
const T & | operator* () const |
return a reference to a const object. More... | |
void | set (T *obj, Bool deleteIt=True, Bool readOnly=False) |
Function used to change this instance of COWPtr. More... | |
void | setReadOnly (const T *obj) |
void | setReadOnly () |
const T & | ref () const |
return a const reference to the object. More... | |
T & | rwRef () |
return a readable and writable reference to this instance. More... | |
Bool | isNull () const |
returns False if this contains a non-null ptr, otherwise, return True. More... | |
Bool | isReadOnly () const |
returns True if the object is const, otherwise, return False. More... | |
Bool | isUnique () const |
returns True if the object is the only instance, otherwise, return False. More... | |
Bool | makeUnique () |
Return True if copied, otherwise, False. More... | |
Protected Attributes | |
CountedPtr< T > | obj_p |
Bool | const_p |
Copy-On-Write-Pointer class - allows control of copy based on constness.
Public interface
The COWPtr class name is a contraction of Copy-On-Write-Pointer and is a reflection of its role as a carrier of objects which need to minimize their copying and control their destruction. Such objects only need to copy if written to.
COWPtr can be used by other classes to implement copy-on-write semantics. Copy-on-write means that a copy of an object is not made until necessary. A well-known example is a String class with internally a pointer to a StringRep containing the true string. When a copy of a String is made, the StringRep is not copied yet. Only when the String gets changed and when more than one String points to the same StringRep, a copy of the StringRep is made. This technique can prevent a lot of copying when arguments are passed by value.
Implementing a String in this way is straightforward when String defines the pointer to its StringRep as COWPtr<StringRep>
and uses the appropriate functions (ref() and rwRef()) to execute const and non-const StringRep functions.
An example of this (straightforward) usage is class RecordDesc.
COWPtr offers possibilities for more advanced usage:
COWPtr<Array>
with readOnly=True, it is assured that a copy is made as soon as somebody wants to change the returned Array object. No (expensive) copy is made when only const access is being done. Apart from the fact that COWPtr handles the copying, it has the big advantage that it forces that its access functions (ref and rwRef) are used in the correct way (ie. ref() for a const function and rwRef() for a non-const function). This ensures that copies are made when needed and not made when not needed.
Note that COWPtr uses the default constructor and the assignment operator to make a copy (thus not the copy constructor). The reason for this is that the copy constructor of some classes (e.g. Array) has reference semantics iso. copy semantics.
This function requires a const Array be passed out from the local scope. The Array is created with non-const functions out of necessity (i.e. no const versions of the Array::getSlice() function exist.) Preventing copies of the Array from being made forces us to use a COWPtr. The COWPtr has arguments which allow us to declare the Array as const and not make any copies until a write operation is performed.
The caller of the function will get their piece of a const array without making a copy until the last possible moment (maybe never.)
Three words; efficiency, efficiency, efficiency. Not everything may be passed as a reference. With COWPtrs we may fake it.
|
inline |
The default constructor: used to create a null pointer which is delete-able by the destructor.
Make our own default pointer - deleteIt==True by default, const_p==False.
It is not "readOnly" so that it may be changed by the COWPtr<T>::set() function.
does nothing
|
explicit |
The dynamic "pointer to object" constructor: default behavior is to delete the allocated memory when this instance's of COWPtr is destructed.
Or the Boolean argument of "deleteIt = False" implies the pointer is being maintained by an object other than this instance of COWPtr and will not delete the allocated memory upon this instance's destruction. Control of copying is provided by the Boolean "readOnly" argument. The default value of "readOnly = False" forces a copy if the number of references to the dynamic memory is greater than one. Copying is always done if the constructor is given an argument of "readOnly = True".
Note: The only copying done (if ever) is upon a call to COWPtr<T>::rwRef();
|
inline |
|
inline |
|
inline |
|
inline |
returns True if the object is the only instance, otherwise, return False.
Definition at line 339 of file COWPtr.h.
References casacore::False, and casacore::True.
Bool casacore::COWPtr< T >::makeUnique | ( | ) |
Return True if copied, otherwise, False.
This function will make this instance's object a copy if it is constructed with "readOnly = True." Additionally, all instances of COWPtr with more than one reference to the allocated memory stored within will be copied.
|
inline |
|
inline |
|
inline |
assignment operator with reference semantics
Definition at line 289 of file COWPtr.h.
References casacore::COWPtr< T >::const_p, and casacore::COWPtr< T >::obj_p.
|
inline |
|
inline |
void casacore::COWPtr< T >::set | ( | T * | obj, |
Bool | deleteIt = True , |
||
Bool | readOnly = False |
||
) |
Function used to change this instance of COWPtr.
The pointer must be dynamically allocated. Default behavior is to delete the allocated memory when this instance's of COWPtr is destructed. Or the Boolean argument of "deleteIt = False" implies the pointer is being maintained by an object other than this instance of COWPtr and will not delete the allocated memory upon this instance's destruction. Control of copying is provided by the Boolean "readOnly" argument. The default value of "readOnly = False" forces a copy if the number of references to the dynamic memory is greater than one. Copying is always done if the constructor is given an argument of "readOnly = True".
Note: The only copying done (if ever) is upon a call to COWPtr<T>::rwRef();
The setReadOnly
function is the same as set
, but forces deleteIt=False
and ReadOnly=True
. In that way a const object can also be safely referenced by COWPtr.
|
inline |
Definition at line 298 of file COWPtr.h.
References casacore::False, and casacore::True.
|
inline |
Definition at line 303 of file COWPtr.h.
References casacore::True.
|
protected |
Definition at line 268 of file COWPtr.h.
Referenced by casacore::COWPtr< T >::operator=().
|
protected |
Definition at line 267 of file COWPtr.h.
Referenced by casacore::COWPtr< T >::operator=().