home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C13 / PStash.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.0 KB  |  37 lines

  1. //: C13:PStash.cpp {O}
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Pointer Stash definitions
  7. #include "PStash.h"
  8. #include <iostream>
  9. #include <cstring> // 'mem' functions
  10. using namespace std;
  11.  
  12. int PStash::add(void* element) {
  13.   const int inflateSize = 10;
  14.   if(next >= quantity)
  15.     inflate(inflateSize);
  16.   storage[next++] = element;
  17.   return(next - 1); // Index number
  18. }
  19.  
  20. // Operator overloading replacement for fetch
  21. void* PStash::operator[](int index) const {
  22.   if(index >= next || index < 0)
  23.     return 0;  // Out of bounds
  24.   // Produce pointer to desired element:
  25.   return storage[index];
  26. }
  27.  
  28. void PStash::inflate(int increase) {
  29.   const int psz = sizeof(void*);
  30.   void** st = new void*[quantity + increase];
  31.   memset(st, 0, (quantity + increase) * psz);
  32.   memcpy(st, storage, quantity * psz);
  33.   quantity += increase;
  34.   delete []storage; // Old storage
  35.   storage = st; // Point to new memory
  36. } ///:~
  37.