//: C04:CppLib.cpp {O}
// C library converted to C++
// Declare structure and functions:
#include "CppLib.h"
#include <iostream>
using namespace std;/* Quantity of elements to add when increasing storage: */
const int increment = 100;
void Stash::initialize(int sz)
{
size = sz;
quantity = 0;
storage = 0;
next = 0;}
int Stash::add(const void* element)
{/* Si noti l'intestazione delle definizione di funzione contraddistinta prima dal nome dell'ambito ove la funzione è dichiarata (il nome della struct) seguito poi da :: e poi il nome della member function.*/
if(next >= quantity)
/* Enough space left?*/
inflate(increment);
/* Osserviamo poi che ogni attributo della struct non è più definito dall'operatore -> in quanto essendo nella member function gli attributi vengono visti direttamente.*/
/* Copy element into storage, starting at next empty space:*/int startBytes = next * size;
unsigned char* e = (unsigned char*)element;
for(int i = 0; i < size; i++)
storage[startBytes + i] = e[i];
next++;
return(next - 1);/* Index number */
}
void* Stash::fetch(int index)
{/* Check index boundaries: */
if(index >= next)
return 0;/* To indicate the end*/
/* Produce pointer to desired element: */return &(storage[index * size]);
}
int Stash::count()
{
return next;
/* Number of elements in CStash */
}
void Stash::inflate(int increase)
{
int newQuantity = quantity + increase;
int newBytes = newQuantity * size;
int oldBytes = quantity * size;
unsigned char* b = new unsigned char[newBytes];
for(int i = 0; i < oldBytes; i++)
b[i] = storage[i];/* Copy old to new */
delete []storage;
/* Old storage */
storage = b; // Point to new memory
quantity = newQuantity;}
void Stash::cleanup()
{
if(storage != 0)
{
cout << "freeing storage" << endl;
delete []storage;}
} ///:~