//: C06:Stash2Test.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
//{L} Stash2
// Constructors & destructors
#include "Stash2.h"
#include "../require.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

/* Questo esempio è simile a quello già proposto a lezione con il nome di ClibTest.cpp.
In questo caso però, si utilizzano costruttori e distruttori. */

int main()
{

/* Qui di seguito viene chiamato il costrutore della classe Stash che assegna
ad un attributo dell'oggetto intStash il valore di "sizeof(int)=4" */

Stash intStash(sizeof(int));
for(int i = 0; i < 100; i++)

intStash.add(&i);

for(int j = 0; j < intStash.count(); j++)

cout << "intStash.fetch(" << j << ") = "
<< *(int*)intStash.fetch(j) << endl;

const int bufsize = 80;

/* In questo caso viene chiamato il costruttore che assegna ad un attributo dell'oggetto
stringStash il valore di "sizeof(char)*bufsize=80 */

Stash stringStash(sizeof(char) * bufsize);
ifstream in("Stash2Test.cpp");

/* La funzione qui di seguito è una funzione di libreria*/

assure(in, " Stash2Test.cpp");
string line;
while(getline(in, line))

stringStash.add((char*)line.c_str());

int k = 0;
char* cp;
while((cp = (char*)stringStash.fetch(k++))!=0)

cout << "stringStash.fetch(" << k << ") = " << cp << endl;

} ///:~