#include <list>
#include "listen2.H"
#include <strstream.h>
//#include <unistd.h>
//#include <signal.h>

template <class objtype>
class reloadable
{
public:
  static objtype *create(istream &ist)
  {
    objtype *neues=new objtype;
    if (ist>>(*neues))
      return neues;
    return NULL;
  }	
};

class atype : public zeigerelemtype<atype> ,public my_io_streamtype, public reloadable<atype>
{
public:
  int a;
  int compare(int _a)
  {
    return (a==_a);
  }
  int calc()
  {
    a--;
    return(!a);
  }
  istream &read(istream &ist)
  {
    if (ist.ipfx(0))
      {
	ist >> a;
	ist.isfx();
      }
    return ist;
  }
  ostream &write(ostream &ost) const
  {
    if (ost.opfx())
      {
	ost << a;
	ost.osfx();
      }
    return ost;
  }
};

class akettetype : public kette_iostype<atype>
{
  int none;
};




/* testen virtueller Basisklassen - todo compiler fehler mailen !!
template <class objtype>
class a
{
public:
  objtype ai;
  a()
  {ai=4;}
  virtual ~a()
  {}

};

template <class objtype>
class b : public virtual a<objtype>
{
public:
int bi;
};

template <class obj>
class c: public virtual a<obj>
{
public:
};

class d: public b<int>, public c<int>
{
public:
};
*/

int main()
{ 
  iotype io;
  list<int32> l;
  l.push_back(1);
  l.push_back(2);
  l.push_back(3);
  l.push_back(4);
  l.push_back(5);
  list<int32>::iterator pos;
  pos=l.begin();
  l.erase(pos,++pos);
  cout << "Letztes: "<<*(l.rbegin())<<endl;
  cout << "Liste vorher ";
  for (pos=l.begin();pos!=l.end();++pos)
    cout << *pos << " ";

  change_container(&io,CFG_TEXT_MODE,l);
  iotype oi(&io);
  change_container(&oi,CFG_TEXT_MODE,l);
  cout << "\nListe nachher ";
  for (pos=l.begin();pos!=l.end();++pos)
    cout << *pos << " ";

  exit(0);

  string tstring;
  strstream test;
  for (int a=0;a!=1000;a++)
    {
      test << "test: "<<a<<endl<<flush;
      while (test>>tstring)
	cout <<tstring<<endl;
      test.clear();
    }
  char *text=new char[100];
  text[0]='t';
  text[1]='e';
  text[2]='s';
  text[3]='t';
  text[4]=' ';
  text[5]='\n';
  text[6]=0;


  istrstream *is=new istrstream(text,7);
  *is >>tstring;
  delete is;
  text[4]='A';
  delete text;
  cout<<tstring<<endl;
  exit(0);

  /* compiler fehler
  d *dptr=new d;
  delete dptr;
  */

  // Erstelle eine Kette
  kettetype<atype> akette;
  akettetype *aketteptr;
  aketteptr=new akettetype;
  aketteptr->zeiger_push(new atype);
  aketteptr->zeiger_push(new atype);
  aketteptr->zeiger_push(new atype);
  aketteptr->anfang->next->a=3;
  aketteptr->anfang->next->next->a=5;
  aketteptr->anfang->next->next->next->a=4;
  cout <<"Testing some routines :\n"
       <<"1. calculating list (in this case decrement every list elem and kill when 0)\n"
       <<"2. searching for an element in the list that is equal to 2\n";
  
  atype *found;

  cout <<"I start with this: "<<(*aketteptr)<<endl;
  while (kette_calc(aketteptr))
    {
      cout <<(*aketteptr)<<endl;
      if (kette_search(aketteptr,2,found))
	cout <<"Found it.\n";
      else
	cout <<"Sorry but there is no 2\n";
    }

  delete aketteptr;

  cout <<"Now i am creating a list out of an istrstream...\n";
  istrstream ist("2 3 5\n");
  create_kettetype<atype> *create_ketteptr=new create_kettetype<atype>;
  kettetype<atype> *oketteptr=create_ketteptr->create(ist);
  if (!oketteptr)
    cout << "Konstruktion failed\n";
  else
    {
      cout << "It works !\n";
      cout << (*oketteptr)<<endl;
    }
  delete oketteptr;
  
  cout << "Your Part:\nPlease enter a List (ex.:'2 6 5' the first digit is the no. of elem.):\n";
  oketteptr=create_ketteptr->create(cin);
  if (oketteptr)
    {
      cout << (*oketteptr)<<endl;
      cout << "Please enter a similar List (reload, same no. of elements): "<<flush;
      if (cin >> (*oketteptr))
	cout << (*oketteptr)<<endl;
      else
	cout << "Bad Input\n";
    }
  else
    cout << "Bad Input\n";
  delete oketteptr;
  delete create_ketteptr;
  exit (0);
}





