#include <RCArray.h>
Inheritance diagram for RCArray< T >:
Public Member Functions | |
RCArray () | |
Argumentless constructor: the RCArray will contain no data elements. | |
ValRef | operator[] (const int ix) throw (std::out_of_range) |
T | operator[] (const int ix) const |
ValRef | operator[] (const size_t ix) throw (std::out_of_range) |
T | operator[] (const size_t ix) const |
void | append (T val) |
Add a new element to the end of the RCArray, resulting in length() + 1 data elements. | |
Protected Member Functions | |
void | init (T intialVal) |
Initialize the array with a value of type T. | |
RCArray (size_t initialSize) | |
Create an RCArray with length() == initialSize. | |
RCArray (size_t initialSize, T initVal) | |
Create an RCArray with length() == initialSize, where length() data elements are initialized to initialValue. | |
T | read (const int i) const |
Return the element at index i. | |
void | write (const int i, const T v) |
Write value v at index i. | |
size_t | length () const |
Return the number of data elements in the RCArray. | |
size_t | getRefCnt () |
Return the reference count. | |
Protected Attributes | |
RCPtr< SharedData > | value |
Objects that are instantiated with this template have three characteristics:
An assignment of the object increments the reference count rather than copying memory. For example, in the code below myObj1 is initialized and then assigned to myObj2. myObj1 and myObj2 now share the same memory (there was no copy) and the reference count will be 2.
#include <iostream> #include "RCArray.h"
typedef RCArray<int> IntVec;
main() { IntVec myObj1, myObj2;
for (int i = 1; i <= 10; i++) { myObj1.append( i ); } myObj2 = myObj1; for (i = 0; i < 10; i++) { std::cout << myObj2[i] << " "; } std::cout << std::endl; }If this code is complex and executed it will print:
1 2 3 4 5 6 7 8 9 10
A change to an object with multiple references results in a copy-on-write. That instance of the object will be changed while the others remain unchanged. Since a unique copy has been made, the reference count will be reduced by one. For example, the assignment
myObj1[4] = 42;will only change myObj1. The contents of myObj2 remain {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.
When the reference count reaches zero, the memory for the object will be automatically deallocated. This allows reference counted array used as if they were elemental values, without worrying about memory allocation and deallocation.
Only the argumentless constructor of RCArray is made public. Classes can expose the other constructors and protected
functions like length() and getRefCnt() (which is useful in testing and verification) by deriving a class from an instance of this template. For example:
#include "RCArray.h"
typedef RCArray<double> DoubleArray;
class DoubleVec : public DoubleArray { private: void init(double intialVal ); public: DoubleVec() : DoubleArray() {} DoubleVec(size_t initialSize) : DoubleArray( initialSize ) {} DoubleVec(size_t initialSize, double initVal ) : DoubleArray(initialSize, initVal) {}
size_t length() { return DoubleArray::length(); } size_t getRefCnt() { return DoubleArray::getRefCnt(); } };The RCArray template has a single data element, value, which is a "smart" pointer. Note that there is no overloaded assignment operator. The default RCArray assignment copies the value element. This in turn results in a copy of the RCPtr instantiated template object (the smart pointer).
The RCArray template uses the default destructor (oh, the horror!) The default destructor will call the destructor on value which will, in turn invoke the removeReference() function in RCObject. This will decrement the reference count. When the reference count reaches zero, the shared data will be deleted.
Compiling the code
The GrowableArray template will throw a runtime exception if the array is indexed beyond the bounds of the data. At least in the case of the Microsoft compilers, this requires that you include the -GX flag. For example:
cl -TP -GX -Zi test.C -o test(the -Zi flag compiles for debug).
Definition at line 156 of file RCArray.h.
Constructor & Destructor Documentation
|
Create an RCArray with length() == initialSize. The length() data elements are uninitialized and their value is undefined. Definition at line 235 of file RCArray.h.
00235 : 00236 value( new SharedData(initialSize) ) {} |
|
Create an RCArray with length() == initialSize, where length() data elements are initialized to initialValue.
Definition at line 245 of file RCArray.h. References RCArray< T >::init().
|
|
Argumentless constructor: the RCArray will contain no data elements.
Definition at line 226 of file RCArray.h.
00226 : value( new SharedData() ) {} |
|
Add a new element to the end of the RCArray, resulting in length() + 1 data elements.
Definition at line 258 of file RCArray.h. References RCArray< T >::value.
00259 { 00260 value->data.append( val ); 00261 } |
|
Return the reference count.
Reimplemented in DoubleVec, and String. Definition at line 279 of file RCArray.h. References RCArray< T >::value.
00280 { 00281 return value->getRefCnt(); 00282 } |
|
Initialize the array with a value of type T.
Reimplemented in DoubleVec. Definition at line 213 of file RCArray.h. References RCArray< T >::value. Referenced by RCArray< T >::RCArray().
|
|
Return the number of data elements in the RCArray.
Definition at line 268 of file RCArray.h. References RCArray< T >::value.
00269 { 00270 return value->data.length(); 00271 } |
|
Definition at line 310 of file RCArray.h. References RCArray< T >::value.
00311 { 00312 return value->data[ix]; 00313 } |
|
Definition at line 317 of file RCArray.h.
00319 { 00320 if (ix >= value->data.length()) { 00321 const char *errMsg = "operator[] - index out of bounds"; 00322 throw std::out_of_range( errMsg ); 00323 } 00324 return ValRef( *this, ix ); 00325 } |
|
Definition at line 288 of file RCArray.h. References RCArray< T >::value.
00289 { 00290 return value->data[ix]; 00291 } |
|
Definition at line 297 of file RCArray.h.
00299 { 00300 if (ix < 0 || ix >= value->data.length()) { 00301 const char *errMsg = "operator[] - index out of bounds"; 00302 throw std::out_of_range( errMsg ); 00303 } 00304 return ValRef( *this, ix ); 00305 } |
|
Return the element at index i.
Implements RCBase< T >. Definition at line 333 of file RCArray.h. References RCArray< T >::value.
00334 { 00335 return value->data[i]; 00336 } |
|
Write value v at index i.
Implements RCBase< T >. Definition at line 344 of file RCArray.h. References RCArray< T >::value.
|
|
Definition at line 182 of file RCArray.h. Referenced by RCArray< T >::append(), RCArray< T >::getRefCnt(), RCArray< T >::init(), RCArray< T >::length(), RCArray< T >::operator[](), RCArray< T >::read(), and RCArray< T >::write(). |