//: C06:Stash2.cpp {O}
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Constructors & destructors
#include "Stash2.h"
#include "../require.h"
#include <iostream>
#include <cassert>
using namespace std;
const int increment = 100;
/* Questo esempio è simile a quello già proposto a lezione con il nome di Clib.cpp.
In questo caso però si ha un costruttore: Stash(int).
Il compito de costruttore "Stash" è l'assegnamento "size = sz".*/Stash::Stash(int sz)
{
size = sz;
quantity = 0;
storage = 0;
next = 0;}
int Stash::add(void* element)
{
/* Enough space left? */
if(next >= quantity)
inflate(increment);
/* 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++;
/* Index number */
return(next - 1);}
void* Stash::fetch(int index)
{
/* La funzione di libreria "require" se non soddisfatta stampa la stringa che ha come
argomento.*/require(0 <= index, "Stash::fetch (-)index");
if(index >= next)/* To indicate the end */
return 0;
/* Produce pointer to desired element: */return &(storage[index * size]);
}
int Stash::count()
{
/* Number of elements in CStash */
return next;}
void Stash::inflate(int increase)
{
/* La funzione di libreria "require" se non soddisfatta stampa la stringa che ha come argomento.*/
require(increase > 0, "Stash::inflate zero or negative 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++)
/* Copy old to new */
b[i] = storage[i];
/* Old storage */delete [](storage);
/* Point to new memory */
storage = b;
quantity = newQuantity;}
/* Il distruttore "~Stash" libera il collegamento tra l'attributo "storage" e la zona di memoria precedentemente allocata. */
Stash::~Stash()
{
if(storage != 0)
{cout << "freeing storage" << endl;
delete []storage;}
} ///:~